javascript - game.physics.arcade.overlap() is not working in phaser -
i want kill bullet overlap asteroid not working. please have of code. looked in code many times looking right. want perform collision on asteroid.
game.js
var bullets; var game = { preload: function() { // load needed image this(play) game screen. //load menu screen this.load.image('menu', './assets/images/menu.png'); // here load needed resources level. // background image screen this.load.image('playgame', './assets/images/back.png'); // globe image screen // this.load.image('playgame', './assets/images/back.png'); this.load.image('spaceship', './assets/images/spaceship.png'); // astroid image screen this.load.image('astroid1', 'assets/images/asteroid1.png'); this.load.image('astroid2', 'assets/images/asteroid2.png'); this.load.image('astroid3', 'assets/images/asteroid3.png'); this.load.image('astroid4', 'assets/images/asteroid4.png'); //load bullet this.load.image('bullet', 'assets/images/bullet.png'); }, create: function() { // setting global variables in create function, initialise them on game start. // need them globally available update function can alter them. //this show physics of background game.physics.startsystem(phaser.physics.arcade); // scrolling starfield background gameback = game.add.tilesprite(0, 0, 800, 600, 'playgame'); textstyle_value = { font: "bold 20px segoe ui", fill: deftextcolor, align: "center" }; textstyleans = { font: "bold 22px 'comic sans ms', 'comic sans'", fill: anstextcolor, wordwrap: true, wordwrapwidth: 10, align: "center" }; textstyleques = { font: "bold 20px 'comic sans ms', 'comic sans'", fill: deftextcolor, wordwrap: true, wordwrapwidth: 10, align: "center" }; // loading questionbar image this.questionbar(); // csll fun. place astroid this.astroid(); // call fun. ans this.generateques(); this.generateans(); // call fun. ques this.comequs(); // call fun. ques this.comeans(); // loading diamond image this.diamond(); // start timer this.starttimer(); // set timer. this.settimer(); this.initloader(); bullets = game.add.group(); bullets.enablebody = true; bullets.physicsbodytype = phaser.physics.arcade; bullets.createmultiple(500, 'bullet', 150, false); // bullets.setall('anchor.x', 0.5); // bullets.setall('anchor.y', 0.5); bullets.setall('outofboundskill', true); bullets.setall('checkworldbounds', true); sprite = game.add.sprite(400, 530, 'spaceship'); sprite.anchor.set(0.4); // playing backgroud music fun_bckg = this.add.audio('fun_bckg', 1, true); fun_bckg.volume = 0.5; this.playfx('fun_bckg'); // bullet fire music fire_bullet = this.add.audio('fire_bullet', 1, true); fire_bullet.volume = 0.5; //this.playfx('fire_bullet'); }, astroid: function() { astroid1 = this.add.button(25, 100, 'astroid1', this.astroidclicked, this); astroid2 = this.add.button(250, 30, 'astroid2', this.astroidclicked, this); astroid3 = this.add.button(400, 40, 'astroid3', this.astroidclicked, this); astroid4 = this.add.button(570, 100, 'astroid4', this.astroidclicked, this); }, fire: function() { this.playfx('fire_bullet'); if (game.time.now > nextfire && bullets.countdead() > 0) { nextfire = game.time.now + firerate; var bullet = bullets.getfirstdead(); bullet.reset(sprite.x - 0, sprite.y - 140); game.physics.arcade.movetopointer(bullet, 300); } } } function collisionhandler(bullets, astroid1) { alert("hello"); // when bullet hits alien kill them both bullets.kill(); astroid1.kill(); } .
as far know, should put collision check update function this:
update: function() { // check collisions game.physics.arcade.collide(bullets, asteroid, this.bullethitasteroid, null, this); }
and kill sprites in function (in case bullethitasteroid function).
bullethitasteroid(_bullets, _asteroid): function() { _bullet.kill(); _asteroid.kill(); }
i hope helps.
Comments
Post a Comment