I am new to AS3 and OOP concepts. I have one class called AlienShips
I have a laser beam and I am trying to check collision of beam with all the alienships on my stage. But collision gives weird results.. it detects all the alienships and not just one specific ship. So when my beam hits one of the alienships it destroys all the rest of the ships as well.
Here is the code of my AlienShip
public class AlienShip extends MovieClip {
private var laserBeam:CannonBeam;
public function AlienShip(){
trace("create new alien ship");
addEventListener(Event.ADDED_TO_STAGE, enterFrameHandler );
}
private function enterFrameHandler(event : Event) : void {
removeEventListener(Event.ADDED_TO_STAGE, enterFrameHandler);
}
public function destroySelf():void{
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, enemyLoop);
}
public function hasCollided(otherObject:MovieClip):Boolean{
return this.hitTestObject(otherObject);
}
}
Here is my main document class on stage where I instantiate all my base class alien objects as follow
public class RaidersMain extends MovieClip {
private var alienOne:AlienShip;
private var alienTwo:AlienShip;
private var alienThree:AlienShip;
private var alienFour:AlienShip;
private var alienShipArray:Array = new Array();
public function RaidersMain(){
initGameElements();
}
public function initGameElements():void{
alienOne = new AlienShip();
stage.addChild(alienOne);
alienTwo = new AlienShip();
stage.addChild(alienTwo);
alienThree = new AlienShip();
stage.addChild(alienThree);
alienFour = new AlienShip();
stage.addChild(alienFour);
alienShipArray.push(alienOne);
alienShipArray.push(alienTwo);
alienShipArray.push(alienThree);
alienShipArray.push(alienFour);
cann = new CannonBeam();
stage.addChild(cann);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event : Event) : void {
checkForBeamAlienCollision();
}
private function checkForBeamAlienCollision() : void {
for(var i = 0; i < alienShipArray.length; i++){
var currentShip:AlienShip = alienShipArray[i];
if(currentShip.hasCollided(cann)){
currentShip.destroySelf();
alienShipArray.splice(i,1);
}
}
}
But in the for loop it destroys all the aliens and not just the specific ship that the beam collides with in the for loop. What am I doing wrong? How do I correct the design of the aliens structure?
Related
I'm building a game. When you die or win a level you're prompted to continue or return to the main menu. If you chose to go to the main menu you can start a new game. When you start a new game, the game is object is created again & it's children have lost their reference to the stage. I'm not sure why this is happening and I've spent over a week trying to figure out why. Here's some code (and descriptions of the code) from the game that should hopefully provide enough insight as to why the problem may be occurring:
if the "new game" button is clicked on the startMenu the NavigationEvent.START event is dispatched. a LevelEvent.COMPLETE event is dispatched by WeeBeeGame when the level is completed.
public class DocumentClass extends MovieClip {
public var startMenu:StartMenuGenerator = new StartMenuGenerator();
public var weeBeeGame:WeeBeeGame;
public var youWonBox:YouWonBox = new YouWonBox();
public function DocumentClass() {
// constructor code
addChild(startMenu);
startMenu.addEventListener(NavigationEvent.START, startGameHandler);
}
public function startGameHandler(e:NavigationEvent) : void {
this.removeChild(startMenuBG);
removeChild(startMenu);
weeBeeGame = new WeeBeeGame();
this.addChild(weeBeeGame);
weeBeeGame.addEventListener(LevelEvent.COMPLETE, levelCompleteHandler);
}
public function levelCompleteHandler(e:LevelEvent) : void {
youWonBox.x = this.stage.stageWidth/2;
youWonBox.y = this.stage.stageHeight/2;
addChild(youWonBox);
youWonBox.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
private function mouseClickHandler(e:MouseEvent) : void {
if(e.target.name === "mainmenubtn"){
mainmenuHandler();
}
}
private function continueHandler() : void {
youWonBox.removeEventListener(MouseEvent.CLICK, mouseClickHandler);
}
private function mainmenuHandler() : void {
youWonBox.removeEventListener(MouseEvent.CLICK, mouseClickHandler);
WeeBeeGame.collisionDOC.removeChildren();
removeChild(weeBeeGame);
weeBeeGame = null;
this.addChild(startMenuBG);
addChild(startMenu);
removeChild(youWonBox);
}
}
the code that dispatches a LevelEvent.COMPLETE event is not shown but it dispatches when the level is complete. collisionDOC needs to be static because it's needed in a lot of other classes and holds the display objects needed for a 3rd party pixel-level collision detection.
public class WeeBeeGame extends MovieClip {
public var bee: Bee;
public var beeHurt:BeeHurt;
public var spawningDaemon:SpawningDaemon;
public static var collisionDOC:DisplayObjectContainer;
public function WeeBeeGame() {
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
}
private function addedToStageHandler(e:Event) : void {
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
collisionDOC = new MovieClip();
addChild(collisionDOC);
bee = new Bee();
collisionDOC.addChild(bee);
beeHurt = new BeeHurt(bee.x, bee.y);
addChild(beeHurt);
beeHurt.visible = false;
spawningDaemon = new SpawningDaemon(currentLevel);
this.addEventListener(LevelEvent.COMPLETE, levelCompleteHandler, false, 0, true);
}
private function levelCompleteHandler(e:LevelEvent) : void {
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}
the first line to throw a 1009 error (Cannot access a property or method of a null object reference) is the line containing stage.mouseX because the stage reference is null.
public class Bee extends MovieClip {
public function Bee() {
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(e:Event) : void {
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
this.x = this.stage.stageWidth / 2;
this.y = this.stage.stageHeight - this.stage.stageHeight / 9;
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
}
private function enterFrameHandler(e:Event) : void {
if(stage == null){
trace('stage is null' + stage.mouseX);
}
}
}
When the swf is first opened and a new WeeBeeGame is created its children have references to the stage. But when WeeBeeGame and it's children are removed from the display list and memory they lose reference and even if they're re-instantiated their references are still lost. How do I fix this? I'm very confused. Thank you all!!
ENTER_FRAME handlers continue to execute even when the display object is removed form the stage. So when you removeChild(weeBeeGame) those ENTER_FRAME handlers are still trying to access stage.mouseX each frame. You need to stop the ENTER_FRAME handlers.
Probably the easiest fix is to add an Event.REMOVED_FROM_STAGE handler to remove the Event.ENTER_FRAME handlers.
A better fix IMO is to not add any ENTER_FRAME handlers from your game objects, but rather expose a public function like update() which gets called from a single ENTER_FRAME handler in your WeeBeeGame when the game is running. Then to stop the game completely you can stop all updates by simply removing that single ENTER_FRAME handler. This is a common practice.
I'm still really new about classes and stuffs. So, I tried making this and I got an error: Access of undefined property.
Why speedX and speedY var still error although I've defined it in public var in the main class?
Thanks!
EDITED: I've tried calling the variables from other class with main.speedX and main.speedY
But it got error : Cannot access a property or method of a null object reference.
at Ball/moveBall()
This is the Main code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
public var speedX:Number = 5;
public var speedY:Number = 5;
public var speedMax:Number = 10;
private var ball:MovieClip = new Ball();
private var paddle:MovieClip = new Paddle();
public function Main()
{
paddle.addEventListener(Event.ENTER_FRAME, movePaddle);
addChild(ball);
addChild(paddle);
}
}
}
This is the Ball Movie Clip Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Ball extends MovieClip
{ public var main:Main;
public function Ball()
{addEventListener(Event.ENTER_FRAME, moveBall);
main= new Main();
}
public function moveBall(e:Event):void
{
x += main.speedX;
y += main.speedY;
}
}
}
That's because your class Ball cannot access speedX and speedY inside the event callback. Why not add speedX and speedY to your Ball class directly instead ?
public class Ball extends MovieClip
{
public var speedX:Number;
public var speedY:Number;
public function Ball(sX:Number = 0, sY:Number = 0)
{
this.speedX = sX;
this.speedY = sY;
addEventListener(Event.ENTER_FRAME, moveBall);
}
public function moveBall(e:Event):void
{
x += speedX;
y += speedY;
}
}
Here's another possible solution where you would be passing main to ball to use the values of speed stored in Main.
public class Main extends MovieClip
{
public var speedX:Number = 5;
private var ball:MovieClip;
public function Main()
{
ball=new Ball(this);
addChild(ball);
}
}
and
public class Ball extends MovieClip
{
private var _main:Main;
public function Ball(main:Main)
{
_main=main;
addEventListener(Event.ENTER_FRAME, moveBall);
}
public function moveBall(e:Event):void
{
x += _main.speedX;
}
}
}
How can I add a children (instance) on stage every time I press the button without replacing the existing children?
I have four Classes: Symbol1, Symbol3, Symbol4, all.
When I Press Symbol3 which is a button I want to create an instance of Symbol1 on the stage through class all.as. With Symbol4 I want to delete one of the created instance in order of creation on stage.
Example: I have pressed Symbol3 three times and I have created three instances of Symbol1 on stage. Now if I press Symbol4 I will delete the first created instance. If I press Symbol4 one more time I will delete the second created instance.
public class Symbol3 extends SimpleButton
{
private var creator:all;
private var child:Symbol1 = new Symbol1 ;
private var child2:Symbol1 = new Symbol1 ;
private var child3:Symbol222 = new Symbol222 ;
public function Symbol3()
{
addEventListener(MouseEvent.CLICK, onCLICK);
}
private function onCLICK(s:MouseEvent)
{
creator = new all(child);
stage.addChild(creator);
}
}
.
public class all extends MovieClip
{
private var _thief1:MovieClip;
public function all(par1:MovieClip)
{
_thief1 = par1;
addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
}
private function onADDED_TO_STAGE(e:Event)
{
removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
this.addChild(_thief1);
_thief1.x = Math.random() * 200;
_thief1.y = Math.random() * 200;
}
}
.
public class Symbol4 extends SimpleButton
{
public function Symbol4()
{
addEventListener(MouseEvent.CLICK, onCLICK);
}
private function onCLICK(s:MouseEvent)
{
stage.removeChild(?);
}
}
This I have so far.
Thanks
You should put all your addable/removable sprite in one same container, let's call it container.
Then the add button will look like this:
private function onCLICK(s:MouseEvent)
{
container.addChild(new all(new Symbol1()));
}
And the remove button:
private function onCLICK(s:MouseEvent)
{
container.removeChildAt(0);
}
When removing the child on layer 0, the other children will go one layer down and the next child to remove will come on 0.
Thanks for the help Kodiak!
I made it finally. I am not sure if this is the right approach but at least it works.
I have three Classes:
AddChild2.as - linkage to Button
Creator.as
Ship2.as - linkage to MovieClip
The tricky moment was that the stage had to be transferred as a parameter to avoid error:1009. The other think is the empty constructor function of the Creator that makes the code more flexible and independent. Now Creator can produce any passed movieClip. Again I believe that there is another better way to do this, so any improvement is welcome.
public class AddChild2 extends SimpleButton
{
private var creatorche:Creator = new Creator;
private var s:Ship2;
public function AddChild2()
{
// constructor code
addEventListener(MouseEvent.CLICK, onCLICK)
}
private function onCLICK(e:MouseEvent)
{
s = new Ship2;
creatorche.onCreator(s, stage);
}
}
.
public class Creator extends MovieClip
{
private var ship:MovieClip;
public function Creator()
{
// constructor code
}
public function onCreator(par1:MovieClip, par2:Stage)
{
ship = par1;
par2.addChild(ship);
ship.x = Math.random() * 200;
ship.y = Math.random() * 200;
}
}
.
public class Ship2 extends MovieClip
{
public function Ship2()
{
// constructor code
}
}
So I've done a fair amount of searching through SO and couldn't quite find the answer to this question. I have a movieclip in my symbol library that's exported for actionscript, and I've written a custom class for it. It mostly works great except for when I try to access a custom private property after I've added the movieclip to the stage. Below's an example:
package {
public class MyMovieClip extends MovieClip {
private var _isEnabled:Boolean = false;
public function MyMovieClip():void {
trace(this);
}
public function set isEnabled( b:Boolean ):void {
_isEnabled = b;
}
public function get isEnabled():Boolean {
return _isEnabled;
}
}
}
And then I have another class where I am adding instances of the movieclip to the stage in a loop:
package {
public class MyOtherClass extends MovieClip {
public var myMC:MyMovieClip;
public var docClass:*;
public function MyOtherClass( docRef:* ):void { // passing in a reference to the DocumentClass so I can access the stage
docClass = docRef;
init();
}
public function init():void {
for(var i:int=0; i<6; i++) {
var myMC:MyMovieClip = new MyMovieClip; // instantiate the movieclip which is exported for actionscript and has a custom class
//set a few native properties
myMC.name = "myMC" + i; //setting the name so I can reference this movieclip after it's been added to stage
myMC.y = myMC.height * i + 20;
myMC.x = 20;
myMC.alpha = .7;
}
dispatchEvent(new Event(MyOtherClass.MOVIECLIPS_ADDED)); // just to be safe, let's dispatch a custom event when all movieclips have been added
}
public function traceEnabled():void {
trace(docClass.stage.getChildByName("myMC1").isEnabled); // this throws: 1119: Access of possibly undefined property isEnabled through a reference with static type flash.display:DisplayObject
}
}
}
And finally I instantiate MyOtherClass inside my document class:
package {
public class DocumentClass extends MovieClip {
public var myOtherClass:MyOtherClass;
public function DocumentClass():void {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(e:Event):void {
myOtherClass = new MyOtherClass(); // upon instantiation, init is called in MyOtherClass and all of my movieclips are added to the sage
}
}
}
What gives? Why can't I access the MyMovieClip property, isEnabled, after it's been added to the stage? Is there another way? (Thanks in advance for any help)
Internally all children of a DisplayObjectContainer are referenced as DisplayObject, so when you use getChildByName, it returns a DisplayObject.
In order to access your custom properties without causing a compile-time error, you would need to cast the result of getChildByName as the Class of your custom properties. See the code below.
That however isn't your only issue (though it's the reason for the error, once you correct you will get runtime errors as well).
In your creation loop, your not adding myMC to the display list, so calling stage.getChildByName() will return null because your clips aren't on the stage.
Your also not adding your myOtherClass to the display list in the posted code.
Also, storing a reference to the document class isn't really needed. Just add the addedToStage listener in MyOtherClass and have the handler be init.
HERE IS SOME UPDATED CODE
For your MyOtherClass:
public function MyOtherClass():void {
if(stage){
init(); //if stage is ready, call init, if not wait for the added to stage event
}else{
addEventListener(Event.ADDED_TO_STAGE,init);
}
}
public function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE,init);
for(var i:int=0; i<6; i++) {
var myMC:MyMovieClip = new MyMovieClip;
myMC.name = "myMC" + i; //setting the name so I can reference this movieclip after it's been added to stage
myMC.y = myMC.height * i + 20;
myMC.x = 20;
myMC.alpha = .7;
addChild(myMC); //!!!! add to the displayList
}
dispatchEvent(new Event(MyOtherClass.MOVIECLIPS_ADDED)); // just to be safe, let's dispatch a custom event when all movieclips have been added
}
public function traceEnabled():void {
var myMC:MyMovieClip = this.getChildByName("myMC1") as MyMovieClip; //!!! cast it as MyMovieClip so you have access to all the properties/methods in that class
if(myMC){ //myMC will be null if the cast failed
trace(myMC.isEnabled);
}
}
/*
getChildByName is slow and cumbersome. Most people generally only use it for accessing things put on the timeline in the Flash IDE. Using events is a much better way of accessing your items. If traceEnabled was caused by a mouse event attached to myMC, then this would be a much better implementation:
*/
public function betterTraceEnabled(e:Event):void {
var myMC:MyMovieClip = e.currentTarget as MyMovieClip;
if(myMC){
trace(myMC.isEnabled);
}
}
AND YOUR DOCUMENT CLASS:
public class DocumentClass extends MovieClip {
public var myOtherClass:MyOtherClass;
public function DocumentClass():void {
if(stage){
onAddedToStage(null); //most of the time stage is already populated in the constructor of your document class
}else{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
public function onAddedToStage(e:Event):void {
myOtherClass = new MyOtherClass(); // upon instantiation, init is called in MyOtherClass and all of my movieclips are added to the sage
addChild(myOtherClass); //add it to the displayList
}
}
I am trying to program a little game but stumbled upon some problems.
I have several classes. One of them is the wall class. Which when hitting the player should remove a heart from the health bar.
Hitting the player works pretty good but I cant remove the heart which is inside the UI class.
Main.as
public class TwinRunner extends MovieClip
{
private var _timer:Timer;
private var _userInterface:UI = new UI();
public function TwinRunner()
{
//Timer initialize
_timer = new Timer(800, 1);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onUpdateTime);
_timer.start();
stage.addChild(_userInterface);
mcButton.addEventListener(MouseEvent.CLICK, onButtonClick);
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
Here I am adding the UI class as an instance.
Wall.as
x += _vx;
//Remove Wall when visible Sword hits
if(MovieClip(parent).mcSword.isVisible)
{
if(this.hitTestObject(MovieClip(parent).mcSword))
{
trace("wall destroyed");
parent.removeChild(this);
}
}
//Remove Wall when Player hit
else if(this.hitTestObject(MovieClip(parent).mcPlayer))
{
trace("player hit");
parent.removeChild(this);
MovieClip(parent)._userInterface.heartMeter = false;
}
//When the Wall is outside of the screen it gets removed
if (x + width / 2 < 0)
{
parent.removeChild(this);
}
}
MovieClip(parent)._userInterface.heartMeter = false; <- This part gives me an error...
And last the UI.as
public class UI extends MovieClip
{
private var heart1:heartFill = new heartFill();
public function UI()
{
addChild(heart1);
heart1.x = 200;
heart1.y = 200;
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onEnterFrame(event:Event):void
{
}
public function set heartMeter(visibility:Boolean):void
{
heart1.visible = false;
}
}
I can access the setter function from the Main class but not from the wall.as class.
Is there a way to do so? I am still learning so I hope this is not such a stupid question :D
Thanks in advance!
Your problem is that you're removing the child (wall) from the parent, which then casuses the parent var to be null - see comments in code below:
parent.removeChild(this); //this will make parent null
MovieClip(parent)._userInterface.heartMeter = false; //parent is now null because this has been removed
Simply switching those two lines around should fix your main issue.
Also, _userInterface is declared as a private var in your main class, so if you want to access it in your wall class (like you are trying to do), you'll need to make it public or you'll get another error.
Something you may want to think about, is using events instead. That way, your code isn't directly referencing other classes like the UI. separation of class dependencies makes testing and troubleshooting easier.
So something like this in your wall class:
else if(this.hitTestObject(MovieClip(parent).mcPlayer))
{
trace("player hit");
stage.dispatchEvent(new Event(PLAYER_HIT)); //PLAYER_HIT would be a public static constant in this class: public static const PLAYER_HIT:String = "PlayerHit";
parent.removeChild(this); //this needs to be last because after this line, stage and parent will be null
}
Then in your UI Class:
private function onAddedToStage(event:Event):void
{
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Wall.PLAYER_HIT,playerHitHandler);
}
private function playerHitHandler(e:Event):void {
heartMeter = false;
}