Search the Community
Showing results for tags 'sprites collision'.
-
Hello everyone! I found Phaser a few days ago, and am having a blast so far with it. I know a small amount of JavaScript, a little jQuery (basic select and change type operations), some CSS, and HTML4, so this is also a good learning experience for me (coming from a C#/SQL background). I took the platform game tutorial and changed it to be 2D, and split it up into multiple files based on the tutorial over on the ToastedWare site (http://toastedware.com/?p=258), turned off gravity, and made it similar to an old-school "Final Fantasy" or "Zelda" game. Everything loads: the level, player, and enemy are there, animations are working fine, and I can control the player with the keyboard. But try as I might, I haven't figured out how to get the collision to work, and the examples given on http://gametest.mobi haven't helped at all (it's the same collisionHandler function being used in each example, which isn't working for me...and I'm not sure why). I'm hesitant about plopping loads of code here into the forums (I don't want to spam, or be rude), but here is my update() function inside the main game loop (the inline javascript in index.html): function update() { level.update(); player.update(); enemy.update(); game.physics.collide(player, enemy, collisionHandler, null, this); }Which is followed by another function "collisionHandler()":function collisionHandler() { console.log(' Colission detected! '); }And here is the update() function in my player.js file: update: function() { // Player movement and animation change if (game.input.keyboard.isDown(KEYS.LEFT)) { this.sprite.x -= SPEED; this.sprite.animations.play('left'); } else if (game.input.keyboard.isDown(KEYS.RIGHT)) { this.sprite.x += SPEED; this.sprite.animations.play('right'); } else if (game.input.keyboard.isDown(KEYS.UP)) { this.sprite.y -= SPEED; this.sprite.animations.play('up'); } else if (game.input.keyboard.isDown(KEYS.DOWN)) { this.sprite.y += SPEED; this.sprite.animations.play('down'); } else { this.sprite.rotation = 0; } game.camera.follow(this.sprite); } No matter what I try, collision doesn't seem to be working. And another question, if you don't mind - the "game.physics.collide", shouldn't it be in an IF statement, saying that "IF a collision happens, call the collisionHandler function"? It just seems like it's going to fire off no matter what. I'm a bit confused about it, because the Sprite vs Sprite example has no "IF" statement, it just calls this once the sprites collide. Thanks for any help that you can give ;-)