Pages: 1
  Print  
Author Topic: How to prevent a collision to a player for 3 seconds after he has respawned.  (Read 2327 times)
Offline (Unknown gender) intygamer
Posted on: May 17, 2013, 12:30:45 PM

Member
Joined: Mar 2013
Posts: 77

View Profile Email
Hi all.

I am trying to make an asteroids type of game.  After the players ship gets killed and respawned to the center of the screen, there maybe be other asteroids in the area that kill him as soon as he respawns and continues till he loses all of his lives.  I would like to allow the player 3 seconds after he respawns to move out of that spot or kill a couple of asteroids to get out of the hot spot without being killed.

In the asteroid object in the event collision with player ship I have.
1.) set lives to -1
2.) Change instance ship into object explosion
3.) Create new instance of ship

I need to set up some decision loop to bypass those 3 actions for 3 seconds.  I know this event only gets processed on a collision with player ship, so I'm not sure how to do this.

Please help.  I have not found this answer on this forum or gamemaker forum.

Thanks
Joe
 
Logged
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: May 17, 2013, 01:45:27 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
Hi there! Here's a suggestion:

Move the collision from the asteroid object to the player ship object. In that event, instead of changing the object to explosion (which is terrible practice and was going to be removed from the engine), just create an explosion object at its position. Then reset x to xstart and y to ystart instead of creating the new ship.

In the ship's create event, set a variable, such as invincible, to 0. Then, wrap the collision with asteroid event you created in if (invincible <= 0) {}. Also in that if block, set invincible = room_speed * 3; to give you three seconds of invincibility. In the step event, decrement it:

Code: (EDL) [Select]
if (invincible > 0)
  invincible -= 1;

That should do it.

Alternatively, you can set an alarm whose job it is to unset invincible instead of decrementing it yourself. Instead of setting invincible, you would use invincible = true; alarm[0] = room_speed * 3;. Alarm0 would then reset invincible to false.
« Last Edit: May 17, 2013, 01:49:04 PM by Josh @ Dreamland » Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) polygone
Reply #2 Posted on: May 17, 2013, 02:51:21 PM

Contributor
Location: England
Joined: Mar 2009
Posts: 794

View Profile
Or you don't have to move it and just use other.invincible in the collision event
Logged
I honestly don't know wtf I'm talking about but hopefully I can muddle my way through.
Offline (Unknown gender) intygamer
Reply #3 Posted on: May 24, 2013, 09:21:11 PM

Member
Joined: Mar 2013
Posts: 77

View Profile Email
Thanks for the help from both of you, invincibility works.  I used the alarm clock method.  Just curious, why use (room_speed * 3) vs (90) steps = 3 seconds.

Thanks
Joe
Logged
Offline (Male) Josh @ Dreamland
Reply #4 Posted on: May 25, 2013, 10:27:54 AM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
In general, the only numbers that should appear in your code are 0 and 1. If you decide that you want your game to run at a framerate of 60 later on, instead of 30, you will not have to change that code to make it 3 seconds instead of 1.5, unlike if you had used 90. So basically, it's just good practice.

If I thought you'd ever reference the invincibility time elsewhere, I'd have told you to use room_speed * invincibility_time, and set invincibility_time to 3 in the Create event.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) Ideka
Reply #5 Posted on: May 27, 2013, 09:35:32 AM

Member
Joined: Apr 2011
Posts: 85

View Profile
More on that: http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants
Logged
Pages: 1
  Print