ForumsProgramming ForumAS3 Need help - Access

4 5053
Xinito
offline
Xinito
109 posts
Nomad

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 <= jumpSpeedLimit)
{
jumpSpeed *= 1 + jumpSpeedLimit / 50;
}
Hero.y += jumpSpeed;
if (Hero.y >= 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?

  • 4 Replies
gaboloth
offline
gaboloth
1,612 posts
Peasant

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.

Xinito
offline
Xinito
109 posts
Nomad

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

gaboloth
offline
gaboloth
1,612 posts
Peasant

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

Xinito
offline
Xinito
109 posts
Nomad

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

Showing 1-4 of 4