L
Lucretius
Guest
I have this object that moves a 32x32 block every time it moves with a state machine. Now I want to limit it's range, let's say by 1 block in every direction (32x32). I have tried many things, but every time it moves away further away from it's spawn point. In hte create event, I have the spawn point saved as startX and startY, and the maximum range, which is 32, is set by the variable maxDistance. The movement is being triggered in the alarm event. This is my step event (the state machine):
And this is my alarm event:
Some people suggested me that my conditions may be too strict. So I changed x == and y == in to <=, but the same still happens. The object happily moves away further. Where am I making a mistake? Thank you!
GML:
/// @description stop and pick new direction
if (action == 0 && place_snapped(32,32)) {
image_speed = 0;
direction = 0;
speed = 0;
}
if (action == 1 && place_snapped(32, 32)) {
sprite_index = spr_chickenRight;
image_speed = 0.5;
direction = 0;
speed = 0.5;
}
if (action == 2 && place_snapped(32, 32)) {
sprite_index = spr_chickenUp;
image_speed = 0.5;
direction = 90;
speed = 0.5;
}
if (action == 3 && place_snapped(32, 32)) {
sprite_index = spr_chickenLeft;
image_speed = 0.5;
direction = 180;
speed = 0.5;
}
if (action == 4 && place_snapped(32, 32)) {
sprite_index = spr_chickenDown;
image_speed = 0.5;
direction = 270;
speed = 0.5;
}
GML:
alarm[0] = 60;
if (x == startX-maxDistance) {
if (y == startY-maxDistance) {
action = choose(1,4);
} else if (y == startY+maxDistance) {
action = choose(1,2);
} else if (y == startY) {
action = choose(1,2,4);
}
} else if (x == startX) {
if (y == startY-maxDistance) {
action = choose(1,3,4);
} else if (y == startY+maxDistance) {
action = choose(1,2,3);
} else if (y == startY) {
action = choose(1,2,3,4);
}
} else if (x == startX+maxDistance) {
if (y == startY-maxDistance) {
action = choose(3,4);
} else if (y == startY+maxDistance) {
action = choose(2,3);
} else if (y == startY) {
action = choose(2,3,4);
}
} else {
action = 0;
}