• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM For Loops and Break: a Question

MattCast

Member
Hello, all. I've been learning how to use Gamemaker Studio 1.4 lately, and I've run into a problem I haven't been able to find the answer to by google-ing it or watching tutorials.

I've been working with for loops using the basic for loop format:

for (i=-45; i<=45; i+=1)
{
if place_free(lengthdir_x(4,image_angle) , lengthdir_y(4,image_angle+i)) break;
}

My problem is, I need to execute an extra line of code before the break. If I leave it how it is now, the engine works fine, since this code doesn't directly affect how it works at all. But I need to execute this line of code:

image_angle = point_direction(x,y,x+4*cos(image_angle),y-4*sin(image_angle+i));

as the 'then' statement of the 'if' clause. However, when I rearrange the code like this:

for (i=-45; i<=45; i+=1)
{
if place_free(lengthdir_x(4,image_angle) , lengthdir_y(4,image_angle+i))
{
image_angle = point_direction(x,y,x+4*cos(image_angle),y-4*sin(image_angle+i));
}
break;
}

the code screws up and ends up rotating my player character around crazily, since the character's direction and speed are directly related to the image_angle.

What I'm trying to do end-goal-wise is to create a Sonic the Hedgehog-esque physics engine, and what I have should in theory work. I think the problem is that I don't know the code language enough to get Gamemaker to do what I need it to do (I could be completely wrong though). If anyone has any information that could help with this, please let me know. Thank you.
 

Stubbjax

Member
What's wrong with doing this?
Code:
for (i=-45; i<=45; i+=1)
{
  if place_free(lengthdir_x(4,image_angle) , lengthdir_y(4,image_angle+i))
    break;
}

image_angle = point_direction(x,y,x+4*cos(image_angle),y-4*sin(image_angle+i));
 

MattCast

Member
What's wrong with doing this?
Code:
for (i=-45; i<=45; i+=1)
{
  if place_free(lengthdir_x(4,image_angle) , lengthdir_y(4,image_angle+i))
    break;
}

image_angle = point_direction(x,y,x+4*cos(image_angle),y-4*sin(image_angle+i));
It introduces the same problem as before–the player character spazzes out with its rotation and movement.
 
Top