Back and forth
Code Actionscript 2.0
Instance action
onClipEvent (load) {
speedx = 10;
}
onClipEvent (enterFrame) {
this._x = this._x + speedx;
if (this._x > 300) {
this._x = 300;
speedx = -speedx;
}
if (this._x < 100) {
this._x = 100;
speedx = -speedx;
}
}
Code Actionscript 3.0
addEventListener(Event.ENTER_FRAME, enterFrame); var speedx = 10; function enterFrame(event:Event) { square.x = square.x + speedx; if (square.x > 300) { square.x = 300; speedx = -speedx; } if (square.x < 100) { square.x = 100; speedx = -speedx; } }
Description
This script causes the square to move back and forth. The horizontal position of the square is increased by the value of speedx, in each step. The if-condition verifies, whether the square has exceeded the determined value on the left or on the right. If this is the case, speedx changes its direction. Unlike the example which demonstrated an oscillating movement, the change of speed occurs immediately.
related to: Oscillating movement
Download
Right click: Flashfile AS 2.0 | Flashfile AS 3.0 | SWF-File