Jump to content


Photo

Three Or More In A Row


  • Please log in to reply
15 replies to this topic

#1 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 01 February 2009 - 07:02 PM

Here's my problem. I have this three-in-a-row game im making, mostly to get better at GML. I have an object, obj_tile, that is supposed to check if there are more obj_tile around in rows. If there are three in a row, including itself, the objects will brake.

It works, but i want it to do the same to objects that are further in the row, it only takes out three even if there are four in the line.

I have this in the create event:
sprite_index=choose(spr_blue, spr_red)
if sprite_index=spr_blue{color="blue"}
if sprite_index=spr_red{color="red"}

alarm[0]=20


And I have this code in the step event:
var k1,k2;
k1 = instance_position(x+96,y,obj_tile)
k2 = instance_position(x+192,y,obj_tile)

if (k1 != noone && k2 != noone){
	if (k1.color=color && k2.color=color){
	 with (k1) instance_destroy();
	   with (k2) instance_destroy();
	   instance_destroy();
	}
}

My friend (Schreib, thank you) helped me with the step event script, and yes, i understand whats going on in it.
How would i go about to make all the objects in a row explode? This is both horizontally and vertically. NOT diagonally.

If i forgot to mention anything, please tell me.

Thanks in advance~
  • 0

#2 Sharkiedude

Sharkiedude

    GMC Member

  • New Member
  • 813 posts

Posted 01 February 2009 - 08:21 PM

Use the for loop, it's good for you
Create event:
[codebox]init = 96;[/codebox]
Step:
[codebox]var k1, i;//Untested D=
for (i=0; i<5; i+=1) //Replace i<5 to the amount of blocks in a whole row
{
init = i*init;
k1 = instance_position(x+init,y,obj_tile);
if (k1 != noone) and (k1.color=color)
{
with (k1) instance_destroy();
}
else
{
instance_destroy();
}
}[/codebox]
Now do the same except with y values. =)

Edited by Sharkiedude, 01 February 2009 - 08:23 PM.

  • 0

#3 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 01 February 2009 - 09:02 PM

It gives me this error
ERROR in
action number 1
of Alarm Event for alarm 0
for object obj_tile:

Error in code at line 22:
   if (k1 != noone) and (k1.color=color)

at position 27: Unknown variable color

And ALL the obj_tiles go boom.

Any ideas on this?
  • 0

#4 Sharkiedude

Sharkiedude

    GMC Member

  • New Member
  • 813 posts

Posted 01 February 2009 - 09:04 PM

You keep what you had previously in the create event o.o;;
[codebox]init = 96;
sprite_index=choose(spr_blue, spr_red)
if sprite_index=spr_blue{color="blue"}
if sprite_index=spr_red{color="red"}

alarm[0]=20[/codebox]
  • 0

#5 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 01 February 2009 - 09:12 PM

Thats just it; i did keep what i already had. D: The code is identical with what you posted there.

Wanna take a look at the gm7 or speculate around this problem some more?

Posted Image

Edited by llama man, 01 February 2009 - 09:15 PM.

  • 0

#6 Sharkiedude

Sharkiedude

    GMC Member

  • New Member
  • 813 posts

Posted 01 February 2009 - 09:19 PM

Change i=0 to i = 1, so it doesn't count whatever was on the very left.

[codebox]var k1, i;//Tested and working! BOOYEAH
for (i=1; i<5; i+=1) //Replace i<5 to the amount of blocks in a whole row
{
init = i*init;
k1 = instance_position(x+init,y,obj_tile);
if (k1 != noone){
if(k1.color=color)
{
with (k1) instance_destroy();
}
else
{
instance_destroy();
}
}
}[/codebox]

Edited by Sharkiedude, 01 February 2009 - 09:25 PM.

  • 0

#7 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 01 February 2009 - 09:24 PM

I tried both, niether work as they should: tiles seem to just randomly disappear instead of three or more in a row, and it still gives me the error.

Any other ideas? :/

Here's the file in case you want to experiment: Posted Image

Thank you for trying to help me. :whistle:
  • 0

#8 Sharkiedude

Sharkiedude

    GMC Member

  • New Member
  • 813 posts

Posted 01 February 2009 - 09:27 PM

Try the above code, it works, I just edited several times
EDIT: last tweak you'd want to end the for loop so it doesn't delete any other blocks:
else
{
break;
instance_destroy();
}

Edited by Sharkiedude, 01 February 2009 - 09:32 PM.

  • 0

#9 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 01 February 2009 - 09:34 PM

It SORT of works. Its just that it seems to be completely random which blocks get destroyed.

Look here:


Before:
Posted Image


