fanheader Posted July 31, 2015 Share Posted July 31, 2015 Can anyone help me figure out why the physics.arcade.overlap() isn't working in this code? thanks in advance --------------------------------------var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('player', 'images/hero.png'); game.load.image('bullet', 'images/fireball.gif'); game.load.image('grapeStaff', 'images/grapeStaff.png'); game.load.image('healthBack', 'images/healthBack.png'); game.load.image('healthFront', 'images/healthFront.png'); game.load.image('manaBack', 'images/manaBack.png'); game.load.image('manaFront', 'images/manaFront.png'); game.load.image('goblin', 'images/monster.png'); } var sprite;var bullets; var goblinDraw = 0;var mana = 100;var health = 100;var healthTotal = 100;var fireRate = 100;var nextFire = 0;var timer = 0; function create() {goblinSpawn(); // turn on phaser arcade physics game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = '#313131'; //bullets--- bullets = game.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(50, 'bullet', 0, false); bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.y', 0.5); bullets.setAll('checkWorldBounds', true); bullets.setAll('outOfBoundsKill', true); //create sprites--- manaBack = game.add.sprite(10,60, 'manaBack'); manaFront = game.add.sprite(15,65, 'manaFront'); healthBack = game.add.sprite(10,10, 'healthBack'); healthFront = game.add.sprite(15,15, 'healthFront'); sprite = game.add.sprite(400, 300, 'player'); grapeSprite = game.add.sprite(400, 300, 'grapeStaff'); grapeSprite.anchor.set(0.5); game.physics.enable(grapeSprite, Phaser.Physics.ARCADE);cursors = game.input.keyboard.createCursorKeys(); grapeSprite.body.allowRotation = false; } //Handle keysdown function for movement--------------var keysDown = {}; addEventListener("keydown", function (e) {keysDown[e.keyCode] = true;}, false); addEventListener("keyup", function (e) {delete keysDown[e.keyCode];}, false);//------------------------------------------------------------------------------------------------------------------------------------------- function update() {game.physics.arcade.overlap(bullets, goblinSprite, bulletHitEnemy, null, this); //game event timertimer += 1;if (timer > 30){timer = 0;}//extra mana every tickif (timer == 0){if (mana < 100){mana += 1;}} //healthBarhealthFront.width = (health * 2);//ManaBarmanaFront.width = (mana * 2);//Weapon location --TODO--change grapeSprite to a changable staff objectgrapeSprite.body.x = (sprite.x - 40);grapeSprite.body.y = (sprite.y + 10); grapeSprite.rotation = game.physics.arcade.angleToPointer(grapeSprite); //Shooting on LeftMouse click if (mana > 1){ game.input.onDown.add(fire, this); } //------------------ //keyboard movementif (87 in keysDown) { // upsprite.y -= 2;}if (83 in keysDown) { // downsprite.y += 2;}if (65 in keysDown) { // leftsprite.x -= 2;}if (68 in keysDown) { // rightsprite.x += 2;}//keep player in screenBoundsif(sprite.y > 560) {sprite.y = 560;}if(sprite.x > 760) {sprite.x = 760;}if(sprite.y < 0) {sprite.y = 0;}if (sprite.x < 0) {sprite.x = 0;} //goblin movement//TODO //goblin vs playerSprite collisionif (sprite.x <= (goblinSprite.x + 32)&& goblinSprite.x <= (sprite.x + 32)&& sprite.y <= (goblinSprite.y + 32)&& goblinSprite.y <= (sprite.y + 32)) {console.log("got one");health -= 1; } }//--------------------------function bulletHitEnemy (goblinSprite, bullet) {console.log('COLLLISION!!!!!!!!!!!!!'); bullet.kill(); goblinSprite.kill(); } function fire() {mana -= 1; if (game.time.now > nextFire && bullets.countDead() > 0) { nextFire = game.time.now + fireRate; var bullet = bullets.getFirstDead(); bullet.reset(sprite.x + 5, sprite.y + 5); game.physics.arcade.moveToPointer(bullet, 500); }}//---------------------------- function goblinSpawn() {//spawn goblin x(25-775) y(150-600)goblinSprite = game.add.sprite(((Math.random()* 750) + 25), ((Math.random()* 400) + 150), 'goblin'); }//------------------------- function collisionHandler() { console.log(' Colission detected! '); } function render() { game.debug.text('Health: ' + health + ' / ' + healthTotal, 32, 32); game.debug.text('Mana: ' + mana + ' / 100 ', 32, 84); game.debug.spriteInfo(sprite, 32, 450); } Link to comment Share on other sites More sharing options...
CodeToWin Posted July 31, 2015 Share Posted July 31, 2015 what do you mean by isn't working? Link to comment Share on other sites More sharing options...
fanheader Posted August 1, 2015 Author Share Posted August 1, 2015 the bulletHitEnemy() function isn't firing off when the bullet hits a goblin Link to comment Share on other sites More sharing options...
CodeToWin Posted August 1, 2015 Share Posted August 1, 2015 do all sprites have enableBody set to true? Link to comment Share on other sites More sharing options...
Recommended Posts