Jump to content

Access sprites in a group with index..?


BdR
 Share

Recommended Posts

I'm working on a game where the player controls a rotating vehicle which can collide with enemies and coins. I'm aware that the sprite body (aka "hit area") can only be a rectangle and it cannot be rotated. So instead, I've created 3 separate invisible sprites that only serve as hit detection areas. These hit areas rotate around the player's axis as the player rotates and this all works fine.
 
See image and github code below, it's just test code where the plane follows the mouse cursor and you can rotate it with the cursor keys:
 
wkrdb5.png
 
My question is this:
I'm wondering if there is a more efficient way to do this. Is there a way to access the sprites in a group like playerHitGroup.sprites[0] and playerHitGroup.sprites[1] etc.? That way I could just create the sprites in a for-loop and I wouldn't have to keep 3 separate vars for the sprites like the code is now:
var dum1;var dum2;var dum3;// setup the player hit spritesdum1 = game.add.sprite(0, 0, null, 0, playerHitGroup);dum1.body.setSize(32, 32);dum1.anchor.setTo(0.5, 0.5);dum2 = game.add.sprite(0, 0, null, 0, playerHitGroup);dum2.body.setSize(32, 32);dum2.anchor.setTo(0.5, 0.5);dum3 = game.add.sprite(0, 0, null, 0, playerHitGroup);dum3.body.setSize(32, 32);dum3.anchor.setTo(0.5, 0.5);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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