Forums → Programming Forum → AS3 Need help - Access
4 | 4953 |
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
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.
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
Try using the keyword "this", it targets automatically the instance the code is running for.
Gabo thanks for your help, I finaly got the script working, please lock this thread.
You must be logged in to post a reply!