Twizty Posted April 4, 2014 Share Posted April 4, 2014 I'm tryin to create my own flappy bird and got a problem: my bird (it's actually Darth Vader) doesn't collide with pipes which are changing their position with baground via tilePosition field. But if I change my bird's location, it collides with pipes. What do I do wrong? Here is my code var width = $(window).width(), height = 600;var game = new Phaser.Game(width, height, Phaser.AUTO,/*id of element which I want to use as game window */ '', { preload: preload, create: create, update: update });var platforms, cursors, player, texture, stars, score = 0, scoreText, timer = 0, spacebar, walls, bg, pipes1 = [], pipes2 = [];var valuesForPipes1 = [-50, -150, -250, -350, -450], valuesForPipes2 = [600, 500, 400, 300, 200];function collision() { alert("You lose!");}function preload() { game.load.image('sky', 'sky.jpg'); game.load.image('pipe1', 'pipe1.png'); game.load.image('darth', '0001.png'); game.load.image('htrad', '0.png'); game.load.image('star', 'star.png'); game.load.image('pipe2', 'pipe2.png');}function create() { game.physics.startSystem(Phaser.Physics.ARCADE); bg = game.add.tileSprite(0, 0, game.stage.bounds.width, height, 'sky'); walls = game.add.group(); walls.enableBody = true; player = game.add.sprite(300, height/2, 'darth'); game.physics.arcade.enable(player); player.body.bounce.y = 0.2; player.body.gravity.y = 250; player.body.collideWorldBounds = true; scoreText = game.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000' }); spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); spacebar.onDown.add(function() { player.body.velocity.y = -200; }, this) cursors = game.input.keyboard.createCursorKeys();}function update() { game.physics.arcade.collide(player, walls, function() { location.reload() }, null, this); timer++; bg.tilePosition.x -= 2; player.body.velocity.x = 0.01; for (var j = 0; j<pipes1.length; j++) { pipes1[j].x -= 2; pipes2[j].x -= 2; game.physics.arcade.collide(player, pipes1[j], collision, null, this); game.physics.arcade.collide(player, pipes2[j], collision, null, this); } if ((timer/200) % 1 == 0) { var i = Math.floor(Math.random() * valuesForPipes1.length); var pipe1 = walls.create(width, valuesForPipes1[i], 'pipe1'); var pipe2 = walls.create(width, valuesForPipes2[i], 'pipe2'); pipe1.body.immovable = true; pipe2.body.immovable = true; pipes1.push(pipe1); pipes2.push(pipe2); } if (cursors.left.isDown) { player.body.velocity.x = -150; player.loadTexture('htrad'); } else if (cursors.right.isDown) { player.body.velocity.x = 150; player.loadTexture('darth'); }}And one more quastion: in collusion callback function I can't use alert function. Why? Link to comment Share on other sites More sharing options...
Twizty Posted April 5, 2014 Author Share Posted April 5, 2014 Up. Sombody help me. Link to comment Share on other sites More sharing options...
JP91 Posted April 5, 2014 Share Posted April 5, 2014 phaser which version you are using? in phaser2 test using game.debug.body (sprite); Link to comment Share on other sites More sharing options...
Twizty Posted April 6, 2014 Author Share Posted April 6, 2014 I use phaser 2 Link to comment Share on other sites More sharing options...
JP91 Posted April 6, 2014 Share Posted April 6, 2014 I ask wrong, is clear that using these phaser2, I meant if it was the version 2.0.1 etc. tried it with game.debug.body(), are well positioned bodies. Link to comment Share on other sites More sharing options...
Twizty Posted April 6, 2014 Author Share Posted April 6, 2014 I use the latest version, they are well positioned but collision does not work how it should. It's dificult to understand everything at once. What else may be wrong? Link to comment Share on other sites More sharing options...
bmceldowney Posted April 6, 2014 Share Posted April 6, 2014 It seems like the problem is that you're manipulating the .x position of your pipes directly. I believe that will break collision detection. Try giving your pipes a low velocity value instead and see if that helps. Link to comment Share on other sites More sharing options...
JP91 Posted April 6, 2014 Share Posted April 6, 2014 I saw the problem in altering the position "x" of the group directly, that would increase the speed abruptly, but say that the body is properly positioned,try altering the position of the body. Link to comment Share on other sites More sharing options...
rich Posted April 8, 2014 Share Posted April 8, 2014 If you've still not solved it, have a look at the Phaser home page (http://phaser.io) - i've lost count of the number of flappy bird tutorials that are up there now! Link to comment Share on other sites More sharing options...
Recommended Posts