A
AnotherDoor
Guest
(click to get the bigger version or press here)
(number in the left-up corner is the target location, middle one is X,Y of the red square, and the last one is the X,Y of the green square)
So, the red square has a code which moves it towards a random location (which is constant 4,7 because I have not randomized it). But there are 2 problems:
1) The starting grid coordinates for the red square are (8,8). But the code says (9,8) which is not logical, as the wall is drew in the X = 9, and it obviously is not inside the wall. Also the spawn coordinates are (320-64,320-64)(one grid square is 32x32)(The whole map is 320x320)
The grid coordinates are calculated by:
X = x/32;
Y = y/32;
One way to fix this is by
X = (x-32)/32
Y = y/32
But why is the X only one "broken" ?
2) Here it gets fascinating. If you pay attention to the pictures you see that in the 6th image (from left), the Y of the red square is not changing but instead the X is the one getting decreased ? But as we can see, it's the variable X that is broken, not the x because the x works just fine.
ALSO same with the next picture (7th from left), the box moves from left to right, so the X value should be increasing right ? But no, instead the Y value is decreased ?
And in the last pictures I just show with the green square the real coordinates of the "grid blocks" (in the last one the green one is on top of the red one thus the red one cannot be seen).
So, only thing what comes to my mind is that somewhere in my code I have a line of code which changes the X variable and the Y variable, but why doesn't it get fixed(by the step event which is more frequent than the timer, explained below):
The:
X = x/32;
Y = y/32;
Is located in step event, meanwhile rest of the moving code is self looping timer with 0,5s delay. (Timer stops looping when the X variable and Y variable of the red square equal to the X and Y of the target location)
foundgoal = 0;
finding = 0;
foundtile = 0;
ready = 0;
alarm[0] = room_speed*1;
global.Xe = 0;
global.Ye = 0;
global.Xc = x/32;
global.Yc = y/32;
//SOME OF THESE ARE NOT USED, BUT THE BOTTOM ONES ARE
finding = 0;
foundtile = 0;
ready = 0;
alarm[0] = room_speed*1;
global.Xe = 0;
global.Ye = 0;
global.Xc = x/32;
global.Yc = y/32;
//SOME OF THESE ARE NOT USED, BUT THE BOTTOM ONES ARE
global.Xc = Xc;
global.Yc = Yc;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;
array[5] = 51;
array[6] = 51;
array[7] = 51;
array[8] = 51;
//THIS FINDS EMPTY SLOT IN THE GRID
if(finding = 0)
{
repeat (100)
{
Y = irandom(9);
X = irandom(9);
global.Xe = X;
global.Ye = Y;
if (ds_grid_get(global.grid,X,Y) = 0)
{
//foundtile = 1;
//ready = 1;
finding = 1;
break;
}
}
}
//TILE = 1 = WALL
//TILE = 0 = EMPTY
array[1] = ds_grid_get(global.grid,Xc,Yc-1); //up
array[2] = ds_grid_get(global.grid,Xc-1,Yc); //left
array[3] = ds_grid_get(global.grid,Xc,Yc+1); //down
array[4] = ds_grid_get(global.grid,Xc+1,Yc); //right
//THIS IS LOCATING THE NEAREST 0 TILE CLOSEST TO THE TARGET BY CALCULATING FORMULA "DISTANCE OF TWO DOTS" WHERE Xc/Yc IS THE CORDS OF THE SQUARE AND WHERE X/Y IS THE TARGET
if (array[1] = 0) //up
{
array[5] = sqrt(abs(sqr(Xc-X))+abs(sqr((Yc-1)-Y)));
}
if (array[2] = 0) //left
{
array[6] = sqrt(abs(sqr((Xc-1)-X))+abs(sqr(Yc-Y)));
}
if (array[3] = 0) //down
{
array[7] = sqrt(abs(sqr(Xc-X))+abs(sqr((Yc+1)-Y)));
}
if (array[4] = 0) //right
{
array[8] = sqrt(abs(sqr((Xc+1)-X))+abs(sqr(Yc-Y)));
}
//show_message(string(array[6]))
//CALCULATES THE ARRAY WITH LOWEST VALUE
i = 8;
j = 50;
k = 0;
repeat(4)
{
if (array < j)
{
j = array;
k = i;
i -= 1;
//show_message(string(j));
}
else
{
i -= 1;
}
}
//THIS MOVES THE SQUARE TO THE DIRECTION OF NEAREST CORD TO THE TARGET
//up
if(k = 5)
{
y -= 32;
}
//left
if(k = 6)
{
x -= 32;
}
//down
if(k = 7)
{
y += 32;
}
//right
if(k = 8)
{
x += 32;
}
if(Xc == X && Yc == Y)
{
exit;
}
if(ready = 0)
{
alarm[0] = room_speed*1;
}
global.Yc = Yc;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;
array[5] = 51;
array[6] = 51;
array[7] = 51;
array[8] = 51;
//THIS FINDS EMPTY SLOT IN THE GRID
if(finding = 0)
{
repeat (100)
{
Y = irandom(9);
X = irandom(9);
global.Xe = X;
global.Ye = Y;
if (ds_grid_get(global.grid,X,Y) = 0)
{
//foundtile = 1;
//ready = 1;
finding = 1;
break;
}
}
}
//TILE = 1 = WALL
//TILE = 0 = EMPTY
array[1] = ds_grid_get(global.grid,Xc,Yc-1); //up
array[2] = ds_grid_get(global.grid,Xc-1,Yc); //left
array[3] = ds_grid_get(global.grid,Xc,Yc+1); //down
array[4] = ds_grid_get(global.grid,Xc+1,Yc); //right
//THIS IS LOCATING THE NEAREST 0 TILE CLOSEST TO THE TARGET BY CALCULATING FORMULA "DISTANCE OF TWO DOTS" WHERE Xc/Yc IS THE CORDS OF THE SQUARE AND WHERE X/Y IS THE TARGET
if (array[1] = 0) //up
{
array[5] = sqrt(abs(sqr(Xc-X))+abs(sqr((Yc-1)-Y)));
}
if (array[2] = 0) //left
{
array[6] = sqrt(abs(sqr((Xc-1)-X))+abs(sqr(Yc-Y)));
}
if (array[3] = 0) //down
{
array[7] = sqrt(abs(sqr(Xc-X))+abs(sqr((Yc+1)-Y)));
}
if (array[4] = 0) //right
{
array[8] = sqrt(abs(sqr((Xc+1)-X))+abs(sqr(Yc-Y)));
}
//show_message(string(array[6]))
//CALCULATES THE ARRAY WITH LOWEST VALUE
i = 8;
j = 50;
k = 0;
repeat(4)
{
if (array < j)
{
j = array;
k = i;
i -= 1;
//show_message(string(j));
}
else
{
i -= 1;
}
}
//THIS MOVES THE SQUARE TO THE DIRECTION OF NEAREST CORD TO THE TARGET
//up
if(k = 5)
{
y -= 32;
}
//left
if(k = 6)
{
x -= 32;
}
//down
if(k = 7)
{
y += 32;
}
//right
if(k = 8)
{
x += 32;
}
if(Xc == X && Yc == Y)
{
exit;
}
if(ready = 0)
{
alarm[0] = room_speed*1;
}
Xc = x/32;
Yc = y/32;
Yc = y/32;
Every help is appreciated and please do ask and I will reply.
Last edited by a moderator: