Pau Posted March 23, 2018 Share Posted March 23, 2018 Hello, I am making a kind of flappy bird. I want the pipes to move only horizontally. They have physics, so i can use the setVelocityX method. But they are falling down because of the gravity. I can change the position changing the x value of the pipe in the update method, but i would like to use the setVelocityX, it is more clear. var columna = this.physics.add.group(); ... columna.setVelocityX(-200); Thanks! Link to comment Share on other sites More sharing options...
PixelPicoSean Posted March 23, 2018 Share Posted March 23, 2018 var columna = this.physics.add.group({ gravityY: 0 }); s4m_ur4i 1 Link to comment Share on other sites More sharing options...
snowbillr Posted March 23, 2018 Share Posted March 23, 2018 I'm assuming you are using Arcade physics here, but please correct me if I'm wrong. There are a couple of ways to use gravity in arcade physics. You can either set it up in the game's config object so that it applies to every dynamic physics body, or, you can apply it to individual physics bodies. In a flappy bird like game, the only game entity that you want to have gravity is the bird itself. So if that's the case, I think it makes sense to set the gravity to 0 in the game's config object, and then individually set the gravity for the bird itself. Link to comment Share on other sites More sharing options...
samme Posted March 23, 2018 Share Posted March 23, 2018 You can also set allowGravity=false on all of those bodies. s4m_ur4i 1 Link to comment Share on other sites More sharing options...
onlycape Posted March 24, 2018 Share Posted March 24, 2018 Hi @Pau , Adding to what they have said above, in the collision of the bird with the pipe you will have to add a callback to adjust the velocityY and prevent the pipe from moving vertically. In my case, when there is a collision, I do not set the velocity.Y, I simply set the velocity.X of the pipes to 0 and let the collided pipe go up or down freely. Link to comment Share on other sites More sharing options...
Pau Posted March 24, 2018 Author Share Posted March 24, 2018 Sorry, these solutions are not working for me. I think it was my fault, because i posted so few lines of code. In the following code you can see what i have made trying to implement your proposals: function nuevaColumna() { var columna = this.physics.add.group({ gravityY: 0 }); var hueco = Math.floor(Math.random() * 5) + 1; for (var i = 0; i < 8; i++) { //El agujero son dos casillas, por eso ponemos hole +1 if (i != hueco && i != hueco + 1) { var cubo = columna.create(800, i * 60 + 10, 'pipe',{ gravityY: 0 }); cubo.allowGravity = false; } } columna.setVelocityX(-200); I have attached the whole "game" too, in order to prevent misunderstandings. Thank so much for your help. exercise.zip Link to comment Share on other sites More sharing options...
onlycape Posted March 24, 2018 Share Posted March 24, 2018 Hola @Pau , With this 2 changes the code works (just like @snowbillr said.): // In config variable: var config = { type: Phaser.AUTO, width: 800, height: 600, scene: { preload: preload, create: create }, physics: { default: 'arcade', arcade: { debug: true, gravity: { y: 0 } /* <<<<<<<<<< --- before 400, now by default gravity is 0 */ } } }; // In create function: function create() { var pajaro = this.physics.add.sprite(50, 100, 'pajaro'); pajaro.setGravity(0,400); /* <<<<<<<<<<< --- */ /*** More code here ***/ }; Tested: Regards. Link to comment Share on other sites More sharing options...
samme Posted March 24, 2018 Share Posted March 24, 2018 cubo.body.allowGravity = false; Link to comment Share on other sites More sharing options...
Pau Posted March 25, 2018 Author Share Posted March 25, 2018 Thank you so much, for the explanations I made it as @snowbillr and @onlycape said (sorry @snowbillr i didn't get it with your first explanation). I take note of the @samme way to make it woking, that is working too. Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts