• 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!

Drawing code repeats and never moves on

S

Sojourner

Guest
Quick little preface. I'm new to GMS2 and programming entirely, so any errors I will make will likely seem very obvious to experienced programmers. Thanks for checking my thread out, and thank you in advance for any help.

The sprite is supposed to sink into the ground and rise back up where the player clicked. When I click somewhere, however, the sprite sinks but does not reappear. At the moment, the sprite is just going to reappear close by the original sprite, so I can see if it works.

There is a spot in the code that the program never runs, this is noted in the code.


/// @desc Draw Sprite

// Variables
anim_length = 5;
frame_size = 26;
anim_speed = 10;

x_offset = frame_size/2;
y_offset = frame_size/2;

if (moveNot = true){ draw_sprite_part(spr_player_move_sheet, 0, 0, 0, frame_size, frame_size, x-x_offset, y-y_offset); }

// Sinking Sprite
if(moveSink = true){
moveNot = false;
draw_sprite_part(spr_player_move_sheet, 1, floor(x_frame)*frame_size, y_frame, frame_size, frame_size, x-x_offset, y-y_offset);
if(x_frame < 5) { x_frame += 10/room_speed; } // room_speed = 60
else if (x_frame > 5){ // Game never gets here. If I set it to >= 5, it sets back to the basic sprite after sinking.
moveSink = false;
moveRise = true;
show_debug_message("It works.");
draw_sprite_part(spr_player_move_sheet, 2, floor(x_frame)*frame_size, y_frame, frame_size, frame_size, x+frame_size, y+frame_size);
if(x_frame < anim_length) { x_frame += anim_speed/room_speed; } // room_speed = 60
else { moveRise = false; moveNot = true;}

}

}


Let me know if there is any more information I need to provide to solve this. Thanks again.
 
Last edited by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
if(x_frame < 5) { x_frame += 10/room_speed; } // room_speed = 60
else if (x_frame > 5){
If you keep adding ~0.1667 (10/60) to a variable, the resulting sequence is roughly: 0.1667, 0.3334, 0.5, 0.6667, 0.8335, 1, 1.1667, 1.3334, 1.5, 1.6667, 1.8335, 2, ... 4.5, 4.6667, 4.8335 and, finally, 5 (give or take, due to floating point precision).

5 is neither less than 5 nor greater than 5. ;)
 
S

Sojourner

Guest
Incredibly easy problem to solve, as predicted xD

Thank you. I appreciate it ^^
 

FrostyCat

Redemption Seeker
Competent Alternatives to Yin-Yang if Blocks

Alternative 1: else


For "if A do X, otherwise do Y" behaviours, state only the positive version of the condition, and leave the negative to else. The else keyword ensures that the second block never runs if the first block gets to, even if code in the first block makes the condition false in the process.
GML:
if (x_frame < 5) {
    ...
} else {
    ...
}
 
S

Sojourner

Guest
Got another problem that makes sense, though I don't know how to fix it.
The sprite is meant to rise up where the player clicked (with some grid fixes). However, I could move my mouse around and the sprite would follow my mouse until the animation finishes. How do I lock the mouse position when the player clicks to fix this?


/// @desc Draw Sprite

// Variables
anim_length = 6;
frame_size = 26;
anim_speed = 10;

x_offset = frame_size/2;
y_offset = frame_size/2;

mx = mouse_x;
my = mouse_y;
cs = cellSize;
gx = (mx div cs);
gy = (my div cs);
xx = gx*cs;
yy = gy*cs;
col_tint = -1

// Tint the Sprite
if(room = rm_tv_hallway){
col_tint = make_colour_hsv(180, 63, 100);
} else {
col_tint = -1;
}

// Unmoving Sprite
if (moveNot = true){ draw_sprite_part_ext(spr_flowey_move_sheet, 0, 0, 0, frame_size, frame_size, x-x_offset, y-y_offset, 1,1,col_tint,1); }

// Sinking Sprite
if(moveSink = true){
moveNot = false;
draw_sprite_part_ext(spr_flowey_move_sheet, 1, floor(x_frame)*frame_size, y_frame, frame_size, frame_size, x-x_offset, y-y_offset, 1,1,col_tint,1);
if(x_frame < anim_length) { x_frame += anim_speed/room_speed; }
else if (x_frame >= anim_length){ moveSink = false; moveRise = true; x_frame = 0;}
}

// Rising Sprite
if(moveRise = true){
draw_sprite_part_ext(spr_flowey_move_sheet, 2, floor(x_frame)*frame_size, y_frame, frame_size, frame_size, xx, yy, 1,1,col_tint,1);
obj_flowey.x = xx+x_offset; obj_flowey.y = yy+y_offset;
if(x_frame < anim_length) { x_frame += anim_speed/room_speed; }
if(x_frame+60/room_speed = anim_length){ moveRise = false; moveNot = true; x_frame = 0; }
else if (x_frame >= anim_length){ moveRise = false; moveNot = true; x_frame = 0; show_debug_message("It works."); }
}
 

Nidoking

Member
Why are you computing where to draw the sprite using the mouse coordinates if you don't want it to be drawn at the mouse coordinates?
 

TheouAegis

Member
Do you really want to lock the mouse position, or do you want to save the mouse position in variables and use those variables for the drawing instead?
 
S

Sojourner

Guest
That's a better way of putting it. I'd like to save the location that the player clicks, albeit with some grid snapping.
 
S

Sojourner

Guest
Aegis said it well. I'd like to save where the player clicks as a variable, and move them accordingly.
 
S

Sojourner

Guest
Getting close to figuring it out, thanks to both of you.
 
Top