Hey I hope anyone can help me with my problem here. Been stuck for a week now.
I am trying to create a bullet platform shooting game like mario. But for some reason the bullet doesn’t wants to be removed from the scene and it keeps popping up this error message
I have 2 classes for this bullet effect the main and the bulletMC
3 symbol, the bulletMC, characterMC and the canon inside the characterMC
here is the code
Main.as
package
{
import flash.display.MovieClip;
import flash.text.engine.GraphicElement;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends MovieClip
{
//bullet var
private var bulletFire:Boolean = false;
var canShoot:Boolean = true;
var bulletTimer:Timer;
private var bullet:bulletMC;
//canon
var canonAngle:Number = 0;
var facingLeft:Boolean = true;
private var _bullets:Array;
public function Main ()
{
_bullets = new Array();
_bullets = [];
stage.addEventListener ("bulletCreated", BulletCreated);
stage.addEventListener (MouseEvent.MOUSE_MOVE, canonMove);
stage.addEventListener (Event.ENTER_FRAME, EnterFrame);
stage.addEventListener (MouseEvent.MOUSE_DOWN, bulletOut);
stage.addEventListener (MouseEvent.MOUSE_DOWN, bulletReload);
}
private function BulletCreated (event:Event)
{
//Add new bullet to the _bullets array
_bullets.push (MovieClip(event.target));
trace (event.target.name);
}
public function EnterFrame (e:Event)
{
bulletDisplay.text = "Bullets on the stage: " + String(_bullets.length);
for (var i:int = 0; i < _bullets.length; i++)
{
if (_bullets*.y + _bullets*.height / 2 < 0)
{
removeChild (_bullets*);
_bullets.splice (i, 1);
i--;
}
else if (_bullets*.y - _bullets*.height / 2 > stage.stageHeight)
{
removeChild (_bullets*);
_bullets.splice (i, 1);
i--;
}
else if (_bullets*.x + _bullets*.width / 2 < 0)
{
removeChild (_bullets*);
_bullets.splice (i, 1);
i--;
}
else if (_bullets*.x - _bullets*.width / 2 > stage.stageWidth)
{
removeChild (_bullets*);
_bullets.splice (i, 1);
i--;
}
}
}
//canon
public function canonMove (e:MouseEvent)
{
var mousex = - mouseX + charMC.x;
var mousey = - mouseY + charMC.y;
var canonRadian = Math.atan2(mousey,mousex);
canonAngle = canonRadian/(Math.PI/180);
checkCanon ();
}
private function checkCanon ():void
{
if (facingLeft)
{
if (canonAngle >= 45 && canonAngle < 90)
{
canonAngle = 45;
}
if (canonAngle <= -45 && canonAngle > -90)
{
canonAngle = -45;
}
if (canonAngle <= 45 && canonAngle >= -45)
{
charMC.canonMC.rotation = canonAngle;
charMC.canonMC.alpha = 1;
canShoot = true;
}
else if (canonAngle > 90 || canonAngle <= -90)
{
charMC.canonMC.rotation = 0;
charMC.canonMC.alpha = 0;
canShoot = false;
}
}
else
{
if (canonAngle >= -135 && canonAngle <= -90)
{
canonAngle = 135;
}
if (canonAngle <= 135 && canonAngle >= 90)
{
canonAngle = 135;
}
if (canonAngle >= 135 || canonAngle <= -135)
{
charMC.canonMC.rotation = canonAngle;
charMC.canonMC.alpha = 1;
canShoot = true;
}
else if (canonAngle < 90 && canonAngle > -90)
{
charMC.canonMC.rotation = 0;
charMC.canonMC.alpha = 0;
canShoot = false;
}
}
}
private function bulletOut (e:MouseEvent)
{
if (! bulletFire)
{
shootBullet ();
bulletFire = true;
}
}
private function bulletReload (e:MouseEvent)
{
bulletFire = false;
}
private function shootBullet ()
{
var bullet_SX:Number = Math.cos(canonAngle * Math.PI / 180) * -10;
var bullet_SY:Number = Math.sin(canonAngle * Math.PI / 180) * -10;
var bulletX:Number = charMC.x - 70 * Math.cos(canonAngle * Math.PI / 180);
var bulletY:Number = charMC.y - 70 * Math.sin(canonAngle * Math.PI / 180);
var bullet:bulletMC = new bulletMC(bullet_SX,bullet_SY,bulletX,bulletY);
parent.addChild (bullet);
}
}
}
bulletMC.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class bulletMC extends MovieClip
{
private var _sx:Number;
private var _sy:Number;
private var _ax:Number;
private var _ay:Number;
private var _angle:Number;
public function bulletMC (sx:Number,sy:Number,startX:Number ,startY:Number)
{
this._sx = sx;
this._sy = sy;
this.x = startX;
this.y = startY;
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage (e:Event)
{
addEventListener(Event.ENTER_FRAME, EnterFrame);
addEventListener(Event.REMOVED_FROM_STAGE, RemovedFromStage);
dispatchEvent(new Event("bulletCreated", true));
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function RemovedFromStage(event:Event):void
{
removeEventListener (Event.ENTER_FRAME, EnterFrame);
removeEventListener(Event.REMOVED_FROM_STAGE, RemovedFromStage)
}
private function EnterFrame(event:Event)
{
rotation += 20;
x += _sx;
y += _sy;
}
}
}
Just started recently in the as3 field. Hope anyone can help me with these!
I would be really happy if anyone can suggest any improvement to my code.
THANKS!!