Jump to content

Sprite Groups Question


Heppell08
 Share

Recommended Posts

I've been looking through the examples in phaser and have been coding pretty well with the use of the examples. 

Some of the code i've been doing has been stuff i already knew but i've noticed sprite groups and would like to ask a question.

 

In my game i have a heart system. The game creates a heart image (5 of them). Each heart represents 10 player health. The question is this.

I have declared 5 var heart so i can add them and kill them in my game in the order i need.

var heart1;var heart2;var heart3;var heart4;var heart5;// Later on i haveif(player.health =>40 && player.health <= 50){heart5.kill();}// so on and so forth until all hearts are removed

But i feel in a group i could order them to kill() specifically. I also have the hearts scaled up on their respective X,Y for the UI look im going for and the size of the game stage itself.

Might also be worth saying im using the same heart image.

I know there is a simpler way and a cheaper resource way of doing this so any information welcomed. 

 

Thanks

 

I'm absolutley loving phaser and the yield on code is amazing.

 

Thanks Rich for the tip on going to HTML5!

 

/***********EDIT************/

 

So i messed around and got a group working for the sprites of the health hearts but now what i want is to be able to kill() each specific as and when needed. I've tried the getFirstAlive but keep getting undefined errors etc. I think i'm writing it wrong because im trying to do:

 

heartgroup.hearts.getFirstAlive();

hearts.kill();

 

The group is called heartgroup and the images i want to kill are called hearts.

Ive got 5 of them as mentioned earlier.

 

Thanks

Link to comment
Share on other sites

 

I think you just need to swap your Group call around a bit:

var heart = heartgroup.getFirstAlive();heart.kill;

I think im doing something wrong here. Am i creating the group wrong? Code below. I want the heart to disappear after 10 health has been removed from the player. So after 20 health is removed from the player another 2nd heart is removed leaving the remaining health until all hearts depleted and player dies. I'm creating the heart sprites and group like so>

 heartgroup = game.add.group();    for(var i = 0; i <5; i++)    {     var hearts = game.add.sprite(500,500, 'heart');     hearts = game.add.sprite(550,500, 'heart');     hearts = game.add.sprite(600,500, 'heart');     hearts = game.add.sprite(650,500, 'heart');     hearts = game.add.sprite(450,500, 'heart');      heartgroup.add(hearts); // added this EDIT     }

then i'm trying to kill the hearts like Rich said above but with no result other than [Uncaught TypeError: Cannot read property 'kill' of null]

Heres the heart kill code in my update // Fixed this.

var hearts = heartgroup.getFirstAlive();    if(player.health >= 40 && player.health <= 49)    {        hearts.kill;    } 

Just doesn't remove the sprite image from the game now

// EDIT AGAIN

 

So a single sprite is removed but only after the player has lost ALL health then i get an error 

Uncaught TypeError: Cannot call method 'kill' of null

 

I dont know how I'm getting these errors now since it is removing a sprite from the game after full health removed as if it is a life but it is not. 

I know what the error is now but not sure how to fix it.

I have a loop of damage happening to my player (for testing this heart code). the loop takes 2 health each tick until dead. The firstalive is removed like it should be. The ones remaining afterwards are not removed and the loop continues around back to the first heart and give me error.

(It loops on health because i have it reset health at 0 health for testing this.)

So after the firstalive is essentially killed as it should be, how do i tell it to then proceed down the line and kill the remaining hearts left?

// EDIT

Sorry i've editted this numerous time while actually working with the code at the same time. Ive resolved this issue. Instead of hearts x5 sprite i named them heart1 to heart 5 in the group and killed them by calling each name within the group.

Link to comment
Share on other sites

So a single sprite is removed but only after the player has lost ALL health then i get an error 

Uncaught TypeError: Cannot call method 'kill' of null

 

This would be because when the player has no hearts left, the getFirstAlive returns null.

Link to comment
Share on other sites

That error was happening after the first heart was removed, hearts 2,3,4 and 5 were still awaiting the kill() but never did. Instead I got that error. I've now fixed it be referencing the 5 hearts in the group as heart1,heart2 etc etc and working like I need now. Almost posted about keeping the hearts onscreen when walking with the character but I just put the parent as the player and reset the XY so I'm relatively happy now. Thanks for the info though, I love how active HTML5 dev is! Makes it better :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...