1

Unfortunately I'm not a programmer, just an artist but I have to do a little game example as my homework to the university.

So, I made a class to my movieclip called Hero and I made the control system in Hero.as (from a tutorial). It's working well however the hero is spinning right round because I didn't set it up yet. Here it is a part of the Hero.as:

package AS {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;


public class Hero extends MovieClip {

    private var moveUp: Boolean;
    private var moveLeft: Boolean;
    private var moveDown: Boolean;
    private var moveRight: Boolean;
    private var moveSpeed: uint;

    public function Hero() {
        init();
    }

    protected function init() {
        moveUp = false;
        moveLeft = false;
        moveDown = false;
        moveRight = false;
        moveSpeed = 5;

        Constants.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedListener);
        Constants.stageRef.addEventListener(KeyboardEvent.KEY_UP, keyReleasedListener);
        Constants.stageRef.addEventListener(Event.ENTER_FRAME, frameListener);
    }

    //when you press down a key
    private function keyPressedListener(e: KeyboardEvent) {
        var key: uint = e.keyCode;
        if (key == 87 || key == 38) { //go to up with W or UP
            moveUp = true;
        } else if (key == 65 || key == 37) { //go to left with A or LEFT
            moveLeft = true;
        } else if (key == 83 || key == 40) { //go to down with S or DOWN
            moveDown = true;
        } else if (key == 68 || key == 39) { //go to right with D or RIGHT
            moveRight = true;
        }
    }

    //Blablabla

And this is how the Hero movieclip looks like in Game.fla. The problem is I don't know how to use gotoAndStop() in Hero.as. First of all I have to stop the hero's spinning - gotoAndStop("StandFront") - and then I have to set up in each section (in keyPressedListener) the right direction where the hero should walk. That's how I think about it with the wrong code and maybe on the wrong place:

        protected function init() {
        gotoAndStop("StandFront");   //------------Stop the character's spinning
        moveUp = false;
        moveLeft = false;
        moveDown = false;
        moveRight = false;
        moveSpeed = 5;

        //Blablabla...

    //when you press down a key
    private function keyPressedListener(e: KeyboardEvent) {
        var key: uint = e.keyCode;

        if (key == 87 || key == 38) { //go to up with W or UP
            moveUp = true;
            gotoAndStop("WalkFront"); //--------------- Play the animation where the
                                      //character's moving up while the player's
                                      //pressing the key W or UP.
        }
        //Blablablabla...

I don't think if it's useful but here it is the Main.as I attached to Game.fla document as a class:

package AS {
    import flash.display.MovieClip;

    public class Main extends MovieClip {

        private var hero:Hero;


        public function Main() {
            init();
        }

        public function init(){
            Constants.stageRef=stage;

            hero=new Hero();
            hero.x=300;   //512;
            hero.y=300;   //418.9;
            stage.addChild(hero);
        }
    }
}

Thank you so much for the help!

0 Answers0