PDA

View Full Version : Simple bouncing effect using "this._x -="


BookMan
11-07-2003, 07:00 PM
Hi,

I'm working through Billy T's tutorial on animation via Actionscript and I want to vary it just a little.

To move a clip or clips that have been either drawn on the stage or placed there using duplicate, I can do this:


onClipEvent(load){
this._x = 30;
}
onClipEvent (enterFrame) {
if (this._x<500) {
this._x += 15;
}
}


That works great. But if I want to then move the clip back the other way (I'm not worried about acceleration or random movement just yet), I need to add another condition:


} else if (this._x>499) {
this._x -= 15;
}


That doesn't work properly. The clip moves rapidly back and forth 15 pixels at a time once it has reached 500.

Why? Because both conditions are true at the same time! How can I get over this? I've searched this forum and seen quite a few examples of similar things, but they have additional clever stuff like random movement and acceleration and I can't work out what I do and don't need for my simple example.

I know that I should be able to work this out, but I've been trying for ages and my brain is starting to melt...

Thanks for any help.

BookMan

baby_annie
11-08-2003, 11:18 PM
Both conditions not true in the same time.
the ball back and forth because:
First the X position is 30

onClipEvent(load){
this._x = 30;
}

So the firstcondition is true:

onClipEvent (enterFrame) {
if (this._x<500) {
this._x += 15;
}
}


, then it add 15 each time until it is 510.(495 + 15) It reachs the second condition:

else if (this._x>499) {
this._x -= 15;
}

Now the X position is 510 - 15 =495, it reachs the first condition....And it return to loop ;)
That's is why the ball back and forth. Hope it helps!

*There are some tutorials about movement at www.bit-101.com very worth to read.

Warrior
11-10-2003, 03:01 AM
I would fix that comma at the end of the link baby_annie ;)

BookMan
11-10-2003, 03:47 AM
Thanks baby_annie,

I did the gravity tutorial on the link you posted and everything is much clearer now - phew!

BookMan

baby_annie
11-10-2003, 11:03 AM
I would fix that comma at the end of the link
LOL, I edited this link 1 time. But it's still have a comma mistake :confused: