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

One of my animations will not play but the code appears to be functional

Hello everyone

I'm having a bit of a hard time with my game. I recently began using states in the class I'm taking to help program my game. The idle animation appears to be working for my enemy object, however the jumping start state does not work at all. The instructor split the jump into three states, Jump_Start, Jump, and Jump_Land. I tried using the debugger, but I couldn't find anything wrong. I tried adjusting the origin point and using the instructors sprites and that didn't work either. Any help or guidance with this problem would be greatly appreciated.

Here is the Oenemy code in the create event:

/// Enemy Udemy Code
hsp = 0;
max_hsp_initial = 2;
max_hsp = max_hsp_initial;
vsp= 0;
spd= 2;
jump_spd = -6;
hsp_decimal= 0;
vsp_decimal = 0;
facing = choose(-1,1);
///Breathing
Breath_Timer_Initial = random_range(room_speed * 1.75, room_speed * 2.25);
Breath_Timer = Breath_Timer_Initial;
image_speed = 0;

///Jump
//Will he jump this chance
Jump_Chance = 0.5;
// how often to check for a jump chance
Jump_Timer_Initial = random_range(room_speed,room_speed * 1.5)
Jump_Timer = Jump_Timer_Initial;
enum Robot_States
{
IDLE,
JUMP_START,
JUMP,
JUMP_LAND

}
state = Robot_States.IDLE

states_array[Robot_States.IDLE] = Robot_Idle_State
states_array[Robot_States.JUMP_START] = Robot_Jump_Start_State
states_array[Robot_States.JUMP] = Robot_Jump_State
states_array[Robot_States.JUMP_LAND] = Robot_Jump_Land_State


sprites_array[Robot_States.IDLE] = S_Robot_Idle
sprites_array[Robot_States.JUMP_START]= S_Robot_Jump_Start
sprites_array[Robot_States.JUMP] = S_Robot_Jump
sprites_array[Robot_States.JUMP_LAND] = S_Robot_Jump_Land



Here is the Idle State Script:

// // Robot Idle State

function Robot_Idle_State()
{
//Get Input
Breathing();

//Calculate Movement


//Modify State
if (Jump_Timer < 0) {
//Reset Timer
Jump_Timer = Jump_Timer_Initial;
// Do we jump?
var _jump = random(1);
if _jump > Jump_Chance
{
state = Robot_States.JUMP_START;
//reset breathe values
image_index = 0;
image_speed = 1;

} else {
Jump_Timer --;
}
}
//Apply Movement

//Animations
Robot_Animation_Normal();
}


Here is the Jump Start State:
// // Robot Jump Start State

function Robot_Jump_Start_State()
{
//Get Input


//Calculate Movement
Player_Collision_Normal();

//Modify State
if image_index >= image_number - sprite_get_speed(image_index)/room_speed {
state = Robot_States.JUMP;
vsp = jump_spd;

}

//Apply Movement


//Animations
Robot_Animation_Normal();
}


Collision Code or Player_Collision_Normal();

function Player_Collision_Normal()
{
if hsp == 0 hsp_decimal = 0;
if vsp == 0 vsp_decimal = 0;
//apply carried over decimals
hsp += hsp_decimal;
vsp += vsp_decimal;

//floor decimals
//save and subtract decimals
hsp_decimal = frac(hsp);
hsp -= hsp_decimal;
vsp_decimal = frac(vsp);
vsp -= vsp_decimal;
//horizontal collision
var side;
//determine which side to test
if hsp > 0 side = bbox_right else side = bbox_left;

//check the top and bottom of the side
var t1 = tilemap_get_at_pixel(global.map, side + hsp, bbox_top);
var t2 = tilemap_get_at_pixel(global.map, side + hsp, bbox_bottom);

if ((t1 != VOID) and (t1 != PLATFORM)) or
((t2 != VOID) and (t2 != PLATFORM)) {
//collision found
if hsp > 0 x = x - (x mod global.tile_size) + global.tile_size - 1 - (side - x);
else x = x - (x mod global.tile_size) - (side - x);
hsp = 0;
}

x += hsp;

//vertical collision
var side;
//determine which side to test
if vsp > 0 side = bbox_bottom else side = bbox_top;

//check the left and right side
var t1 = tilemap_get_at_pixel(global.map, bbox_left, side + vsp);
var t2 = tilemap_get_at_pixel(global.map, bbox_right, side + vsp);
var t3 = tilemap_get_at_pixel(global.map, bbox_left, bbox_bottom);
var t4 = tilemap_get_at_pixel(global.map, bbox_right, bbox_bottom);

if (t1 != VOID and(((vsp >0 or t1 != PLATFORM)) and t3 != PLATFORM) or (t1 == SOLID and t3 == PLATFORM)) or
(t2 != VOID and (((vsp >0 or t2 != PLATFORM))and t4 != PLATFORM) or (t2 == SOLID and t4 == PLATFORM)) {
//collision found
if vsp > 0 y = y - (y mod global.tile_size) + global.tile_size - 1 - (side - y);
else y = y - (y mod global.tile_size) - (side - y);
vsp = 0;
}

y += vsp;
}
 
Last edited:

TheouAegis

Member
sprite_get_speed(image_index)
Wrong variable in the parentheses.

After that, check your collision script. For that matter, why are you even running a collision check? You haven't started moving yet, have you?

If it still doesn't work, show us your script for Robot_Animation_Normal().
 
Wrong variable in the parentheses.

After that, check your collision script. For that matter, why are you even running a collision check? You haven't started moving yet, have you?

If it still doesn't work, show us your script for Robot_Animation_Normal().
How is that the wrong variable? The instructor said it was a replacement for this " image_index >= image_number - image_speed"
 
Wrong variable in the parentheses.

After that, check your collision script. For that matter, why are you even running a collision check? You haven't started moving yet, have you?

If it still doesn't work, show us your script for Robot_Animation_Normal().
Here is the animation script:

function Robot_Animation_Normal()
{
sprite_index = sprites_array[state];
//mask_index = mask_array[state];
image_xscale = facing;
switch(state){
case Robot_States.JUMP:
if image_index >= 2 {
image_speed = 0;
if vsp < 0 image_index = 2 else image_index = 3;
}
if vsp <0 image_index = 0 else image_index = 1;
break;
}
}
 

Nidoking

Member
if image_index >= 2 {
image_speed = 0;
if vsp < 0 image_index = 2 else image_index = 3;
}
if vsp <0 image_index = 0 else image_index = 1;
It looks like this is supposed to make the animation loop between frames 2 and 3, but instead, it will always make the animation loop between frames 0 and 1. If all of this is actually supposed to be there, you're either missing an else, or you have some brackets in the wrong place.
 
It looks like this is supposed to make the animation loop between frames 2 and 3, but instead, it will always make the animation loop between frames 0 and 1. If all of this is actually supposed to be there, you're either missing an else, or you have some brackets in the wrong place.
I fixed it in accordance to the lesson, it should read:

function Robot_Animation_Normal() {
sprite_index = sprites_array[state];
//mask_index = mask_array[state];
image_xscale = facing;
switch(state){
case Robot_States.JUMP:
if image_index >= 2 {
image_speed = 0;
if vsp < 0 image_index = 2 else image_index = 3;
}

break;
}
}

But the sprite still isn't moving from it's idle animation. The mask index is commented out because I haven't gotten to that lesson yet.
 

Slyddar

Member
if (Jump_Timer < 0) {
//Reset Timer
Jump_Timer = Jump_Timer_Initial;
// Do we jump?
var _jump = random(1);
if _jump > Jump_Chance
{
state = Robot_States.JUMP_START;
//reset breathe values
image_index = 0;
image_speed = 1;

} else {
Jump_Timer --;
}
}
Your brackets don't line up. The jump_timer-- should be happening if jump_timer < 0 fails. The jump_timer count will never decrease as it is.
 
Your brackets don't line up. The jump_timer-- should be happening if jump_timer < 0 fails. The jump_timer count will never decrease as it is.
Do you mean like this?
// // Robot Idle State

function Robot_Idle_State()
{
//Get Input
Breathing();

//Calculate Movement


//Modify State
if (Jump_Timer < 0) {
//Reset Timer
Jump_Timer = Jump_Timer_Initial;
// Do we jump?
var _jump = random(1);
if _jump > Jump_Chance {
state = Robot_States.JUMP_START;
//reset breathe values
image_index = 0;
image_speed = 1
}

else {
Jump_Timer --;
}
}

//Apply Movement

//Animations
Robot_Animation_Normal();
}
 

Slyddar

Member
Use spaces to make your code readable in the forum.
Also [.code=GML] to start code and [./code] to end code blocks (without the period).
Setting it out like that makes it easier to see which brackets belong to which if. Look at the logic of your code below. Step through it line by line and work out if jump_timer will ever get decreased?
GML:
//Modify State
if (Jump_Timer < 0) {
  //Reset Timer
  Jump_Timer = Jump_Timer_Initial;
  // Do we jump?
  var _jump = random(1);
  if _jump > Jump_Chance {
    state = Robot_States.JUMP_START;
    //reset breathe values
    image_index = 0;
    image_speed = 1
  } else {
    Jump_Timer --;
  }
}
 