After:
Posted Image

EDIT: Wait, i didnt see the break thing. Where do i insert it?

Edited by llama man, 01 February 2009 - 09:35 PM.

  • 0

#10 Sharkiedude

Sharkiedude

    GMC Member

  • New Member
  • 813 posts

Posted 01 February 2009 - 09:55 PM

Insert it when the for loop stops and destroys the instance.
var k1, i;//Tested and working! BOOYEAH
for (i=1; i<5; i+=1) //Replace i<5 to the amount of blocks in a whole row
{
init = i*init;
k1 = instance_position(x+init,y,obj_tile);
if (k1 != noone){
if(k1.color=color)
{
with (k1) instance_destroy();
}
else
{
break;
instance_destroy();
}
}
}

  • 0

#11 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 02 February 2009 - 09:33 AM

Doesnt help im afraid. :/

Here, take a look at the gmk, you will understand what's wrong when you start it up.

Posted Image

EDIT: There's something fishy going on...

Even THIS doesnt work:
var k1,k2,k3,k4,k5;
k1 = instance_position(x+96,y,obj_tile)
k2 = instance_position(96*2+x,y,obj_tile)
k3 = instance_position(96*3+x,y,obj_tile)
k4 = instance_position(96*4+x,y,obj_tile)
k5 = instance_position(96*5+x,y,obj_tile)

if (k1 != noone && k2 != noone && k3 != noone)
{
	if (k1.color==color && k2.color==color && k3.color==color)
	{
	 with (k1) instance_destroy();
	 with (k2) instance_destroy();
	 with (k3) instance_destroy();
	 instance_destroy()
	}
}

if (k1 != noone && k2 != noone)
{
	if (k1.color=color && k2.color=color)
	{
	 with (k1) instance_destroy();
	 with (k2) instance_destroy();
	 instance_destroy()
	}
}

What am i doing wrong? I have absolutely no idea how to solve this.
If you've got any other idea of how to accomplish what im trying to do, please tell me.

Edited by llama man, 02 February 2009 - 03:12 PM.

  • 0

#12 T. Westendorp

T. Westendorp

    GMC Member

  • New Member
  • 1270 posts

Posted 02 February 2009 - 04:27 PM

Or just execute some instance_place checks in every block every time you place a block of that type.





T
  • 0

#13 NakedPaulToast

NakedPaulToast

    GM Studio/Mac/Win

  • GMC Member
  • 7203 posts
  • Version:GM:Studio

Posted 02 February 2009 - 05:22 PM

if (k1 != noone && k2 != noone && k3 != noone)
{
if (k1.color==color && k2.color==color && k3.color==color)
{
with (k1) instance_destroy();
with (k2) instance_destroy();
with (k3) instance_destroy();
instance_destroy()
}
}

Lets assume that at this point you destroy k1,k2 and k3.
The variables k1,k2 and k3 will still contain the intance numbers of the previously destroyed instances




if (k1 != noone && k2 != noone) This statment will evaluate as TRUE, because k1 and k2 still contain valid instance numbers
{
if (k1.color=color && k2.color=color)
{
with (k1) instance_destroy();
with (k2) instance_destroy();
instance_destroy()
}
}


  • 0

#14 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 02 February 2009 - 06:56 PM

Yes, i forgot to add an else in between, but its doesnt solve the problem. Take a look at the gmk, please? I would really like get an answer to this. Again, thanks for helping me. :D
  • 0

#15 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 03 February 2009 - 02:51 AM

Instead of immediately destroying instances when three are in row, mark them to be deleted, and actually delete them when you have checked all of instances. It will eventually handle the case that there are four or more.
  • Add a variable in the create event:
    removed = false;
  • In the step event, with your original code (in the first post), replace all of three occurences of "instance_destroy()" with "removed = true;"
  • Then put this in the end step event.
    if (removed) instance_destroy();

Edited by torigara, 03 February 2009 - 02:59 AM.

  • 0

#16 llama man

llama man

    NOD Logo Artist

  • GMC Member
  • 1584 posts
  • Version:GM8.1

Posted 03 February 2009 - 12:03 PM

Instead of immediately destroying instances when three are in row, mark them to be deleted, and actually delete them when you have checked all of instances. It will eventually handle the case that there are four or more.

  • Add a variable in the create event:
    removed = false;
  • In the step event, with your original code (in the first post), replace all of three occurences of "instance_destroy()" with "removed = true;"
  • Then put this in the end step event.
    if (removed) instance_destroy();


YES, it works! Finally i can contine with my game. Thank you all so much!

Working gmk for anyone with the same trouble: Posted Image
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users