Jump to content


Photo

Sprite Not Animating in Platformer State Machine


  • Please log in to reply
5 replies to this topic

#1 solidsnack

solidsnack

    GMC Member

  • New Member
  • 11 posts

Posted 01 June 2011 - 10:02 PM

I'm currently working out my first platformer with the help of The Game Maker's Companion book, doing my best to convert their drag-and-drop directions to GML as I go. Using the more advanced tutorials for platformers (the chapters on ZOOL), I have worked out the player's behavior without a hitch with the exception of one excruciating bug: none of my animated sprites will animate. I cannot for the life of my figure out why.

The book suggests a state machine to direct the behaviors of the player (jumping, walking, climbing, falling, etc.) and subsequently to draw the proper sprites for each respective state. My parent object of all the player states,"oPlayer", holds all of the sprite drawing in its draw event. From the drag-and-drop directions in the book, I converted things into GML and it looks like the following:

if(facing = FACE_RIGHT)
{
if(state = PSTATE_STAND) draw_sprite(sPStandR,-1,x,y);
if(state = PSTATE_WALK) draw_sprite(sPWalkR,-1,x,y);
if(state = PSTATE_JUMP) draw_sprite(sPJumpR,-1,x,y);
if(state = PSTATE_FALL) draw_sprite(sPFallR,-1,x,y);
if(state = PSTATE_CLING) draw_sprite(sPClingR,-1,x,y);
if(state = PSTATE_CLIMB) draw_sprite(sPClimbR,-1,x,y);
}

if(facing = FACE_LEFT) //
{
if(state = PSTATE_STAND) draw_sprite(sPStandL,-1,x,y);
if(state = PSTATE_WALK) draw_sprite(sPWalkL,-1,x,y);
if(state = PSTATE_JUMP) draw_sprite(sPJumpL,-1,x,y);
if(state = PSTATE_FALL) draw_sprite(sPFallL,-1,x,y);
if(state = PSTATE_CLING) draw_sprite(sPClingL,-1,x,y);
if(state = PSTATE_CLIMB) draw_sprite(sPClimbL,-1,x,y);
}


So for example, if facing = FACE_RIGHT (because the player has hit the right key) and state = PSTATE_JUMP (becuase the player hit space) the game draws the appropriate jumping sprite. Of all the sprites, the sPClimb & sPWalk sprites are animated and contain more than one frame, however they never animate when the player hits the respective state, they just sit frozen in frame 0 of those sprites instead.

I have no idea what's going wrong. I tried to include "image_speed = 0.4" in the draw events as a means of persuading the sprites to move, but they still don't. Aside from the GML/drag-and-drop difference, I'm pretty sure my code is identical to the tutorials (the first half of which can be seen below), but while my animated sprites work in the tutorial demo files they never work in mine.

This is driving me absolutely crazy, so if anyone has any ideas of any stupid mistakes I'm making, let me know please!

Posted Imagea
  • 0

#2 akhs001

akhs001

    GMC Member

  • New Member
  • 13 posts

Posted 01 June 2011 - 10:24 PM

Try to use Change sprite instead of Draw sprite.
if u use Gml code use the sprite_index and not Draw_sprite.

Edited by akhs001, 01 June 2011 - 10:26 PM.

  • 0

#3 solidsnack

solidsnack

    GMC Member

  • New Member
  • 11 posts

Posted 01 June 2011 - 10:40 PM

Try to use Change sprite instead of Draw sprite.
if u use Gml code use the sprite_index and not Draw_sprite.



I did try this before and just tried it again to make sure, but this does not work. I think it has something to do with the functions being in the draw event... changing from draw_sprite(); to sprite_index= renders no visual sprite on screen for the player.
  • 0

#4 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 02 June 2011 - 02:42 AM

When you put -1 for draw_sprite, it uses image_index to determine the sub-image to draw. Image_index is updated according to the current sprite that is assigned to the instance (i.e. sprite_index.) If the current sprite has only 1 sub-image, then image_index is always 0, so draw_sprite will always draw the first frame of the specified sprite.

As suggested, you'd better actually change sprite_index with the "change sprite" action, so Game Maker properly updates image_index according to the assigned sprite. And move it to the end step event, leave the draw event empty to let GM draw the sprite automatically.
  • 0

