PDA

View Full Version : horizontal movement loop



bigteeth
October 18th, 2003, 09:31 AM
First of all I want to thank you Kirupa for all the wonderful tutorials. I am just learning actionscript, and your site has been able to clarify a number of things for me. With that said my question is this:

In your tutorial you used the following code to create horizontal movement:onClipEvent(enterFrame) { speed = 10; this._x += speed; }

how would you create a continuous loop of this movement (ie. make the movie move horizontally across the screen over and over).

Sorry if this is an insultingly simple question.

Voetsjoeba
October 18th, 2003, 09:44 AM
On the timeline in which the movieclip is in ...



startpos = 0-bal._width/2
limit = 700
bal._x = startpos
bal.onEnterFrame = function(){
this._x >= limit+this._width/2 ? this._x = startpos : this._x +=10
updateAfterEvent()
}

ahmed
October 18th, 2003, 09:44 AM
onClipEvent(enterFrame) {
speed = 10;
this._x += speed;
if ( this._x > 550 ) this._x = 0;
}
The last line check if the object is at the end of the stage and sets its position to 0 if it is :)

[edit] or do what V suggested :P

bigteeth
October 18th, 2003, 10:13 AM
Originally posted by ahmed

onClipEvent(enterFrame) {
speed = 10;
this._x += speed;
if ( this._x > 550 ) this._x = 0;
}
The last line check if the object is at the end of the stage and sets its position to 0 if it is :)

[edit] or do what V suggested :P



Thank you, that worked brilliantly!

senocular
October 18th, 2003, 10:41 AM
Im going to step in and be picky

Consider changing your script to:


onClipEvent(load) {
speed = 10;
}
onClipEvent(enterFrame) {
this._x += speed;
if ( this._x > 550 ) this._x = 0;
}

This does 2 things. 1) It prevents the continuous reassignment of speed every frame where it only needs to be defined once and can then just be left alone. 2) It allows you to, should you need to, change speed. Now, if you tried to change the speed variable, it would reset itself back to 10 right before it was used making any changes irrelavent. Having defined it to start in load lets you change it without it resetting itself every frame.

:D