Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1360548 Posts in 63351 Topics- by 55198 Members - Latest Member: RevolvingDoor

May 25, 2019, 12:33:09 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Game Maker animation using code
Pages: [1]
Print
Author Topic: Game Maker animation using code  (Read 22190 times)
denzgd
Level 1
*



View Profile WWW
« on: July 25, 2014, 06:18:18 PM »

I'm currently using Game Maker Studio, and I'm trying to animate the sprite using code in my Step Event. I've tried looking this up, but the responses have either been tailored to somebody's unique situation (like I can't sift through a block of code and find which part does the animating), or it has used one of the presets in the event. I want to know how to do this using code only.

Also, I have all these frames for various actions, such as walking, jumping, talking, etc. Should I have different objects for these, or just keep all the frames for all animations in the same object? Thanks in advance!
Logged

Virtu
Level 0
*



View Profile
« Reply #1 on: July 25, 2014, 07:07:40 PM »

It's actually quite simple as Game Maker handles most of the animating for you.

Create one sprite for each of your actions: have one sprite containing all the frames of your walk cycle, one for the jumping animation, one for the talking loop and so on. You can tell your game object to use that sprite by assigning its sprite_index variable to it. The animation will loop at the same speed as your room's speed by default, but you can change that by setting the image_speed variable to a different value. (e.g.:"image_speed = 0;" displays a static frame, whereas "image_speed = 0.5;" displays your animation at half the room speed)

Say you have named your walking and standing sprites spr_walk and spr_idle respectively. Your code could look slightly like this:

Code: (Step Event)
 if (keypress) { //Started walking
     [...]  //actually move
     sprite_index = spr_walk; //Set the correct sprite (walking animation)
     image_speed = 1/4;       //Room speed is 4 times faster than animation. (In a 60 fps game, the animation would show 15 frames per second.)
  }

  if (keyrelease) { //Stopped walking
     sprite_index = spr_idle;
     image_speed = 0;
  }

Additionally, you can control your animation's current frame with the image_index variable. Might be useful if you want to do something when an object with an animated sprite hits a certain frame. You do not need to manually increment this variable if you set the image_speed correctly.

I hope this helped. If you have any other questions, ask away! Wink
Logged

denzgd
Level 1
*



View Profile WWW
« Reply #2 on: July 25, 2014, 08:10:43 PM »

If you have any other questions, ask away! Wink

What is the code that controls how the sprite is drawn? Specifically, I'd like to flip it horizontally based on which way the player is moving.
Logged

Zack Bell
Level 10
*****



View Profile WWW
« Reply #3 on: July 25, 2014, 10:29:04 PM »

Set image_xscale to 1 or -1.
Logged

denzgd
Level 1
*



View Profile WWW
« Reply #4 on: July 25, 2014, 10:52:11 PM »

Set image_xscale to 1 or -1.

Oh yeah forgot to mention I figured it out. Thanks, though! And good luck with Super III! Looking forward to seeing how it turns out.
Logged

Zack Bell
Level 10
*****



View Profile WWW
« Reply #5 on: July 25, 2014, 11:11:07 PM »

Set image_xscale to 1 or -1.

Oh yeah forgot to mention I figured it out. Thanks, though! And good luck with Super III! Looking forward to seeing how it turns out.

Thanks! SUPER III is indeed a Game Maker: Studio game, as well.  Coffee
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic