singgih .D. ACE
singgih .D. ACE

Reputation: 23

AS3 MOVING OBJECT ON SPECIFIC FRAME

so i've been working on this game for a week now and i dont have any coding background at all so im trying to find tutorial here and there.. then i come up with this problem ...

so what i wanna do is move the object (CHARA) to the right when i hit frame 80 inside (CHARA,which is a nested movieClip with 99 frames btw ) then move it back to original position when i hit frame 99... the problem is anything i do doesn't make my object move at all (movieClip still played btw) what did i do wrong here? did i just put the code at the wrong position ?? (CHAR is moved only if i put the code x= directly inside frame 80 but i try using class here)

here is my code,sorry i know its messy its my first code i try my best here

package {

    public class Main extends MovieClip {

        public var CHARA:CHAR = new CHAR;//my main char
        public var rasen:Rasen_button = new Rasen_button;//the skill button
        public var NPCS:NPC = new NPC;// the npc

        public function Main() {
            var ally:Array = [265,296];//where me and my ally should be
            var jutsu:Array = [330,180];// where the buttons should be
            var enemy:Array = [450,294];//where the enemies should be

            addChild(NPCS);
            NPCS.x = enemy[0];
            NPCS.y = enemy[1];
            NPCS.scaleX *=  -1;

            addChild(rasen);
            rasen.x = jutsu[1];
            rasen.y = jutsu[0];

            addChild(CHARA);
            CHARA.x = ally[0];
            CHARA.y = ally[1];
            rasen.addEventListener(MouseEvent.CLICK, f2_MouseOverHandler);
            function f2_MouseOverHandler(event:MouseEvent):void {
                CHARA.gotoAndPlay(46); //here is the problem
                if (CHARA.frame == 80)
                {
                    CHARA.x = ally[1]; //just random possition for now
                }

            }
        }
    }
}

any suggestions?

Upvotes: 2

Views: 356

Answers (1)

Aaron Beall
Aaron Beall

Reputation: 52193

Your if statement is inside a click handler (f2_MouseOverHandler), so it only gets executed when a user clicks rasen, not necessarily when the playback reaches frame 80. This is a common beginner mistake related to timing and code execution. The most straight forward solution is to write some code that will execute every frame using an ENTER_FRAME handler:

        rasen.addEventListener(MouseEvent.CLICK, f2_MouseOverHandler);
        function f2_MouseOverHandler(event:MouseEvent):void {
            CHARA.gotoAndPlay(46); //here is the problem

            // add an ENTER_FRAME handler to check every frame
            CHARA.addEventListener(Event.ENTER_FRAME, chara_EnterFrameHandler)
        }
        function chara_EnterFrameHandler(event:Event):void {
            if (CHARA.currentFrame == 80)
            {
                CHARA.x = ally[1]; //just random possition for now

                // remove the ENTER_FRAME after the condition is met
                // so it stops executing each frame
                CHARA.removeEventListener(Event.ENTER_FRAME, chara_EnterFrameHandler);
            }
        }

Upvotes: 3

Related Questions