Abhay Nayar
Abhay Nayar

Reputation: 87

How to move a Movie Clip by clicking a Button through coordinates

I am having a problem in AS2, I am making a game in flash in which by clicking a button you should be able to move a Movie Clip up. The problem here is that I know how to do that:

    on(press){
example._y-=10;
}

But I want to move it up gradually, as in coordinate by coordinate till it reaches ten, to give a bit of an animation. I also don't want any motion tween because the movie clip is already sharing codes with other things, so to that not to make it complex. I tried out loops but it didn't work pretty well, here is the code:

on (press) {

    var i = 1;
    while (i < 10)
    {
        _root.example._y-=1;
        i++;
    }
}

I am actually not good at loops and I just took it off the internet. So maybe you can help me out with a correction in the loop code, or you can help me by getting up with any other technique, but whatever, it must not be anything related to motion tweening, it should only be changing variables of a specific movie clip.

If you want the file I am creating, do reply, Thanks! :)

Upvotes: 0

Views: 1094

Answers (1)

Strille
Strille

Reputation: 5781

If you change the _y property repeatedly in a while loop you will not see the change as an animation (flash will run the code and update the screen when the script has finished, so it will jump to the end position immediately).

Instead, you can change the value on each new frame until the new position has been reached:

on (press) {
    var moveCount = 10;

    _root.example.onEnterFrame = function() {
        moveCount--;

        if (moveCount > 0) {
            this._y--;
        } else {
            delete this.onEnterFrame;
        }
    }
}

Also, it is highly recommended that you don't have a lot of code in the on(press) handler but create a new function and call that instead. Makes it easier to reuse and maintain the code.

Upvotes: 1

Related Questions