# Help with for loop

12 replies to this topic

### #1 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 28 March 2012 - 02:14 PM

i am using point in rectangle(x1,y1,x2,y2) method to determine mouse clicks
my x values goes like this 40,100,120,180,200,260,280,340,360,420,440,500,520,580
and my y values goes like this80,180,280,380,480,580

Now my 1st row point in rectangle will be (40,80,100,180) and my 2nd point in rect (120,80,180,180)
Now my 1st rectangle from 2nd row will be (40,180,100,280) and soo on

Now how can i use a for loop for this
• 0

### #2 Cpt Chuckles

Cpt Chuckles

GMC Member

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

Posted 28 March 2012 - 03:11 PM

looks like the values in the x are increasing by 60 then 20 then 60 etc., so you'll need a toggle variable to keep track of which increment to use on each iteration of the loop. the y value simply increases by 100 each time so that's easy.

i assume you're asking for how to construct a for...loop that handles the increments the way you need, so here is what i came up with (i used a repeat instead of for...loop because it seemed simpler; if you need the for...loop for some reason it should be easy to change):
```x = 40;
y = 80;
twenty = false; //this keeps track of when to increment x by 20. if false, then by 60.
repeat(howevermanytimes)
{
if(twenty){ x += 20; } else { x += 60; }
y += 100;

twenty = !twenty; //invert the toggle
}```
this code doesn't include the checks you need to do, it only demonstrates how to increment the values the way they do in your list. it should be easy enough to save the values to arrays or something, and then perform your checks using those.
• 0
¯\(ºдಠ)/¯ I DUNNO

### #3 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 28 March 2012 - 05:21 PM

i have 9 x 5 rectangles
so there wont be any increment of x for 1st 9 rectangles which your code doesnt seem to do

I think forloop might be simpler soln
• 0

### #4 Maerron

Maerron

GMC Member

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

Posted 28 March 2012 - 05:56 PM

Probably this is what you want?
```var x1, y1, ;
for (y1 = 80; y1 <= 480; y1 += 100)
{for (x1 = 40; x1 <= 520; x1 += 80)
{if (point_in_rectangle(x1,y1,x1+60,y1+100))
{;}
}
}
```

Edited by Maerron, 28 March 2012 - 06:02 PM.

• 0

### #5 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 29 March 2012 - 02:27 PM

Probably this is what you want?

```var x1, y1, ;
for (y1 = 80; y1 <= 480; y1 += 100)
{for (x1 = 40; x1 <= 520; x1 += 80)
{if (point_in_rectangle(x1,y1,x1+60,y1+100))
{;}
}
}
```

Thanks this works
But i have also one problem if there is already a plant present in rectangle then it should do nothing
how to acheive

Edited by witcher, 29 March 2012 - 02:44 PM.

• 0

### #6 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 30 March 2012 - 01:57 PM

bump
• 0

### #7 Maerron

Maerron

GMC Member

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

Posted 30 March 2012 - 04:33 PM

Do you use array to store information about the whether the rectangles contain plants or not? Or simply by checking collision?

If you use 2D array:
```d1 = (x1-40)/80;
d2 = (y1-80)/100;
array[d1,d2]
```

• 0

### #8 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 31 March 2012 - 05:51 AM

Do you use array to store information about the whether the rectangles contain plants or not? Or simply by checking collision?

If you use 2D array:

```d1 = (x1-40)/80;
d2 = (y1-80)/100;
array[d1,d2]
```

I didnt use any array i tried the code place_free() but that aint working
• 0

### #9 Desert Dog

Desert Dog

GMC Member

• GMC Elder
• 6409 posts
• Version:Unknown

Posted 31 March 2012 - 07:21 AM

You could use the collision_rectangle function. Look it up in the GM help manual.
• 0
HTML5 games for mobile:
HexDogs Bugz Burn! Captain George Golfing Block Memory

Games for Androids

Code: General Array Functions - GM-S friendly. sorting, shuffling. Includes a quicksort.
Use the quicksort to sort ds_lists 10-18 times faster than ds_list_sort()!

### #10 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 31 March 2012 - 02:59 PM

var x1, y1, ;
for (y1 = 80; y1 <= 580; y1 += 100)
{for (x1 = 40; x1 <= 740; x1 += 80)
{if (point_in_rectangle(x1,y1,x1+60,y1+100,mouse_x,mouse_y))
{
if drag=true and (collision_rectangle(x1,y1,x1+60,y1+100,plan1,false,false))
{
{
x= (x1+x1+80)/2
y=(y1+y1+100)/2
drag=false;
action_change_object(plan1,true);
}

}

}
}
}

When i use above code cant put plant down at all

Edited by witcher, 31 March 2012 - 02:59 PM.

• 0

### #11 Maerron

Maerron

GMC Member

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

Posted 31 March 2012 - 04:31 PM

Are you trying to change an instance into plan1 and place them in the middle of the rectangle if there is plan1 in the rectangle?
I think the position should be:
```x = x1+60/2
y = y1+100/2```

• 0

### #12 witcher

witcher

GMC Member

• GMC Member
• 396 posts
• Version:Unknown

Posted 31 March 2012 - 05:21 PM

I am trying to make sure that there is no plan1 in rectangle
and if there is no plan1 then place a plan1 in rectangle(it gets changed to plan1)
But aint working

I think my above positions are perfect as plants was snapping perfectly in the middle before putting collison code
• 0

### #13 Desert Dog

Desert Dog

GMC Member

• GMC Elder
• 6409 posts
• Version:Unknown

Posted 31 March 2012 - 08:32 PM

```var x1, y1, ;
for (y1 = 80; y1 <= 580; y1 += 100)
{
for (x1 = 40; x1 <= 740; x1 += 80)
{
if (point_in_rectangle(x1,y1,x1+60,y1+100,mouse_x,mouse_y))
{
if drag=true and (collision_rectangle(x1,y1,x1+60,y1+100,plan1,false, false))
{
x= (x1+x1+80)/2
y=(y1+y1+100)/2
drag=false;
action_change_object(plan1,true);
}

}
}
}```

I cleaned up the braces (you had too many)

Also, you may want to change the last argument in collision_rect to 'true'. (It won't matter unless the calling object is an object plan1,(possibly by inheritance)
• 0
HTML5 games for mobile:
HexDogs Bugz Burn! Captain George Golfing Block Memory

Games for Androids