Hi!
I’m trying to learn how to use classes in AS3 and I’ve run into a problem…
- I have a main class and another class for the bullets the character shoots.
- The bullets are supposed to move towards the crosshair when added to stage.
The crosshair is added to the stage directly, but even so I get this error that says “Access of undefined property aimMC”.
Oh, and the bullets are added to stage when I press SPACE.
public function Main() {
drawObjects();
private function drawObjects():void {
player.x = stage.stageWidth /2;
player.y = 380;
stage.addChild(player);
aimMC.x = stage.stageWidth /2;
aimMC.y = stage.stageHeight /2;
stage.addChild(aimMC);
}
Then, in the bullet class, I have:
public function AmmoMC() {
this.addEventListener(Event.ENTER_FRAME, moveAmmo, false, 0, true);
private function moveAmmo(evt:Event):void {
var x:Number = aimMC.x;
var y:Number = aimMC.y;
this.x += (x - this.x);
this.y += (y - this.y);
if (_xpos < 0 || _xpos > stage.stageWidth || _ypos < 0 || _ypos > stage.stageHeight) {
removeEventListener(Event.ENTER_FRAME, moveAmmo);
parent.removeChild(this);
}
}
What do you think is wrong?