Use spaces to make your code readable in the forum.
Also [.code=GML] to start code and [./code] to end code blocks (without the period).
Setting it out like that makes it easier to see which brackets belong to which if. Look at the logic of your code below. Step through it line by line and work out if jump_timer will ever get decreased?
GML:
//Modify State
if (Jump_Timer < 0) {
  //Reset Timer
  Jump_Timer = Jump_Timer_Initial;
  // Do we jump?
  var _jump = random(1);
  if _jump > Jump_Chance {
    state = Robot_States.JUMP_START;
    //reset breathe values
    image_index = 0;
    image_speed = 1
  } else {
    Jump_Timer --;
  }
}
I checked it against what was written in the class and in Game Maker. The if brackets are fine, however, the animation still doesn't work.
 

Slyddar

Member
I checked it against what was written in the class and in Game Maker. The if brackets are fine, however, the animation still doesn't work.
Here's a screenshot from the lecture showing the code you are referring to. Please have a close look at it and compare it to your own. As I've mentioned, the jump_timer count will never decrease as you have it, compared to what is in the class.

1625101167557.png

The brackets in your code are not aligned correctly. Bascially the bottom 4 lines are incorrect.
 
Last edited:

Japster

Member
Either way, it didn't work.
...I think you forgot "Thanks for spotting that, but..." before that. Your helper showed you a bug that clearly would ALSO be part of the issue anyway, and stopping the code from working correctly... ;)

PS - As you get better at coding, you'll start to realise it's better to generally assume that something IS wrong with your code, if it's not working... ...it still happens to ALL of us, even now... :( ...we get frustrated, going around in circles, thinking "It's not *US*", then finally realise we've done something possibly tiny, but wrong. and sometimes 1 attribute (or even 1 character!) wrong, is all that it takes...

ie:- "I checked it against what was written in the class and in Game Maker. The if brackets are fine, however, the animation still doesn't work. "

They're really not. @Slyddar is trying so hard to help you out mate, BUT also trying to teach you to troubleshoot at the same time - I've read your responses, and I sense your frustration that this still isn't working as expected, but honestly, a little politeness, appreciation and a lot less contradicting your helper goes a long way. ;)

To be fair, if you're struggling to see the very thing that's caused this issue, you NEED to figure this out, before you can improve your troubleshooting and coding, and it is literally ONE character in the wrong place. If you step through (without skipping) both pieces of code, comparing brackets (and I've now said too much), you should see the issue....

Anyway, I respect your desire to get into coding! - it's definitely sometimes frustrating, but when you crack an issue, code a cool feature that works well, or eventually release your dream game, it's an UNBEATABLE feeling!

Best of luck!
 
Last edited:
...I think you forgot "Thanks for spotting that, but..." before that. Your helper showed you a bug that clearly would ALSO be part of the issue anyway, and stopping the code from working correctly... ;)

PS - As you get better at coding, you'll start to realise it's better to generally assume that something IS wrong with your code, if it's not working... ...it still happens to ALL of us, even now... :( ...we get frustrated, going around in circles, thinking "It's not *US*", then finally realise we've done something possibly tiny, but wrong. and sometimes 1 attribute (or even 1 character!) wrong, is all that it takes...

ie:- "I checked it against what was written in the class and in Game Maker. The if brackets are fine, however, the animation still doesn't work. "

They're really not. @Slyddar is trying so hard to help you out mate, BUT also trying to teach you to troubleshoot at the same time - I've read your responses, and I sense your frustration that this still isn't working as expected, but honestly, a little politeness, appreciation and a lot less contradicting your helper goes a long way. ;)

To be fair, if you're struggling to see the very thing that's caused this issue, you NEED to figure this out, before you can improve your troubleshooting and coding, and it is literally ONE character in the wrong place. If you step through (without skipping) both pieces of code, comparing brackets (and I've now said too much), you should see the issue....

Anyway, I respect your desire to get into coding! - it's definitely sometimes frustrating, but when you crack an issue, code a cool feature that works well, or eventually release your dream game, it's an UNBEATABLE feeling!

Best of luck!
You're mistakenly under the impression that I was ignoring his advice and my brief responses are meant to be impolite; they were not. I missed a small detail because I was blinded by confirmation bias that was reinforced because I and Slyddar, whom I am sure is the instructor, checked the code in the Q and A section of his class.
 
Last edited:
Here's a screenshot from the lecture showing the code you are referring to. Please have a close look at it and compare it to your own. As I've mentioned, the jump_timer count will never decrease as you have it, compared to what is in the class.

View attachment 41022

The brackets in your code are not aligned correctly. Bascially the bottom 4 lines are incorrect.
Thanks, I honestly can't believe I missed that. I do appreciate your patience and help with this issue.
 
Top