Collision Checks/Pipe Movement

John (Synthetic Pixel) S
A free video tutorial from John (Synthetic Pixel) S
Game Devloper, Game Designer, GameMaker User, Game Maker
3.8 instructor rating • 1 course • 758 students

Lecture description

In this lecture we add collision checks for our flappy bird for hitting the ground and the pipes. We will also add code to our pipes to have them move.

Learn more from the full course

Mobile Game Development With Gamemaker Studio

Learn what it takes to create your own Mobile Games using Gamemaker Studio. (Compatible with GMS2)

04:46:12 of on-demand video • Updated February 2020

  • Understand how to setup and use In App Purchases (IAP), in game ads, and leaderboards
  • Create Mobile Games with Gamemaker Studio
  • Understand how to create and code your own games using Gamemaker Studio's built in scripting language GML
English [Auto] Hey everyone and welcome to Section 2 lecture theory and in this video we'll be having a collision check to our bird for hitting the ground in the pipes and when the bird hits the ground or the pipes will be setting its life to false will have the backgrounds stop their movement and if the bird is in the air we'll have the speed set to zero and then the bird will fall down until it reaches the ground. And of course with the pipes we'll be adding movement and delete them when they're over there off screens. That way we don't have a bunch of objects crowding up our memory. All right. So let's go ahead and get into that ice. First let's go ahead and update. Flappy bird will just double click on them and then we'll click on step and open up the code action and then right here after we set the maximum speed 14 go ahead and add a space here. And then we'll say if y is greater than or equal to 574 which is where about where our ground is it's actually slightly above our ground. But if the y position is greater than or equal to 574 then we're going to set a life equal to false and then we're going to have the back ground. It's age speed horizontal speed for back ground zero set to zero. And then that ground speed for the next background also set to zero. So that way everything stops moving and then we're going to come right down here. We're applying our speed and say yes. Why is less than 574. Then continue applying our speed to the opposition. In other words if it's greater than or equal to 474 our bird is not going to move at all. It's falling further. It's going to stop on the ground. And that's exactly what we want. So you may be asking how do I know that 574 pixels is slightly above the ground. Well I'll go ahead and answer that right now first correct. That's area. And we'll just click on the top left checkmark that will close it out and save it. OK. So open up our room if we go to settings we can see that at the height of our room 768 that be right here right at the bottom now and that's where the ground begins. And we know that the ground is 168 in height as we can see here. So 768 minus 168 is 600. So the very edge there on the ground in the room is 600 pixels. So if we go up a little bit and of course we're counting down because we're taking away from the opposition. So 500 and 74 would be slightly above the ground. OK. Now before I forget to add those comments let's go back in here and comment this properly. So what's going on here is if Bird collides with Brown said live the false start that ground is simple enough. Now down here with the if we'll say if a bird is at ground I'm sorry if the bird is not at the ground then apply speed to because we're saying if y is less than 574 then we're applying y to the speed. I'm sorry we're applying speed to Y. So he got that comment here. Going in close that now we're going to anti-collision for that will click right here on how the event will select collision which is right below the step and will select 0 pipe for the collision. And there is our collision event will come over here to control click on that tab and drag over a code action. Now this is going to basically say that when we collide with the pipe the following code will execute OK. So if life is equal to true the speed equals zero life equals false and of course back around your score h speed her back ground zero equals zero and background turned the score h speed to background one equals zero as well and close out of it. So basically again that's saying that if we hit the pipe and the bird is alive then we're setting life to false or setting speed to zero and we're stopping the movement of the backgrounds. Well go ahead and Meisl comment here. Lyn bird lives with pipes set these variables go now all we have is one more step to complete here for this lecture. Well opening up old pipe. Click on Advent and for that we will do a step of that and then of course we'll hit the control tab drag over a code action and then we'll say gif. Oh flappy bird life is equal to true. An X minus sequel 2. Now again remember that everything starts in the top left corner of the room so that means if we're taking away from X we're moving to the left. If we're adding to X we're moving to the right. So now the next part is. Now this checks if it's outside of the room. So that way we don't. Again we don't have a bunch of objects cramming up our code so we want to check if the pipes outside of the room a certain distance then we destroy the pipe. So if x is less than negative 40 then instance destroy. Now you may notice that we're calling a variable from 0 flappy bird here. As we discussed in section 1 if you're calling a variable from another object you have to type in the name of the object dot life and identifies that we're calling the variable for life. In 05 burden checking if it's true. So that's our brief comment here. So move towards player at same speed around here live is Forgive my spelling here. You towards and destroy the object when for the pixels out side of the room. And I did not put a comma there. There we go now to comment. I will just hit the green checkmark in the top left and there we have it. Everything's ready to go for our test. So we'll go ahead and close out these two objects and we'll click run. So take a second to pop up here for us. Here again now as we see we can control the bird and when it hits the ground it stops and the background stops because the bird is now dead. It's no longer alive. So let's try one more thing here. That's a pipe to our rooms so that way we can at least see how that interaction is going to work for now. OK we'll add it right there. You may say we'll want to delete itself because it's outside of the room. And the answer is No. The check we placed is checking if it's negative x negative x would be on this side of the room on the left. So the pipes don't delude themselves until they're outside of the room on the left side. So that's all fine they're going close. So yes I know there's instances outside of the room. You'll get this warning. You can turn it off but there are one instances and zero hotels outside of the room. Should I remove them. No we don't want you to remove them. So go in and click run again and have to restart the game because of course he likes to come over here to my second screen. Here you go. Here comes the pipe. And notice I didn't go through that pipe again. Boom. So one more time this guy's as you can see there's clearly no collision there. So why am I not able to go through the pipe. Well the answer to that is in lecture for. Well we'll be discussing a particular topic that is causing this issue. Now I would love to see if any of you can figure out what the issue is before moving on to section forward. So if you figure out the answer please leave it in the comments. I would love to see all of your answers anyway. Until next time I will see you next lecture and have a good one.