Making Subclass Objects Non-Null - actionscript-3

I'm currently having some trouble with referencing objects that turn out null on export. Basically I want the Document Class to run the code of another class that is the class of a MovieClip without having Error 1009 popping up everywhere in my output panel.
Here's the subclass I'm trying to run:
package
{
import flash.display.*;
import flash.events.*;
import Document;
import RestartButton;
public class McMain extends MovieClip
{
public var document:Document;
public var leftKeyDown:Boolean = false;
public var rightKeyDown:Boolean = false;
public var upKeyDown:Boolean = false;
public var downKeyDown:Boolean = false;
public var onGround:Boolean = true;
public var xSpeed:Number = 0;
public var ySpeed:Number = 0;
public var mainSpeed:Number = 3.75;
public var frictionPower:Number = 0.9;
public var jumpPower:Number = 13;
public var gravityPower:Number = 0.5;
public var terminalVelocity:Number = 75;
public function McMain()
{
this.addEventListener(Event.ADDED_TO_STAGE, initMcMain);
// constructor code
}
public function initMcMain(event:Event)
{
addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
addEventListener(Event.ENTER_FRAME, hitTest);
addEventListener(Event.ENTER_FRAME, Main);
}
public function Main(event:Event):void
{
this.moveCharacter();
this.dynamicMovement();
}
public function checkKeysDown(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
this.leftKeyDown = true;
}
if (event.keyCode == 38)
{
this.upKeyDown = true;
}
if (event.keyCode == 39)
{
this.rightKeyDown = true;
}
if (event.keyCode == 40)
{
this.downKeyDown = true;
}
}
public function checkKeysUp(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
this.leftKeyDown = false;
}
if (event.keyCode == 38)
{
this.upKeyDown = false;
}
if (event.keyCode == 39)
{
this.rightKeyDown = false;
}
if (event.keyCode == 40)
{
this.downKeyDown = false;
}
}
public function moveCharacter():void
{
if (this.leftKeyDown)
{
this.xSpeed -= this.mainSpeed;
if (this.onGround)
{
this.scaleX = -1;
}
}
if (this.rightKeyDown)
{
this.xSpeed += this.mainSpeed;
if (this.onGround)
{
this.scaleX = 1;
}
}
if (this.leftKeyDown && this.onGround || this.rightKeyDown && this.onGround)
{
this.gotoAndStop(2);
}
if (this.currentFrame == 2)
{
if (! this.leftKeyDown && ! this.rightKeyDown)
{
this.gotoAndStop(3);
}
}
if (this.currentFrame == 3)
{
if (this.skidAnimation.currentFrame == this.skidAnimation.totalFrames)
{
this.gotoAndStop(1);
}
}
if (this.upKeyDown)
{
this.ySpeed -= this.jumpPower;
}
if (this.upKeyDown && this.leftKeyDown)
{
this.ySpeed -= 0;
this.xSpeed -= 10;
}
if (this.upKeyDown && this.rightKeyDown)
{
this.ySpeed -= 0;
this.xSpeed += 10;
}
if (this.xSpeed > 3 && ! onGround || this.xSpeed < -3 && ! onGround)
{
if (this.currentFrame == 2)
{
this.gotoAndStop(5);
}
}
if (this.ySpeed < -0.5 && ! onGround)
{
this.gotoAndStop(4);
}
else if (this.ySpeed > 0.5 && ! onGround)
{
this.gotoAndStop(5);
}
if (this.currentFrame == 5 && onGround)
{
this.gotoAndStop(1);
}
//if (! leftKeyDown && ! rightKeyDown && ! upKeyDown)
//{
//mcMain.gotoAndStop(1);
//}
}
public function dynamicMovement():void
{
if (onGround)
{
this.x += this.xSpeed;
this.xSpeed *= this.frictionPower;
}
else
{
this.x += this.xSpeed;
this.xSpeed *= (this.frictionPower + 0.09);
}
if (this.xSpeed > 7)
{
this.xSpeed = 7;
}
if (this.xSpeed < -7)
{
this.xSpeed = -7;
}
this.y += this.ySpeed;
this.ySpeed += this.gravityPower;
if (this.ySpeed > this.terminalVelocity)
{
this.ySpeed = this.terminalVelocity;
}
}
public function hitTest(event:Event)
{
//spawnArea.visible = false;
this.mcMainHitArea.visible = false;
this.document.levelChange.wallCollision.visible = false;
//^^^^^^^^^^^^^^^^^^^^^^^^^^
//Error 1009 appearing here!
this.document.levelChange.deathArea.visible = false;
this.document.levelChange.goalArea.goalHitArea.visible = false;
while (document.levelChange.wallCollision.hitTestPoint(this.x, this.y, true))
{
this.y = this.y - 0.5;
this.ySpeed = 0;
}
while (document.levelChange.wallCollision.hitTestPoint(this.x, this.y - 24, true))
{
this.y = this.y + 0.5;
this.ySpeed = 0;
}
while (document.levelChange.wallCollision.hitTestPoint(this.x - 12, this.y - 11, true))
{
this.x = this.x + 0.5;
this.xSpeed = 0;
if (! onGround)
{
this.leftKeyDown = false;
}
}
while (document.levelChange.wallCollision.hitTestPoint(this.x + 12, this.y - 11, true))
{
this.x = this.x - 0.5;
this.xSpeed = 0;
if (! onGround)
{
this.rightKeyDown = false;
}
}
if (document.levelChange.wallCollision.hitTestPoint(this.x - 16, this.y - 11, true))
{
if (this.upKeyDown)
{
this.ySpeed -= 10;
this.xSpeed += 50;
}
}
if (document.levelChange.wallCollision.hitTestPoint(this.x + 16, this.y - 11, true))
{
if (this.upKeyDown)
{
this.ySpeed -= 10;
this.xSpeed -= 50;
}
}
if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y, true))
{
if (this.upKeyDown)
{
this.upKeyDown = false;
this.onGround = false;
}
}
if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1, true))
{
if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1.5, true))
{
this.upKeyDown = false;
}
if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 5, true))
{
//upKeyDown = false;
onGround = false;
}
}
if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1, true))
{
this.ySpeed = 0;
if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 5, true))
{
onGround = true;
}
}
if (document.levelChange.deathArea.hitTestPoint(this.x, this.y + 1, true))
{
this.x = this.x;
this.y = this.y;
}
if (this.hitTestObject(document.levelChange.goalArea.goalHitArea))
{
if (stage.contains(document.level_1))
{
this.removeChild(document.level_1);
//stage.removeEventListener(Event.ENTER_FRAME,hitTest);
}
if (stage.contains(document.spawnArea))
{
this.x = this.x;
this.y = this.y;
}
addChild(document.level_2);
document.level_2.x = -1425;
document.level_2.y = -2550;
}
}
}
}
Document Class:
package
{
import flash.events.*;
import flash.display.*;
import flash.geom.Point;
import McMain;
import RestartButton;
import Level_2;
public class Document extends MovieClip
{
public var levelNumber:int = 1;
public var levelChange:Object;
public var levelArray:Array = new Array();
public var collisionArray:Array = new Array();
public var deathAreaArray:Array = new Array();
public var goalAreaArray:Array = new Array();
public var goalHitAreaArray:Array = new Array();
public var mcMain:McMain;
public var restartButton:RestartButton;
public var level_2:Level_2;
public function Document()
{
addEventListener(Event.ADDED_TO_STAGE, init);
mcMain = new McMain();
mcMain.document = this;
restartButton = new RestartButton();
restartButton.document = this;
level_2 = new Level_2();
// constructor code
}
public function init(event:Event)
{
this.levelChange = this.level_1;
}
public function levelHandler(event:Event)
{
this.levelChange = this["level_" + levelNumber];
if (level_2.stage)
{
levelNumber = 2;
//trace(levelChange);
}
for (var i:int = numChildren - 1; i >= 0; i--)
{
var collisionChild:DisplayObject = getChildAt(i);
if (collisionChild.name == "wallCollision")
{
collisionArray.push(collisionChild);
}
var deathAreaChild:DisplayObject = getChildAt(i);
if (deathAreaChild.name == "deathArea")
{
deathAreaArray.push(deathAreaChild);
}
var goalAreaChild:DisplayObject = getChildAt(i);
if (goalAreaChild.name == "goalArea")
{
goalAreaArray.push(goalAreaChild);
}
}
for (var i_2:int = numChildren - 2; i >= 0; i--)
{
var goalHitAreaChild:DisplayObject = getChildAt(i_2);
if (goalHitAreaChild.name == "goalHitArea")
{
goalHitAreaArray.push(goalHitAreaChild);
}
}
}
public function vCamMovement(event:Event):void
{
/*for (var i:int = 0; i < this.numChildren - 1; i++)
{
this.getChildAt(i).x -= xSpeed;
//levelObjects.getChildAt(i).y -= ySpeed;
}*/
level_1.x += stage.stageWidth * 0.5 - mcMain.x;
level_1.y += stage.stageHeight * 0.5 - mcMain.y;
level_2.x += stage.stageWidth * 0.5 - mcMain.x;
level_2.y += stage.stageHeight * 0.5 - mcMain.y;
spawnArea.x += stage.stageWidth * 0.5 - mcMain.x;
spawnArea.y += stage.stageHeight * 0.5 - mcMain.y;
mcMain.x = stage.stageWidth * 0.5;
mcMain.y = stage.stageHeight * 0.5;
}
}
}
Edit: Fixed a small typo within the code, but the code is still not working.
Edit:
Here's the debugger output:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at McMain/hitTest()[C:\Users\*\Desktop\*\Flash\*\McMain.as:186] *= XXXXXXXXXXXX

Two things. 1st, I cannot see anywhere that you initialize level_1 object. Two, you should move this code out of your constructor object and into your init method.
mcMain = new McMain();
mcMain.document = this;
restartButton = new RestartButton();
restartButton.document = this;
The reason is simple. You're just creating mcMain, then setting its .document property to "this." Within mcMain, you're referencing mcMain.document.levelChange, but you do not define document.levelChange until later, in your init method. I'm not entirely convinced that this is absolutely your issue, but you really should be do doing error checking when you're accessing dynamic properties on a dynamic class. Try doing things like
if(document != null && document.levelChange != null)
before you attempt to explicitly reference those properties/objects as you are with your hitTest.

Related

Access of possibly undefined property alpha through a reference with static type Class

I'm not sure how to go about setting the transparency when the character's health goes down.
Error is on line 38
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
public class Character extends MovieClip
{
var velocity:Number;
var shootLimiter:Number;
var health:Number;
var maxHealth:Number;
public function Character()
{
velocity = 10;
shootLimiter = 0;
health = 100;
maxHealth = 100;
addEventListener("enterFrame", move);
x = 300
y = 150
}
function takeDamage(d)
{
health -= d;
if(health <= 0)
{
health = 0;
kill();
}
Game.healthMeter.bar.scaleX = health/maxHealth;
Character.alpha = health/100;
}
function kill()
{
var blood = new Blood();
stage.addChild(blood);
blood.x = this.x;
blood.y = this.y;
removeEventListener("enterFrame",move);
this.visible = false;
Game.gameOver();
}
function move(e:Event)
{
shootLimiter += 5;
if(Key.isDown(Keyboard.RIGHT))
{
if(this.x <= 560)
{
this.x = this.x + velocity;
}
}
if(Key.isDown(Keyboard.LEFT))
{
if(this.x >= 40)
{
this.x = this.x - velocity;
}
}
if(Key.isDown(Keyboard.UP))
{
if(this.y > 20)
{
this.y = this.y - velocity;
}
}
if(Key.isDown(Keyboard.DOWN))
{
if(this.y < 280)
{
this.y = this.y + velocity;
}
}
if(Key.isDown(Keyboard.SPACE) && shootLimiter > 8)
{
shootLimiter = 0;
var b = new Needles();
stage.addChild(b);
b.x = this.x+65;
b.y = this.y+45;
}
}
}
}
Character has no static property called alpha. You are referring to the instance of the class and therefore it should be this.alpha = health/100; instead of Character.alpha = health/100;

Error #2025 twice

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
private var Player1Child:Player1Actual = new Player1Actual();
private var Player2Child:Player2Actual = new Player2Actual();
private var GreenLightLeft:Projectile1 = new Projectile1();
private var GreenLightRight:Projectile2 = new Projectile2();
private var NeonChild:mcNeonPlayer = new mcNeonPlayer();
private var _keyDownStatus:Object = {};
private var defaultSpeed:int = 10;
private var Player1Lock:Boolean = false;
private var Player2Lock:Boolean = false;
private var Player1Left:Boolean = false;
private var Player2Left:Boolean = true;
private var greenLightLeft:Boolean = true;
private var Player1CD:int = 0;
private var Player1Ready:Boolean = true;
private var Player1Hit:Boolean = false;
private var Player1X:int;
private var Player1Y:int;
private var NeonX:int;
private var NeonY:int;
private var NeonCD:int = 0;
private var NeonDuration:int = 240;
private var NeonUse:Boolean = false;
public function supportForce()
{
this.addEventListener(Event.ENTER_FRAME, general);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
btnStart.addEventListener(MouseEvent.CLICK, clickStart);
btnAbout.addEventListener(MouseEvent.CLICK, clickAbout);
btnDust.addEventListener(MouseEvent.CLICK, clickDust);
}
private function general(event:Event)
{
Player1X = Player1Child.x;
Player1Y = Player1Child.y;
NeonX = NeonChild.x;
NeonY = NeonChild.y;
GreenLightLeft.x -= (defaultSpeed * 2);
GreenLightRight.x += (defaultSpeed * 2);
//status_text = NeonCD;
if (NeonDuration > 0 && NeonUse == true)
{
NeonDuration -= 1;
}
else if (NeonDuration == 0 && NeonUse == true)
{
NeonUse = false;
removeChild(NeonChild);
addChild(Player1Child);
Player1Child.x = NeonX;
Player1Child.y = NeonY;
}
if (NeonCD > 0)
{
NeonCD -= 1;
}
trace(NeonCD);
if (Player1Child.BladeInstance.hitTestObject(Player2Child) && Player1CD <= 0 && Player1Child.BladeInstance.alpha != 0)
{
if (Player1Left == true)
{
Player2Child.x -= defaultSpeed;
Player1Hit = true;
Player1CD = 8;
//trace("G");
}
if (Player1Left == false)
{
Player2Child.x += defaultSpeed;
Player1Hit = true;
Player1CD = 8;
//trace("G");
}
}
if (Player1Ready == false)
{
Player1CD -= 1;
}
if (Player1CD <= 0)
{
Player1Ready = true;
Player1CD = 0;
}
else
{
Player1Ready = false;
}
if (Player1Left == true)
{
if (Player1Child.x <= (0 + Player1Child.width))
{
Player1Child.x = (0 + Player1Child.width);
}
if (Player1Child.x >= 550)
{
Player1Child.x = 550;
}
if (Player1Child.y >= 400)
{
Player1Child.y = 400;
}
if (Player1Child.y <= (0 + Player1Child.height))
{
Player1Child.y = (0 + Player1Child.height);
}
}
else if (Player1Left == false)
{
if (Player1Child.x <= 0)
{
Player1Child.x = 0;
}
if (Player1Child.x >= (550 - Player1Child.width))
{
Player1Child.x = (550 - Player1Child.width);
}
if (Player1Child.y <= 0)
{
Player1Child.y = 0;
}
if (Player1Child.y >= (400 - Player1Child.height))
{
Player1Child.y = (400 - Player1Child.height);
}
}
if (Player2Child.x <= Player2Child.width)
{
Player2Child.x = Player2Child.width;
}
if (Player2Child.x >= 550)
{
Player2Child.x = 550;
}
if (Player2Child.y >= (400 - Player2Child.height))
{
Player2Child.y = 400 - Player2Child.height;
}
if (Player2Child.y <= 0)
{
Player2Child.y = 0;
}
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[37])
{
if (Player1Left == false)
{
Player1Left = true;
Player1Child.rotation += 180;
Player1Child.y += (Player1Child.height - 5);
Player1Child.x += Player1Child.width;
}
Player1Child.x -= defaultSpeed;
}
if (_keyDownStatus[39])
{
if (Player1Left == true)
{
Player1Left = false;
Player1Child.rotation += 180;
Player1Child.y -= (Player1Child.height - 5);
Player1Child.x -= Player1Child.width;
}
Player1Child.x += defaultSpeed;
}
if (_keyDownStatus[38])
{
Player1Child.y -= defaultSpeed;
}
if (_keyDownStatus[40])
{
Player1Child.y += defaultSpeed;
}
if (_keyDownStatus[96] && Player1Ready == true)
{
Player1Child.play();
Player1Ready = false;
}
if (_keyDownStatus[97] && NeonCD <= 0)
{
removeChild(Player1Child);
stage.addChild(NeonChild);
NeonChild.x = Player1X;
NeonChild.y = Player1Y;
NeonCD = 480;
NeonUse = true;
Player1Lock = false;
}
if (_keyDownStatus[65])
{
if (Player2Left == false)
{
Player2Left = true;
Player2Child.rotation -= 180;
Player2Child.y -= (Player2Child.height - 5);
Player2Child.x -= Player2Child.width;
}
Player2Child.x -= defaultSpeed;
}
if (_keyDownStatus[68])
{
if (Player2Left == true)
{
Player2Left = false;
Player2Child.rotation += 180;
Player2Child.y += (Player2Child.height - 5);
Player2Child.x += Player2Child.width;
}
Player2Child.x += defaultSpeed;
}
if (_keyDownStatus[87])
{
Player2Child.y -= defaultSpeed;
}
if (_keyDownStatus[83])
{
Player2Child.y += defaultSpeed;
}
if (_keyDownStatus[90])
{
if (Player2Left == true)
{
var GreenLightLeft:Projectile1 = new Projectile1();
stage.addChild(GreenLightLeft);
GreenLightLeft.x = Player2Child.x;
GreenLightLeft.y = Player2Child.y;
greenLightLeft = true;
}
if (Player2Left == false)
{
var GreenLightRight:Projectile2 = new Projectile2();
stage.addChild(GreenLightRight);
GreenLightRight.x = (Player2Child.x);
GreenLightRight.y = (Player2Child.y - Player2Child.height);
greenLightLeft = false;
}
}
}
private function clickStart(event:MouseEvent):void
{
gotoAndStop(2);
}
private function clickAbout(event:MouseEvent):void
{
}
private function clickDust(event:MouseEvent):void
{
}
}
}
It works perfectly when I press Numpad 1 (97) but the second time, it gives this error.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at supportForce/keyCheck()
So I planned of removeing the NeonChild after 10 seconds or 240 frames but it gives this error.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at supportForce/general()
If you cannot understand what I am trying to say, this is the link to the actual .fla and .as
files
if you dont know parent of displayObject, which you want to remove you can write
mc.parent.removeChilte(mc);
where mc is movieclip , which you want to delete
Use this instead
this.parent.removeChild(mc);
where mc is the MovieClip.

Bullet fires perfectly in three directions, but not left

I'm quite new with actionscript, and have been scrapping together a little test game to sort of get used to the language, get my feet wet. The premise behind the game is pretty average, I have a character that can move in 8 directions, can pick up several keys that are randomly placed, and open a door when he possesses a key.
The problem comes in to play with the bullet shooting. I don't know much about trigonometry, so when a key is pressed, i just use the .rotate property to turn the player. I pass the .rotate integer into my bullet class and use that to tell which direction the bullet should travel. It works perfectly for the up, down, and right movements, but when the character is facing left the bullet is created but has no velocity whatsoever, and for the life of me I cannot figure out where the error in the code is.
If someone could look over my code and help me out, I would be much appreciated. I'm sure it's something simple that I'm just missing. I know it's a bit sloppy, so if there's any other tips you want to pass on to a novice please feel free!!
Thank you so much guys.
Main Class
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.text.TextField;
public class Main extends MovieClip
{
var player:Player;
var inventory:Inventory;
var invArray:Array = new Array();
var key:_Key;
var vx:int;
var maxSpeed:int;
var key_Up:Boolean;
var key_Down:Boolean;
var key_Left:Boolean;
var key_Right:Boolean;
var maxKey:int;
var keyCount:TextField;
var keysUp:int;
var door:Door;
var doorOpen:Boolean;
var wall1:Wall;
var wall2:Wall;
var wallCollide:Boolean;
var bulletTime:int;
var bulletLimit:int;
var bulletShoot:Boolean;
static var playerRotation:int;
public function Main()
{
init();
}
public function init():void
{
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
initPlayer();
initVariables();
initInventory();
initItems();
}
public function gameLoop(e:Event):void
{
movePlayer();
collisionDetection();
bullet();
}
public function keyDownHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
key_Left = true;
break;
case 38 :
key_Up = true;
break;
case 39 :
key_Right = true;
break;
case 40 :
key_Down = true;
break;
case 32:
if(bulletShoot)
{
bulletShoot = false;
var newBullet:Bullet = new Bullet(player.rotation);
newBullet.x = player.x;
newBullet.y = player.y;
addChild(newBullet);
}
}
}
public function keyUpHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
key_Left = false;
break;
case 38 :
key_Up = false;
break;
case 39 :
key_Right = false;
break;
case 40 :
key_Down = false;
break;
}
}
public function movePlayer():void
{
if (key_Left && !key_Right)
{
player.x -= maxSpeed;
player.rotation = 270;
}
if (key_Right && !key_Left)
{
player.x += maxSpeed;
player.rotation = 90;
}
if (key_Up && !key_Down)
{
player.y -= maxSpeed;
player.rotation = 0;
}
if (key_Down && !key_Up)
{
player.y += maxSpeed;
player.rotation = 180;
}
/*if ( key_Left && key_Up && !key_Right && !key_Down )
{
player.rotation = 315;
}
if ( key_Right && key_Up && !key_Left && !key_Down )
{
player.rotation = 45;
}
if ( key_Left && key_Down && !key_Right && !key_Up )
{
player.rotation = 225;
}
if ( key_Right && key_Down && !key_Left && !key_Up )
{
player.rotation = 135;
}*/
}
public function initPlayer():void
{
player = new Player();
player.x = stage.stageWidth * .5;
player.y = stage.stageHeight * .5;
stage.addChild(player);
}
public function initVariables():void
{
vx = 0;
maxSpeed = 4;
key_Up = false;
key_Down = false;
key_Left = false;
key_Right = false;
maxKey = 3;
keysUp = 0;
doorOpen = false;
wallCollide = false;
bulletTime = 0;
bulletLimit = 12;
bulletShoot = true ;
}
public function collisionDetection():void
{
for (var i:int=0; i < invArray.length; i++)
{
key = invArray[i];
if (player.hitTestObject(key))
{
stage.removeChild(key);
invArray.splice(i, 1);
keysUp++;
//trace(keysUp);
i--;
keyCount.text = String(keysUp);
break;
}
}
if (player.hitTestPoint(door.x,door.y + 25,true) && (keysUp > 0) && ! doorOpen)
{
door.gotoAndStop(2);
keysUp--;
invArray.pop();
trace(keysUp);
keyCount.text = String(keysUp);
doorOpen = true;
}
if (player.hitTestObject(door) && (keysUp == 0) && ! doorOpen)
{
wallCollide = true;
}
if (player.hitTestObject(wall1))
{
wallCollide = true;
}
if (player.hitTestObject(wall2))
{
wallCollide = true;
}
if (wallCollide == true)
{
player.y += 4;
wallCollide = false;
}
}
public function initInventory():void
{
inventory = new Inventory();
inventory.x = stage.stageWidth * .15;
inventory.y = stage.stageHeight * .90;
stage.addChild(inventory);
keyCount = new TextField();
stage.addChild(keyCount);
keyCount.x = inventory.x - 8;
keyCount.y = inventory.y + 3;
keyCount.text = String(keysUp);
//keyCount.border = true;
keyCount.width = 20;
keyCount.height = 20;
}
public function initItems():void
{
while (invArray.length < maxKey)
{
key = new _Key ;
key.x = Math.random() * 550;
key.y = Math.random() * 300;
stage.addChild(key);
invArray.push(key);
}
door = new Door();
door.x = 250;
door.y = 25;
wall1 = new Wall();
stage.addChild(wall1);
wall1.x = door.x - 175;
wall1.y = door.y;
wall2 = new Wall();
stage.addChild(wall2);
wall2.x = door.x + 175;
wall2.y = door.y;
stage.addChild(door);
}
public function bullet():void
{
if (bulletTime < bulletLimit)
{
bulletTime++;
} else
{
bulletShoot = true;
bulletTime = 0;
}
}
}
}
Bullet Class
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip {
public var _root:Object;
public var speed:int = 10;
public var bulletRotation:int;
public function Bullet(pRotation:int) {
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, eFrame);
bulletRotation = pRotation;
}
private function beginClass(e:Event):void
{
_root = MovieClip(root);
}
private function eFrame(e:Event):void
{
if (bulletRotation == 0)
{
this.y -= speed;
}
else if (bulletRotation == 90)
{
this.x += speed;
}
else if(bulletRotation == 270)
{
this.x -= speed;
}
else if(bulletRotation == 180)
{
this.y += speed;
}
if(this.y < -1 * this.height)
{
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
if(this.x < -1 * this.width)
{
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
}
}
}
In Bullet class, change 270 to -90, at line 38:
else if(bulletRotation == -90)
{
this.x -= speed;
}

As3: removing programmatically added child on hit test gives error #1009

I have tried removing programatically added movieclips when they collide with a certain object. When they do, they vanish. But after they have vanished, I get a #1009 error.
The error points at line 95, which is
if (this.x > stage.stageWidth + 2 - (stage.stageWidth - (this.width /2)) && this.x < stage.stageWidth - (this.width /2))"
And sometimes at 59, which is
if (this.x >= stage.stageWidth - 20)"
Why does it keep trying to run line 95 (and sometimes) 59? I removed the eventlistener.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.*;
public class Worker extends MovieClip
{
private var _root:MovieClip;
private var actualRange:Number;
private var reachedGoal:Boolean = false;
private var b:uint;
private var destination:Number;
private var goRight:Boolean = true;
private var goNowhere:Boolean = true;
private var yDir:Number;
private var yNumber:Number;
public function Worker()
{
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, loop);
//defining _root as the document root
actualRange =(Math.floor(Math.random() * (520 - 80 + 1)) + 80);
b = setInterval(startInc, 3000);
}
private function beginClass(event:Event):void{
//defining _root as the document root
_root = MovieClip(root);
}
private function loop(event:Event):void
{
if (this.hitTestObject(_root.killGun))
{
{
removeEventListener(Event.ENTER_FRAME, loop);
_root.removeChild(this);
}
}
if (reachedGoal == true)
{
if (goNowhere == true)
{
gotoAndStop(1);
}
//Boundaries
if (this.x <= 20)
{
goRight = true;
goNowhere = false;
}
if (this.x >= stage.stageWidth - 20)
{
goRight = false;
goNowhere = false;
}
if (this.y >= stage.stageHeight - 20)
{
yDir = 0
}
if (this.y <= 20)
{
yDir = 1
}
}
//Intro walk
if (reachedGoal == false)
{
if (this.x < actualRange)
{
gotoAndStop(4);
this.x += 3
}
if (this.x >= actualRange)
{
reachedGoal = true;
gotoAndStop(1);
}
}
//Main walk
if (this.x > stage.stageWidth + 2 - (stage.stageWidth - (this.width /2)) && this.x < stage.stageWidth - (this.width /2))
{
if (goNowhere == false)
{
if (goRight == true)
{
this.x += 1;
gotoAndStop(2);
//Y value
}
if (goRight == false)
{
if (this.x < stage.stageWidth - this.width /2)
this.x -= 1
gotoAndStop(3);
//Y value
if (yNumber == 1)
{
if(yDir == 0)
{
this.y -= 1;
}
if (yDir == 1)
{
this.y += 1;
}
}
}
}
}
}
//Timer functions
private function completePlay()
{
clearInterval(b);
}
private function startInc()
{
var moveNumber:Number = Math.floor(Math.random() * 20);
if (moveNumber == 1)
{
destination = Math.floor(Math.random() * 50);
yDir = Math.floor(Math.random() * 2);
yNumber = Math.floor(Math.random() * 4);
goRight = true;
goNowhere = false;
}
else if (moveNumber == 2)
{
destination = Math.floor(Math.random() * 50);
yDir = Math.floor(Math.random() * 2);
yNumber = Math.floor(Math.random() * 4);
goRight = false;
goNowhere = false;
gotoAndStop(3);
}
else
{
goNowhere = true;
}
}
//End
}
}
The error is happening because you're removing the eventListener within the eventListener's function and your removing the object from it's parent and still processing data that the object no longer contains or has been nullified. You should add a boolean to tell if the hitTest has occured and then not process any more of the loop. For instance:
private function loop(event:Event):void
{
var objectHit:Boolean = false;
objectHit = this.hitTestObject(_root.killGun);
if(!objectHit) {
if (reachedGoal == true)
{
if (goNowhere == true)
{
gotoAndStop(1);
}
//Boundaries
if (this.x <= 20)
{
goRight = true;
goNowhere = false;
}
if (this.x >= stage.stageWidth - 20)
{
goRight = false;
goNowhere = false;
}
if (this.y >= stage.stageHeight - 20)
{
yDir = 0
}
if (this.y <= 20)
{
yDir = 1
}
}
//Intro walk
if (reachedGoal == false)
{
if (this.x < actualRange)
{
gotoAndStop(4);
this.x += 3
}
if (this.x >= actualRange)
{
reachedGoal = true;
gotoAndStop(1);
}
}
}
if (objetHit)
{
removeEventListener(Event.ENTER_FRAME, loop);
_root.removeChild(this);
}
}
You should also remove the Event.ADDED listener once the object has been added.
private function beginClass(event:Event):void{
//defining _root as the document root
_root = MovieClip(root);
this.removeEventListener(Event.ADDED, beginClass);
}
Could it be that the rest of the loop is trying to run still?
Try breaking out of the loop after the hit:
if (this.hitTestObject(_root.killGun))
{
removeEventListener(Event.ENTER_FRAME, loop);
_root.removeChild(this);
return;
}

as3 flashdevelop freezes after build

Currently my game seems to work fine, the only problem it has is the builder does not respond 8 out of 10 times. The question is why does it stop working and how can I solve it.
Here is my code.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
/**
* ...
* #author BLANK
*/
public class Main extends Sprite
{
public var start:StartScherm = new StartScherm;
public var button:Startbutton = new Startbutton;
public var balletje:Ball = new Ball;
public var player1:Player = new Player;
public var player2:Player2 = new Player2;
public var player3:Player3 = new Player3;
public var player4:Player4 = new Player4;
public var background:Background = new Background;
public var kader:Kader = new Kader;
public var scoreboard:Monkeys = new Monkeys;
public var leven1:Life = new Life;
public var leven2:Life2 = new Life2;
public var leven3:Life3 = new Life3;
public var leven4:Life4 = new Life4;
public var wall1:Walls = new Walls;
public var wall2:Wall2 = new Wall2;
public var wall3:Wall3 = new Wall3;
public var wall4:Wall4 = new Wall4;
public static var KEY_LEFT1:Boolean = false;
public static var KEY_LEFT2:Boolean = false;
public static var KEY_LEFT3:Boolean = false;
public static var KEY_LEFT4:Boolean = false;
public static var KEY_RIGHT1:Boolean = false;
public static var KEY_RIGHT2:Boolean = false;
public static var KEY_RIGHT3:Boolean = false;
public static var KEY_RIGHT4:Boolean = false;
public var GAME:Boolean = false;
public var RESET:Boolean = false;
public var ballSpeedX:int = -3;
public var ballSpeedY:int = -2;
public var LIFE1:Number = 10;
public var LIFE2:Number = 10;
public var LIFE3:Number = 10;
public var LIFE4:Number = 10;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
addChild(start);
addChild(button);
}
private function gameStart():void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
if (stage) world();
else addEventListener(Event.ADDED_TO_STAGE, world);
}
private function gameReset():void
{
removeEventListener(Event.ADDED_TO_STAGE, world);
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function world(e:Event = null):void
{
addChild(background);
addChild(kader);
addChild(player1);
addChild(player2);
addChild(player3);
addChild(player4);
addChild(scoreboard);
addChild(leven1);
addChild(leven2);
addChild(leven3);
addChild(leven4);
addChild(balletje);
}
private function loop(e:Event):void
{
//balletje.update();
if (GAME == true)
{
removeEventListener(Event.ADDED_TO_STAGE, init);
removeChild(start);
removeChild(button);
gameStart();
}
if (RESET == true)
{
removeEventListener(Event.ADDED_TO_STAGE, world);
removeChild(background);
removeChild(kader);
removeChild(player1);
removeChild(player2);
removeChild(player3);
removeChild(player4);
removeChild(scoreboard);
removeChild(leven1);
removeChild(leven2);
removeChild(leven3);
removeChild(leven4);
removeChild(balletje);
removeChild(wall1);
removeChild(wall2);
removeChild(wall3);
removeChild(wall4);
LIFE1 = 3;
LIFE2 = 3;
LIFE3 = 3;
LIFE4 = 3;
gameReset();
}
if (LIFE1 == 0)
{
removeChild(leven1);
addChild(wall1);
}
if (LIFE1 == 1)
{
leven1.width = 10;
leven1.height = 10;
}
if (LIFE1 == 2)
{
leven1.width = 30;
leven1.height = 30;
}
if (LIFE2 == 0)
{
removeChild(leven2);
addChild(wall2);
}
if (LIFE2 == 1)
{
leven2.width = 10;
leven2.height = 10;
}
if (LIFE2 == 2)
{
leven2.width = 30;
leven2.height = 30;
}
if (LIFE3 == 0)
{
removeChild(leven3);
addChild(wall3);
}
if (LIFE3 == 1)
{
leven3.width = 10;
leven3.height = 10;
}
if (LIFE3 == 2)
{
leven3.width = 30;
leven3.height = 30;
}
if (LIFE4 == 0)
{
removeChild(leven4);
addChild(wall4);
}
if (LIFE4 == 1)
{
leven4.width = 10;
leven4.height = 10;
}
if (LIFE4 == 2)
{
leven4.width = 30;
leven4.height = 30;
}
balletje.x += ballSpeedX;
balletje.y += ballSpeedY;
if(balletje.x <= -350-balletje.width/2){ //check if the x position of the left side of the ball is less than or equal to the left side of the screen, which would be 0
balletje.x = -350-balletje.width/2; //then set the ball's x position to that point, in case it already moved off the screen
ballSpeedX *= -1; //and multiply the ball's x speed by -1, which will make it move right instead of left
LIFE2 -= 1;
} else if(balletje.x >= 350-balletje.width/2){ //check to see the right side of the ball is touching the right boundary, which would be 550
balletje.x = 350-balletje.width/2; //reposition it, just in case
ballSpeedX *= -1; //multiply the x speed by -1 (now moving left, not right)
LIFE4 -= 1;
}
//now we do the same with the top and bottom of the screen
if(balletje.y <= -350-balletje.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen
balletje.y = -350-balletje.height/2; //like we did before, set it to that y position...
ballSpeedY *= -1; //...and reverse its y speed so that it is now going down instead of up
LIFE3 -= 1;
} else if(balletje.y >= 350-balletje.height/2){ //if the bottom of the ball is lower than the bottom of the screen
balletje.y = 350-balletje.height/2; //reposition it
ballSpeedY *= -1; //and reverse its y speed so that it is moving up now
LIFE1 -= 1;
}
if( player1.hitTestObject(balletje) == true ){
if(ballSpeedX < 0){
ballSpeedY *= -1;
ballSpeedX = calculateBallAngle2(player1.x, balletje.x);
}
if(ballSpeedX > 0){
ballSpeedY *= 1;
ballSpeedX = calculateBallAngle2(player1.x, balletje.x);
}
} else if(player2.hitTestObject(balletje) == true ){
if(ballSpeedX > 0){
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(player2.y, balletje.y);
}
if(ballSpeedX < 0){
ballSpeedX *= 1;
ballSpeedY = calculateBallAngle(player2.y, balletje.y);
}
} else if(player3.hitTestObject(balletje) == true ){
if(ballSpeedX > 0){
ballSpeedY *= -1;
ballSpeedX = calculateBallAngle2(player3.x, balletje.x);
}
if(ballSpeedX < 0){
ballSpeedY *= 1;
ballSpeedX = calculateBallAngle2(player3.x, balletje.x);
}
}
else if(player4.hitTestObject(balletje) == true ){
if(ballSpeedX > 0){
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(player4.y, balletje.y);
}
if(ballSpeedX < 0){
ballSpeedX *= 1;
ballSpeedY = calculateBallAngle(player4.y, balletje.y);
}
}
if (KEY_LEFT1 == true)
{
player1.x -= 10;
}
if (KEY_LEFT2 == true)
{
player1.x += 10;
}
if (KEY_LEFT3 == true)
{
player3.x -= 10;
}
if (KEY_LEFT4 == true)
{
player3.x += 10;
}
if (KEY_RIGHT1 == true)
{
player2.y += 10;
}
if (KEY_RIGHT2 == true)
{
player2.y -= 10;
}
if (KEY_RIGHT3 == true)
{
player4.y += 10;
}
if (KEY_RIGHT4 == true)
{
player4.y -= 10;
}
}
private function keyDown(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
KEY_LEFT1 = true;
}
if (e.keyCode == 39)
{
KEY_LEFT2 = true;
}
if (e.keyCode == 67)
{
KEY_LEFT3 = true;
}
if (e.keyCode == 86)
{
KEY_LEFT4 = true;
}
if (e.keyCode == 65)
{
KEY_RIGHT1 = true;
}
if (e.keyCode == 81)
{
KEY_RIGHT2 = true;
}
if (e.keyCode == 102)
{
KEY_RIGHT3 = true;
}
if (e.keyCode == 105)
{
KEY_RIGHT4 = true;
}
if (e.keyCode == 13)
{
GAME = true;
}
if (e.keyCode == 8)
{
RESET = true;
}
}
private function keyUp(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
KEY_LEFT1 = false;
}
if (e.keyCode == 39)
{
KEY_LEFT2 = false;
}
if (e.keyCode == 67)
{
KEY_LEFT3 = false;
}
if (e.keyCode == 86)
{
KEY_LEFT4 = false;
}
if (e.keyCode == 65)
{
KEY_RIGHT1 = false;
}
if (e.keyCode == 81)
{
KEY_RIGHT2 = false;
}
if (e.keyCode == 102)
{
KEY_RIGHT3 = false;
}
if (e.keyCode == 105)
{
KEY_RIGHT4 = false;
}
if (e.keyCode == 13)
{
GAME = false;
}
if (e.keyCode == 8)
{
RESET = false;
}
}
public function calculateBallAngle(paddleY:Number, ballY:Number):Number
{
var ySpeed:Number = 1 * ( (ballY-paddleY) / 40 );
return ySpeed;
}
public function calculateBallAngle2(paddleX:Number, ballX:Number):Number
{
var xSpeed:Number = 1 * ( (ballX-paddleX) / 40 );
return xSpeed;
}
}
}
Thanks in advance.