Help with for loop
#1
Posted 28 March 2012 - 02:00 PM
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
#2
Posted 28 March 2012 - 02:57 PM
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.
#3
Posted 28 March 2012 - 05:07 PM
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
#4
Posted 28 March 2012 - 05:42 PM
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 - 05:48 PM.
#5
Posted 29 March 2012 - 02:13 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:30 PM.
#6
Posted 30 March 2012 - 01:43 PM
#7
Posted 30 March 2012 - 04:19 PM
If you use 2D array:
d1 = (x1-40)/80; d2 = (y1-80)/100; array[d1,d2]
#8
Posted 31 March 2012 - 05:37 AM
I didnt use any array i tried the code place_free() but that aint workingDo 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]
#9
Posted 31 March 2012 - 07:07 AM
#10
Posted 31 March 2012 - 02:45 PM
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:45 PM.
#11
Posted 31 March 2012 - 04:17 PM
I think the position should be:
x = x1+60/2 y = y1+100/2
#12
Posted 31 March 2012 - 05:07 PM
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
#13
Posted 31 March 2012 - 08:18 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 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