#5 solidsnack

solidsnack

    GMC Member

  • New Member
  • 11 posts

Posted 02 June 2011 - 06:34 PM

When you put -1 for draw_sprite, it uses image_index to determine the sub-image to draw. Image_index is updated according to the current sprite that is assigned to the instance (i.e. sprite_index.) If the current sprite has only 1 sub-image, then image_index is always 0, so draw_sprite will always draw the first frame of the specified sprite.


I'm not quite sure I understand what you're explaining here. Are you saying that the default assigned sprite (which, in my case, only has one frame) ensures that none of the other drawn sprites will have any of their other frames exposed when running? Just a bit confused.

As suggested, you'd better actually change sprite_index with the "change sprite" action, so Game Maker properly updates image_index according to the assigned sprite. And move it to the end step event, leave the draw event empty to let GM draw the sprite automatically.


I just tried this and it too, did not work. I removed the draw event, and replaced it with an end step event in the parent object, and then entered the code like...

if(facing = FACE_RIGHT) // Draw proper sprites for each state
{
 if(state = PSTATE_STAND) sprite_index=sPStandR
 if(state = PSTATE_WALK) sprite_index=sPWalkR
...
etc...

This resulted in the character always showing the right facing sprites, and still, unfortunately, not animating at all.

So frustrating. I really don't understand what's going on... the method that was suggested in the tutorial works absolutely perfect in the demo file, and as I mentioned before my sprites work in the demo file as well... but not in my own version... which I'm positive is identical to the original. Ugh. Thanks for the suggestion though...
  • 0

#6 nu-jalix21

nu-jalix21

    GMC Member

  • GMC Member
  • 1 posts

Posted 11 March 2012 - 03:13 AM

I can kinda relate to this, i'm doing more or less the same thing. (useing the Game Maker's Companion as a resorce to make my own game.) i've read through alot of the book and can tell you that the answer is in chapter 11, GML; ninja to pirate.

Now I could be mean and just leave it at that... but no. so here is the code the book gives us.

{
if( hurt == true )
if( global.step_count mod 6 == 0 )
exit;

if( facing == FACE_RIGHT )
{
switch( state )
{
case ZSTATE_STAND: sprite_index = spr_zool_stand_right; break;
case ZSTATE_WALK: sprite_index = spr_zool_walk_right; break;
case ZSTATE_JUMP: sprite_index = spr_zool_jump_right; break;
case ZSTATE_FALL: sprite_index = spr_zool_fall_right; break;
case ZSTATE_CLIMB: sprite_index = spr_zool_climb_right; break;
case ZSTATE_CLING: sprite_index = spr_zool_climb_right; break;
case ZSTATE_SLIP: sprite_index = spr_zool_slip_right; break;
case ZSTATE_SKID: sprite_index = spr_zool_skid_right; break;
case ZSTATE_KICK: sprite_index = spr_zool_kick_right; break;
case ZSTATE_SPIN: sprite_index = spr_zool_spin_right; break;
case ZSTATE_DEAD: sprite_index = spr_zool_die_right; break;
}
}

if( facing == FACE_LEFT )
{
switch( state )
{
case ZSTATE_STAND: sprite_index = spr_zool_stand_left; break;
case ZSTATE_WALK: sprite_index = spr_zool_walk_left; break;
case ZSTATE_JUMP: sprite_index = spr_zool_jump_left; break;
case ZSTATE_FALL: sprite_index = spr_zool_fall_left; break;
case ZSTATE_CLIMB: sprite_index = spr_zool_climb_left; break;
case ZSTATE_CLING: sprite_index = spr_zool_climb_left; break;
case ZSTATE_SLIP: sprite_index = spr_zool_slip_left; break;
case ZSTATE_SKID: sprite_index = spr_zool_skid_left; break;
case ZSTATE_KICK: sprite_index = spr_zool_kick_left; break;
case ZSTATE_SPIN: sprite_index = spr_zool_spin_left; break;
case ZSTATE_DEAD: sprite_index = spr_zool_die_left; break;
}
}

draw_sprite( sprite_index, image_index, x, y );
}

This can be found on page 259.

hope this helped some.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users