Jump to content

bullet switch sprite


mrdotb
 Share

Recommended Posts

Hello,

I want to switch sprite or frame when i fire. I use this for the moment but it's only working when i load the game.

It's possible to switch sprite or frame in createMultiple(quantity, key, frame, exists) ?

APP.bullets = game.add.group();    APP.bullets.createMultiple(10, random_card());    APP.bullets.setAll('anchor.x', 0.5);    APP.bullets.setAll('anchor.y', 1);    APP.bullets.enableBody = true;    game.physics.enable(APP.bullets);function random_card() {    var bullet    var rand = Math.floor(Math.random() * 3 + 1);    switch (rand) {        case 1:            bullet = "bullet1";            return bullet;            break;        case 2:            bullet = "bullet2";            return bullet;            break;        case 3:            bullet = "bullet3";            return bullet;            break;        case 1:            bullet = "bullet4";            return bullet;            break;        default:            return "error!";    }}
Link to comment
Share on other sites

Can you describe what the different sprites are and what you mean by switch? You probably want the different frames of the bullet to be on a single spritesheet. Next you'll need to create a function tied to input to play the spritesheet animation. See this example: when the arrow keys are pressed, an animation is played: http://examples.phaser.io/_site/view_full.html?d=games&f=starstruck.js&t=starstruck

if (cursors.left.isDown)// ...  player.animations.play('left'); 
Link to comment
Share on other sites

createMultiple creates many sprites with the same properties.  So when you run  this you get 10 bullets with the same key.

You can do that :

for (var i=0;i<10;i++) {    App.bullets.create(x,y,random_card());}

this will create 10 bullets with different keys.

Keep in mind that Group.create  makes the value sprite.visible=true instead of sprite.visible=false. 

Link to comment
Share on other sites

Thank you for you're help. Finally i use this code:

//create the group APP.bullets = game.add.group();    bulletTabs = [//Then i add 2 bullets with the same sprite in case i fire more than 4 bullets        APP.bullets.create(0, 0, "bullet1"),        APP.bullets.create(0, 0, "bullet1"),        APP.bullets.create(0, 0, "bullet2"),        APP.bullets.create(0, 0, "bullet2"),        APP.bullets.create(0, 0, "bullet3"),        APP.bullets.create(0, 0, "bullet3"),        APP.bullets.create(0, 0, "bullet4"),        APP.bullets.create(0, 0, "bullet4")    ];//i kill them    for (i = 0; i < bulletTabs.length; i++) {        bulletTabs[i].kill();    }APP.bullets.enableBody = true;    game.physics.enable(APP.bullets);// And when i grab the bullet i usevar currentBullet = APP.bullets.getRandom(0, 8);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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