Forums

ForumsProgramming Forum

AS3 Need help - Access

Posted Oct 16, '11 at 1:05pm

Xinito

Xinito

108 posts

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

    public class Hero extends MovieClip
    {
        var leftKeyDown:Boolean = false;
        var upKeyDown:Boolean = false;
        var rightKeyDown:Boolean = false;
        var downKeyDown:Boolean = false;
        var mainSpeed:Number = 7;
        var mainJumping:Boolean = false;
        var jumpSpeedLimit:int = 15;
        var jumpSpeed:Number = jumpSpeedLimit;

        public function Hero()
        {
            addEventListener(Event.ENTER_FRAME, moveChar);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
        }
        public function moveChar(event:Event):void
        {
            if (leftKeyDown)
            {
                Hero.x -=  mainSpeed;
            }
            if (rightKeyDown)
            {
                Hero.x +=  mainSpeed;
            }
            if (upKeyDown || mainJumping)
            {
                mainJump();
            }
        }
        public function checkKeysDown(event:KeyboardEvent):void
        {
            if (event.keyCode == 37 || event.keyCode == 65)
            {
                leftKeyDown = true;
            }
            if (event.keyCode == 38 || event.keyCode == 87)
            {
                upKeyDown = true;
            }
            if (event.keyCode == 39 || event.keyCode == 68)
            {
                rightKeyDown = true;
            }
            if (event.keyCode == 40 || event.keyCode == 83)
            {
                downKeyDown = true;
            }
        }
        public function checkKeysUp(event:KeyboardEvent):void
        {
            if (event.keyCode == 37 || event.keyCode == 65)
            {
                leftKeyDown = false;
            }
            if (event.keyCode == 38 || event.keyCode == 87)
            {
                upKeyDown = false;
            }
            if (event.keyCode == 39 || event.keyCode == 68)
            {
                rightKeyDown = false;
            }
            if (event.keyCode == 40 || event.keyCode == 83)
            {
                downKeyDown = false;
            }
        }
        public function mainJump():void
        {
            if (! mainJumping)
            {
                mainJumping = true;
                jumpSpeed = jumpSpeedLimit * -1;
                Hero.y +=  jumpSpeed;
            }
            else
            {
                if (jumpSpeed < 0)
                {
                    jumpSpeed *=  1 - jumpSpeedLimit / 75;
                    if (jumpSpeed > -jumpSpeedLimit/ 5)
                    {
                        jumpSpeed *=  -1;
                    }
                }
                if (jumpSpeed > 0 && jumpSpeed = stage.stageHeight - Hero.height)
                {
                    mainJumping = false;
                    Hero.y = stage.stageHeight - Hero.height;
                }
            }
        }
    }
}

And this is the error I keep getting on like 20 lines:

\\Hero.as, Line 28 1119: Access of possibly undefined property height through a reference with static type Class.

How can I resolve this?

 

Posted Oct 16, '11 at 2:00pm

gaboloth

gaboloth

1,372 posts

Do you get the same error in every line? However, the error might be that with "Hero" in lines like "Hero.x +=  mainSpeed" you're targeting the class, while you should target the instance. So try replacing all the "Hero"s with "this", except for the class name and the constructor function.

 

Posted Oct 16, '11 at 3:24pm

Xinito

Xinito

108 posts

I got your point, I made a new MovieClip and named it mainChar, changed the Hero to mainChar in my script and renamed the script to: Character. I get this error on like 30 lines: \\Character.as, Line 20 1120: Access of undefined property mainChar

 

Posted Oct 16, '11 at 3:27pm

gaboloth

gaboloth

1,372 posts

Try using the keyword "this", it targets automatically the instance the code is running for.

 

Posted Oct 16, '11 at 4:21pm

Xinito

Xinito

108 posts

Gabo thanks for your help, I finaly got the script working, please lock this thread.

 
Reply to AS3 Need help - Access

You must be logged in to post a reply!