I am trying to remove a box that i have created on the screen.I have a box exported as
Box and a ship exported as player, they are both movie clips. this is the code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
//playermovevar
private var _player:MovieClip;
private var _playerSpeed:Number=5;
private var _destinationX:int;
private var _destinationY:int;
//boxaddvar
private var boxAmount:Number=0;
private var boxLimit:Number=20;
private var _root:Object;
public function Main() {
createPlayer();
//playermovlisten
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandlerdown);
//boxaddlisten
addEventListener(Event.ENTER_FRAME, eFrame);
_box.addEventListener(MouseEvent.CLICK, boxclick);
}
//playermoving
private function createPlayer():void {
_destinationX=stage.stageWidth/2;
_destinationY=stage.stageHeight/2;
_player = new Player();
_player.x=stage.stageWidth/2;
_player.y=stage.stageHeight/2;
stage.addChild(_player);
}
private function enterFrameHandler(event:Event):void {
_player.x += (_destinationX - _player.x) / _playerSpeed;
_player.y += (_destinationY - _player.y) / _playerSpeed;
}
private function mouseHandlerdown(event:MouseEvent):void {
_destinationX=event.stageX;
_destinationY=event.stageY;
addEventListener(MouseEvent.MOUSE_UP, mouseHandlerup);
rotatePlayer();
}
private function mouseHandlerup(event:MouseEvent):void {
}
private function rotatePlayer():void {
var radians:Number=Math.atan2(_destinationY-_player.y,_destinationX-_player.x);
var degrees:Number = radians / (Math.PI / 180) + 90;
_player.rotation=degrees;
}
//boxadding
private function eFrame(event:Event):void {
if (boxAmount<=boxLimit) {
boxAmount++;
var _box:Box=new Box ;
_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);
} else if (boxAmount >= boxLimit) {
removeEventListener(Event.ENTER_FRAME, eFrame);
} else {
addEventListener(Event.ENTER_FRAME, eFrame);
}
}
function boxclick(event:MouseEvent):void {
removeChild(_box);
}
}
It gives me this error:
1120: Access of undefined property _box. _box.addEventListener(MouseEvent.CLICK, boxclick);
1120: Access of undefined property _box. removeChild(_box);
anyone know whats wrong?
thanks
Not sure what results you’re wanting but try this and feel free to ask any questions.