Jump to content


Photo

How To Destroy Some Instances


  • Please log in to reply
16 replies to this topic

#1 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 07:13 PM

I'm making a top down bomberman-like game. When the player puts a bomb on the floor 3 seconds pass and it explodes, but how do I make it blow only the 2 rocks above,below, at the left and right of it and not all the rocks?

Thanks.
  • 0

#2 AB10784

AB10784

    GMC Member

  • New Member
  • 288 posts

Posted 07 May 2009 - 07:16 PM

use distance_to_object to check if the bomb is close enough to the bricks, then instance_destroy

Edited by AB10784, 07 May 2009 - 07:16 PM.

  • 0

#3 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 08:22 PM

Didn't work :S
  • 0

#4 AB10784

AB10784

    GMC Member

  • New Member
  • 288 posts

Posted 07 May 2009 - 08:31 PM

Can you please give an example of object names, the code you are using for each object as well?

Edit: spelling

Edited by AB10784, 07 May 2009 - 08:31 PM.

  • 0

#5 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 07 May 2009 - 08:45 PM

Run collision_line() in a while loop, so that it detects all rocks colliding horizontally and vertically two blocks.
while(collision_line(x-64,y,x+64,y,obj_rock,false,true)>=0)
	{
	with(collision_line(x-64,y,x+64,y,obj_rock,false,true)>=0)
		{
		instance_destroy();
		}
	}
Do the same thing for the vertical line, changing the arguments for collision_line() to match it.

collision_line(x1,y1,x2,y2,obj,prec,notme) This function tests whether there is a collision between the line segment from (x1,y1) to (x2,y2) and entities of object obj. This is a powerful function. You can e.g. use it to test whether an instance can see another instance by checking whether the line segment between them intersects a wall.


  • 0

#6 bbb0280

bbb0280

    GMC Member

  • GMC Member
  • 570 posts

Posted 07 May 2009 - 08:56 PM

make an explosion sprite, no an explosion object

then instance create, obj_explosion
\
if_place_meeting(obj_explosion) instance_destroy()
  • 0

#7 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 09:00 PM

I put this code:

while(collision_line(x-48,y,x+48,y,objRock,false,true)>=0)
	{
	with(collision_line(x-48,y,x+48,y,objRock,false,true)>=0)
		{
		instance_destroy();
		}
	}

and when I placed a bomb in-game it freezed.

Edited by Necdilzor, 07 May 2009 - 09:08 PM.

  • 0

#8 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 07 May 2009 - 09:09 PM

I put this code:

while(collision_line(x-48,y,x+48,y,objRock,false,true)>=0)
	{
	with(collision_line(x-48,y,x+48,y,objRock,false,true)>=0)
		{
		instance_destroy();
		}
	}

and when I placed a bomb in-game it freezed.

My mistake.
while(collision_line(x-48,y,x+48,y,objRock,false,true)>=0)
	{
	with(collision_line(x-48,y,x+48,y,objRock,false,true))
		{
		instance_destroy();
		}
	}
I'm an idiot :D. Had >=0 at the end of the with statement.

Edited by Snail Productions, 07 May 2009 - 09:10 PM.

  • 0

#9 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 09:19 PM

yes it works! but only horizontally, maybe something is missing?
  • 0

#10 AB10784

AB10784

    GMC Member

  • New Member
  • 288 posts

Posted 07 May 2009 - 09:22 PM

LOL infinate loop, nice on the first go Snail.. JK I did that on accident in one of my games and was like wtf why does it freeze!! Not to mention once i accidentally made an object increase its speed exponentially when I pressed an arrow key.. LOL


anyways try to duplicate the code then change the + to - and vice versa.?

EDIT: scratch that ^^ change the x's to y's leave the +/-

Edit2: man Im retarded..... Remove the + and - modifiers from the x and put them on y

Edit: @ Snail productions

Btw, if you're getting annoyed at me just hinting at the code instead of downright posting it, then ask.

^ LOL

Edited by AB10784, 07 May 2009 - 09:38 PM.

  • 0

#11 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 07 May 2009 - 09:23 PM

yes it works! but only horizontally, maybe something is missing?

Glad it works :D
I deliberately left out the vertical code because I didn't want to just hand it to you (more or less wanted to teach you how to do it). But it's pretty much the same thing; duplicate the code, and just alter the collision_line() arguments. Hope that helps.

Edit:
@AB10784:
Yeah, infinite loops are evil :huh:. Also you're right about the xy thing. Basically changing the endpoints of the lines.

Edited by Snail Productions, 07 May 2009 - 09:28 PM.

  • 0

#12 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 09:26 PM

what arguments do I have to alter? the X's and Y's?
  • 0

#13 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 07 May 2009 - 09:35 PM

what arguments do I have to alter? the X's and Y's?

Yes.

collision_line(x-48,y,x+48,y,objRock,false,true);

The first two arguments (x-48,y) are coordinates for the first endpoint. The second two arguments (x+48,y) are coordinates for the second endpoint. The line is horizontal, from 48 pixels left of x to 48 points to the right of x. In the vertical line, you want it from 48 points above y to 48 below it.

Btw, if you're getting annoyed at me just hinting at the code instead of downright posting it, then ask.
  • 0

#14 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 09:45 PM

I'm new to GML and I haven't found out yet how to do it vertically. Could you post the code please?

Edited by Necdilzor, 07 May 2009 - 09:50 PM.

  • 0

#15 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 07 May 2009 - 09:52 PM

I'm new to GML and I haven't found out yet how to do it vertically.

Full code here.
while(collision_line(x-48,y,x+48,y,objRock,false,true)>=0)
	{
	with(collision_line(x-48,y,x+48,y,objRock,false,true))
		{
		instance_destroy();
		}
	}
while(collision_line(x,y-48,x,y+48,objRock,false,true)>=0)
	{
	with(collision_line(x,y-48,x,y+48,objRock,false,true))
		{
		instance_destroy();
		}
	}
Explanation:
The vertical code is:
collision_line(x,y-48,x,y+48,objRock,false,true)>=0
The first coordinate here would be (x,y-48), 48 pixels above the object's position. (x,y+48) is the second coordinate, 48 pixels below. So the first instance (lowest instance id) that intersects that line is detected. If that is greater than or equal to 0 (it exists), then destroy it, and check for any other rocks intersecting the line again. Keep doing this until the collision_line() returns a negative number (meaning there are no more rocks in the line), and then stop the while loop.

Pretty complex for beginning GML. I suggest you look up the while statement in the manual for a better explanation.
Anyways, hope this helps, and good luck with your game :D
  • 0

#16 Necdilzor

Necdilzor

    GMC Member

  • GMC Member
  • 45 posts
  • Version:GM7

Posted 07 May 2009 - 10:15 PM

EDIT: Damn, I didn't write it right! It works now, thanks a lot for your help!

Edited by Necdilzor, 07 May 2009 - 10:17 PM.

  • 0

#17 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 07 May 2009 - 10:18 PM

EDIT: Damn, I didn't write it right! It works now, thanks a lot for your help!

No probs :D. Good luck!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users