Reputation: 536
Hey everyone so I am having some trouble displaying the player Vector lives in a Array added to the stage. When I first start my game the lives are added correctly and when an enemy hits the player they are removed correctly but when I added a store where you can purchase an extra life I am getting a bug to where the vector lives are added to the stage but they dont remove when an enemy hits the player and they just are there.
in the constructor I have nLives = 2;
addPlayerLivesToStage();
Here is the addPlayerLivesToStage();
function as well as the removePlayerLives();
private function addPlayerLivesToStage():void
{
var startPoint:Point = new Point((stage.stageWidth / 2) + 240, (stage.stageHeight / 2) - 180);
var xSpacing:Number = 30;
lives = new Vector.<Sprite>();
for (var i:int = 0; i < nLives; i++)
{
var playerLives:mcPlayerLives = new mcPlayerLives();
playerLives.x = startPoint.x + (xSpacing * i);
playerLives.y = startPoint.y;
addChild(playerLives);
lives.push(playerLives);
//aPlayerLivesArray.push(playerLives);
}
}
private function removePlayerLive():void
{
//Remove live from screen
removeChild(lives[lives.length - 1]);
lives[lives.length - 1] = null;
lives.splice(lives.length - 1, 1);
}
Now in my games ENTER.FRAME Loop I have the function purchaseExtraLifeFunc();
that controls the Purchase and is supposed to add 1 life to the stage.
Like So:
private function purchaseExtraLifeFunc():void
{
if (nCoins >= 5 && purchaseItem)
{
trace("purchase life");
//remove coins from cost of purchase
nCoins -= 5;
updatecoinsTextScore();
//add extra life
nLives += 1;
addPlayerLivesToStage();
}
else
{
purchaseItem = false;
}
}
as you can say I add 1 Live and call the addPlayerLivesToStage function.
THis is not working correctly and I'm all out of ideas if anyone could help me with this I would really aprreciate it
Upvotes: 0
Views: 33
Reputation: 10520
I can see a few logic problems that could be the cause.
It looks like your addPlayerLivesToStage
method ignores the fact that it could have been called before, and there could already be lives Sprites added to the stage. You then overwrite the lives Vector with a new Vector. So you don't have access to the previously added Sprites anymore.
I bet if you made the lives Sprites slightly transparent, you would see them stack over each other as you purchase more lives. Or to test, you could store a number that you increment every time you add a lives Sprite in your loop. And set the Y of the new sprite to this number. That way you would see all the Sprites you are adding.
To fix this, you want to remove all the lives Sprites at the beginning of your addPlayerLivesToStage
function, because you are recreating them later in that function anyway.
EDIT*
The problem is still that the Sprites are not being cleaned up between addPlayerLivesToStage calls.
It is easier if you first use a container Spirte to hold all you life sprites. So add a private property to your class:
private var livesContainer:Sprite;
Then in you constructor, just set it up:
livesContainer = new Sprite();
livesContainer.x = (stage.stageWidth / 2) + 240;
livesContainer.y = (stage.stageHeight / 2) - 180;
addChild(livesContainer);
The you can just add all the lives into this container.
I modified your addPlayerLivesToStage function to remove any left over life sprites at the end, and cleaned up the adding, so it does not add a life that is already added.
private function addPlayerLivesToStage():void
{
var xSpacing:Number = 30;
// Add new life sprites if we need to.
for (var i:int = lives.length; i < nLives; i++)
{
var playerLives:mcPlayerLives = new mcPlayerLives();
playerLives.x = xSpacing * i;
livesContainer.addChild(playerLives);
lives.push(playerLives);
}
// Remove any life Sprites that are left over from last time.
for (var j:int = lives.length - 1; j > nLives; j--)
{
var life:Sprite = lives[j]
livesContainer.removeChild(life);
lives.splice(j, 1);
}
}
Upvotes: 1