A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [mx04]Script to fire when clip is hit firing prior to collision

  1. #1
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575

    [mx04]Script to fire when clip is hit firing prior to collision

    This is really freaking me out.

    I have got a simple game where you add sections of track to the stage & then get the ball to move along it.

    At this very early stage there are only 3 types of track – “straight right”, “straight left” & “up.”

    It all works great except if you lay the track in this sequence:

    First track - “straight right” another “straight right” then 2 “up” followed by 2 “straight left” then 1 “up” – for some reason on the last “up” section the code that is supposed to change the direction of the ball fires much to early before the ball has even hit the clip?

    Any ideas please

    A link to a working version of the movie is HERE

    Fla also attached
    Last edited by pup100; 09-22-2008 at 12:08 AM.
    You will know everything when you know you never will.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe try something like this...

    See attached MX2004 file...
    Last edited by dawsonk; 06-25-2008 at 05:34 PM.

  3. #3
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Many thanks for that dawsonk - yet again a response over & above the call of duty!

    You code is certainly a lot more eloquent & concise than mine + the fact it works!

    Any idea why the ball was taking itself off in the wrong direction in my version?
    You will know everything when you know you never will.

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Basically, in the posted version, it wasn't taking off in the worng direction, so much as the hit test function was using the whole ball against the whole track. And the font was causing the track to register as larger than it appears. Also when going from a type 2 or 3 to a type 1, the _root on enter frame function wasn't getting cleared and would over write the speed values on the next frame update.

    So, the least I could change and get the posted version to work...

    Open the library and in all the movie clips with font arrows, select the arrow and modify/break apart.
    In the 'track' movie clip, open it and center it on the y-axis.

    Then in the code, add the highlighted information shown below.
    Code:
    attachBut_mc.onPress = function() {
    	_root.attachMovie("track", "track" + count, count);
    	_root["track" + count]._x = 100;
    	_root["track" + count]._y = 100;
    	_root["track" + count].num = 1;
    	_root["track" + count].onPress = function() {
    		this.startDrag();
    	};
    	_root["track" + count].onRelease = function() {
    		this.stopDrag();
    		this.yFactor = this._y;
    	};
    	hold_array.push(_root["track" + count]);
    	count++;
    };
    And further down in the code, add this highlighted information.
    Code:
    startBut_mc.onPress = function() {
    	_root.onEnterFrame = null;
    	_root.ballOne.onEnterFrame = function() {
    		//ballX = this._x;
    		this._x += xSpeed;
    		this._y += ySpeed;
    		for (var i in _root.hold_array) {
    			//if (_root.hold_array[i].hitTest(_root.ballOne)) {
    			if (_root.hold_array[i].hitTest(this._x, this._y, true)) {
    				holdNum = _root.hold_array[i].num;
    				xPos = _root.hold_array[i].xFactor;
    				yPos = _root.hold_array[i].yFactor;
    				_root.hold_array.shift();
    				if (holdNum == 1) {
    					this._y = yPos;
    					xSpeed = 2;
    					ySpeed = 0;
    				}
    				if (holdNum == 2) {
    					// _root.onEnterFrame = null;
    					// _root.onEnterFrame = function() {
    					// if (_root.ballOne._y <= yPos) {
    					this._y = yPos;
    					xSpeed = -2;
    					ySpeed = 0;
    					// }
    					// };
    				}
    				if (holdNum == 3) {
    					trace("we");
    					// _root.onEnterFrame = null;
    					// _root.onEnterFrame = function() {
    					// if (_root.ballOne._x >= xPos) {
    					this._x = xPos;
    					xSpeed = 0;
    					ySpeed = -2;
    					// }
    					// };
    				}
    			}
    		}
    	};
    };
    By the way, I've noticed that your logic/coding is getting better and better. Keep up the good work!

    HTH

  5. #5
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Many thanks again dawsonk - sometimes it easier to understand your mistakes when it is in the context of the code you have developed yourself. Having lived with it for a while, you kind of know it inside out & can understand better when someone points out what the problem was.

    Thank you very much also for you kind words on the improvements in my logic/coding. I think for too long now I have convinced myself that I am going to learn all I need to know from books. I think the bottom line is you have to grab the nettle & start coding & developing movies - you will make mistakes, but you learn from them & build your confidence along the way!

    Cheers mate

    Pup100

    Oh, just one last point on the movie:

    I've noticed there is a small jump as the ball gets positioned in the correct location for the clip it is on:

    if (holdNum == 1) {
    this._y = yPos;

    Although totally wrong, my version did avoid this. Any way of losing it in the updated version?

    Thanks again.
    Last edited by pup100; 05-20-2008 at 12:56 AM.
    You will know everything when you know you never will.

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Add in a check to see if the ball is close (within +or - value of speedLim) to the center line of track, before moving it to the center line and changing the speed values.

    Also might want to consider using splice instead of shift on the hold_array. When using shift, the tracks must be generated in the same sequence as the intended path of the ball. Using splice, it will always remove the current track under the ball.

    Code:
    var speedLim = 2;
    startBut_mc.onPress = function() {
    	_root.onEnterFrame = null;
    	_root.ballOne.onEnterFrame = function() {
    		this._x += xSpeed;
    		this._y += ySpeed;
    		for (var i in _root.hold_array) {
    			if (_root.hold_array[i].hitTest(this._x, this._y, true)) {
    				holdNum = _root.hold_array[i].num;
    				xPos = _root.hold_array[i].xFactor;
    				yPos = _root.hold_array[i].yFactor;
    				if (this._x <= xPos + speedLim && this._x >= xPos - speedLim || this._y <= yPos + speedLim && this._y >= yPos - speedLim) {
    					_root.hold_array.splice(i, 1);
    					if (holdNum == 1) {
    						this._y = yPos;
    						xSpeed = 2;
    						ySpeed = 0;
    					}
    					if (holdNum == 2) {
    						this._y = yPos;
    						xSpeed = -2;
    						ySpeed = 0;
    					}
    					if (holdNum == 3) {
    						this._x = xPos;
    						xSpeed = 0;
    						ySpeed = -2;
    					}
    				}
    			}
    		}
    	};
    };

  7. #7
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Many thanks yet again for that dawsonk.
    You will know everything when you know you never will.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center