-
Do while loops
Hey, I am trying to have a movie clip shoot where the mouse position is when they click a button, heres my script, I havent done any action script in a long time so I can't really see whats wrong.
code: on (release) {
shootx = _xmouse;
shooty = _ymouse;
CowboyMC.play();
duplicateMovieClip(bullet,bullet01,1);
bullet01._x = CowboyMC._x;
bullet01._y = CowboyMC._y;
do {
bullet01._x = bullet01._x - 5;
bullet01._y = bullet01._y + shooty * 0.2;
} while(bullet01._x < shootx && bullet01._y < shooty)
}
-
Senior Member
The problem is that you can't produce animation by doing a series of discrete movements inside a loop. This is because the statements in the script execute between frame-drawing. All those movements will happen very quickly and then when the next frame is drawn, the bullet will already be at the target.
If you want script to execute for the next few frames, you need a way to say "execute this script in the future". You can use an onEnterFrame handler for this, or setInterval.
Here's an example:
code:
on (release) {
shootx = _xmouse;
shooty = _ymouse;
CowboyMC.play();
duplicateMovieClip(bullet,bullet01,1);
bullet01._x = CowboyMC._x;
bullet01._y = CowboyMC._y;
bullet01.onEnterFrame = function()
{
this._x = bullet01._x - 5;
this._y = bullet01._y + shooty * 0.2;
if (this._x >= shootx || this._y >= shooty)
{
// we're there, delete this eventhandler so we stop moving
delete this.onEnterFrame;
}
};
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|