Copy link to clipboard
Copied
I have spawning enemies inside an MC coming from diffierent x and y locations on a timer. . I understand that for level 2 I need a separate function.How do I phrase the code so i don't get an error Right now I get the null object reference error with this timeline code.
var timer:Timer = new Timer(5000,0);
timer.addEventListener(TimerEvent.TIMER, createEnemy);
function createEnemy(e:TimerEvent):void{
//create a new enemy, pass the x and y you want (xPos and yPos here)
//var xPos:int, yPos:int
var enemy:Enemy = new Enemy( 840, 120);
var enemy2:Enemy = new Enemy( 3020, 100);
var enemy3:Enemy = new Enemy( 1800, -170);
back.collisions.lvl1.addChild(enemy);
back.collisions.lvl1.addChild(enemy2);
back.collisions.lvl1.addChild(enemy3);
enemyList.push(enemy);
enemyList.push(enemy2);
enemyList.push(enemy3);
var enemy4:Enemy = new Enemy( 840, 120);
var enemy5:Enemy = new Enemy( 3020, 100);
var enemy6:Enemy = new Enemy( 1800, -170);
back.collisions.lvl2.addChild(enemy4);
back.collisions.lvl2.addChild(enemy5);
back.collisions.lvl2.addChild(enemy6);
enemyList.push(enemy4);
enemyList.push(enemy5);
enemyList.push(enemy6);
}
timer.start();
So i need to brake this code into 2 functions. Can you properly show me how to do that? Thanks.
Copy link to clipboard
Copied
You need, repeat of code in "level 2" ?? Is it ?
Copy link to clipboard
Copied
show your Enemy class
Copy link to clipboard
Copied
Yes I need a repeat of the second half of the code for level 2 (lvl2). I also have this line code, but I don't think is relevant.
var enemyList:Array = new Array();
if (enemyList.length > 0)
{
for (var i:int = 0; i < enemyList.length; i++)
{
And the enemy class
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
private var xSpeedConst:int = 6;
public function Enemy(xLocation:int, yLocation:int) {
// constructor code
x = xLocation;
y = yLocation;
addEventListener(Event.ENTER_FRAME, loop);
}
public function loop(e:Event):void {
x -= xSpeedConst;
}
public function removeSelf():void {
trace("remove enemy");
removeEventListener(Event.ENTER_FRAME, loop);
this.parent.removeChild(this);
}
}
}
Maybe I don't need enemy 4,5 and 6 If I can make the first 3 show up on the second level from newly given coordinates.
Copy link to clipboard
Copied
To stop the "generator" or you can remove the event or simply stop counting example:
timer.removeEventListener (TimerEvent.TIMER, createEnemy);
or
timer.stop ();
Well, johnnien is a complicated issue to be worked by the forum by exchanging text. Usually when it comes to game development or even a simple application, we get that phrase that "every problem has more than one solution." So we ended up staying dead and what you are doing has to be done several ways, for example:
- Brand levels with array;
- Brand levels with different MovieClip.
If you want, feel - if avontade to send me the files in this email developer.henrique@hotmail.com so that I can take to analyze your question better.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now