Reputation: 836
So I have an array of fireballs, I was wondering how I would go about moving these items to create a gauntlet like game. I've got the array created and it's full of fireballs but I can't seem to get them to move. This is what my creation of the array looks like:
for (var i:Number = 0; i < fireballs; i++) {
var mcFireball :fireball = new fireball();
this.addChild(mcFireball);
mcFireball.x = Math.floor((Math.random() * location) + 100);
mcFireball.y = Math.floor((Math.random() * location) + 100);
mcFireball.scaleX = .5;
mcFireball.scaleY = .5;
array.push(mcFireball);
}
this is how I try to move them:
for (var i :Number = 0; i < fireballs; i++) {
if (array[i] == null) {
trace("Error here");
return;
}
trace(array[i]);
var mcFireball :fireball = array[i];
mcFireball.moveEnemy();
}
And this is what my moveEnemy() looks like:
public function moveEnemy():void
{
if ((this.x + this.width > this.stage.stageWidth) || (this.x - this.width <= 0))
_nEnemyMovementSpeed *= -1;
this.x += _nEnemyMovementSpeed;
}
I'm sure the error is within the scope of the function, but I'm not sure what I need to do to get these to work properly
My error is that moveEnemy() isn't a function
Upvotes: 0
Views: 165
Reputation: 15881
Ok I'm sleepy & this is off the top of my head (no Flash tested) but it should give you general idea.
mcFireball.moveEnemy();
was causing error because you're trying to reach it by saying it's a function within the mcFireball class. To understand better (example): You have a Game_Stages.as class file and each level is a function so to run level 5 you would say similar to what you had.. Game_Stages.Level5(); Now consider is mcFireBall a class file? does it have a moveEnemy function? See why Flash cries?
Possible Solution
for (var i :Number = 0; i < fireballs; i++)
{
if (array[i] == null)
{ trace("Error here"); return; }
trace(array[i]);
var mcFireball :fireball = array[i];
moveEnemy(mcFireball); //do moveEnemy func with mcFireball as input
}
Then you can do moveEnemy
like below. In this function we now reference the same input as "fball"
public function moveEnemy(fball:Sprite):void
{
if ((fball.x + fball.width > this.stage.stageWidth) || (fball.x - fball.width <= 0))
{ _nEnemyMovementSpeed *= -1; }
else
{fball.x += _nEnemyMovementSpeed; }
}
This assumes that mcFireball is a sprite (and should work whether its a library object or created by code)
Upvotes: 1