# Repeat movieclip action in AS

**URL:** <https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471>\
**Category:** flash\
**Created:** [July 9, 2008, 10:02am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471 "2008-07-09T10:02:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AgentOrange](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@AgentOrange](https://forum.kirupa.com/u/AgentOrange)\
**Post date:** [July 9, 2008, 10:02am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/1 "2008-07-09T10:02:18Z")

</div>

Hi all. I’m using the code below to move an instance down a page but I’d like it to run on a loop (just downwards not up and down). What do I need to add to the code?  
Thanks in advance.

onClipEvent(enterFrame) {  
speed = 8;  
this.\_y += speed;  
}

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [July 9, 2008, 10:15am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/2 "2008-07-09T10:15:51Z")

</div>

look at tween class which is repeated when it gets to the bottom of a page

---

<div class="post-metadata">

**Author:** ![AgentOrange](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@AgentOrange](https://forum.kirupa.com/u/AgentOrange)\
**Post date:** [July 9, 2008, 10:31am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/3 "2008-07-09T10:31:36Z")

</div>

Thanks but unfortunately (for whatever reason) the place where I’m working wants pure AS and don’t use classes.

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [July 9, 2008, 10:38am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/4 "2008-07-09T10:38:02Z")

</div>

This is identical to the earlier question you asked about changing the alpha value:

> **[Fade to a percentage](https://www.kirupa.com/forum/showthread.php?t=303128)**
>
> Got a web design/development question or just want to hang out with the most awesome people on the planet? Drop on in!

Rather than provide the code, I’ll leave it as an exercise for you to figure out…suffice to say that you need to include a similar condition that checks when the movieclip has moved past the bottom of your stage, and which then moves it back to its original starting position.

---

<div class="post-metadata">

**Author:** ![AgentOrange](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@AgentOrange](https://forum.kirupa.com/u/AgentOrange)\
**Post date:** [July 9, 2008, 11:20am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/5 "2008-07-09T11:20:49Z")

</div>

Wahey! I had a feeling it would be something similar. Thanks Glos. I got it to stop at a certain point with this:

onClipEvent (enterFrame) {  
speed = 45;  
if (this.\_y \< 130) {  
this.\_y += speed;  
}  
}

But I can’t for the life of me set up the loop. I tried this but it didn’t work.  
onClipEvent (enterFrame) {  
speed = 8;  
if (this.\_y \< 520) {  
this.\_y += speed;  
}  
if (this.\_y = 520) {  
this.\_y = 0;  
}  
}

---

<div class="post-metadata">

**Author:** ![gigaboost](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gigaboost](https://forum.kirupa.com/u/gigaboost)\
**Post date:** [July 9, 2008, 9:18pm UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/6 "2008-07-09T21:18:44Z")

</div>

Heya mate, your problem right now seems to be the missuse of onClipEvent.

onClipEvent(enterFrame){  
//do something  
}  
will trigger the actions inside as many times per second as your frame rate is. So say your frame rate is 30, //do something will be repeated 30 times per second, as long as the movie clip is on stage.

onClipEvent(load){  
//do something else  
}  
will only trigger the events inside the clipevent once, when the movie clip loads (or when it first appears on stage).

onClipEvent(load){  
speed = 10; //loading the variable only once  
}  
onClipEvent(enterFrame){  
\_y += speed; //repeats the action each frame  
}

Hope I helped.

---

<div class="post-metadata">

**Author:** ![AgentOrange](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@AgentOrange](https://forum.kirupa.com/u/AgentOrange)\
**Post date:** [July 10, 2008, 1:15pm UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/7 "2008-07-10T13:15:59Z")

</div>

Hmmmm. I’m not sure I follow. The movement code below works using ‘enterFrame’ but doesn’t as a ‘load’ when attached to the object. I had it in my head that it would run along the lines of getting the object to move to a certain place, stopping and then the code would run again.

onClipEvent (enterFrame) {  
speed = 45;  
if (this.\_y \< 130) {  
this.\_y += speed;  
}  
}

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [July 10, 2008, 3:13pm UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/8 "2008-07-10T15:13:31Z")

</div>

Some tips.  
Firstly, move away from placing code directly on symbols and place it on the timeline instead.  
Secondly, try to write down the effect you want to achieve. This is commonly referred to as pseudo-code which is then easily converted to AS code:  
MC starts at x = ?? and y = ??  
At the frame rate of the movie, move MC downwards  
If the MC moves off the stage, return it to the original start position

This code should do what you want:

```auto
// Initialise start positions
start_x = 200; // horizontal position of movieclip
start_y = 0 - my_mc._height; // places movieclip above stage
// Assign start positions to the movieclip
my_mc._x = start_x;
my_mc._y = start_y;
// Use onEnterFrame handler to repeat at movie frame rate
my_mc.onEnterFrame = function() {
	speed = 10; // Speed of descent
	this._y += speed; // Apply motion
	if (this._y > Stage.height) { // Check if movieclip is offstage
		this._y = start_y; // If so, reset start position
	}
};

```

Adjust start\_x, speed, and my\_mc name as appropriate

---

<div class="post-metadata">

**Author:** ![AgentOrange](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@AgentOrange](https://forum.kirupa.com/u/AgentOrange)\
**Post date:** [July 14, 2008, 8:45am UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/9 "2008-07-14T08:45:01Z")

</div>

Thanks Glos. I’ll check that code out. You’re right with regards to changing the way I approach code, I’ve always been a designer and the realisation that (with FLASH at least) I have to learn how to write script has left me floundering a little. Thanks for the advice. While I have your indulgence, could I ask if you could recommend any sites (other than this one of course) that could help me learn AS?

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [July 14, 2008, 12:45pm UTC](https://forum.kirupa.com/t/repeat-movieclip-action-in-as/265471/10 "2008-07-14T12:45:20Z")

</div>

Best place to start are the help files that come with Flash, particularly the Tutorials and Samples that are provided.

Next best place would be to start here [http://www.kirupa.com/forum/showthread.php?t=190941](http://www.kirupa.com/forum/showthread.php?t=190941) with the four sites listed and the Useful Flash ActionScript Tutorials and Resources link.

There are also a ton of books on the subject, with many focusing specifically on actionscript rather than how to draw stuff in Flash…check out the reference section of your library.
