Jump to content


Photo

How to stop movement while animation is playing?


  • Please log in to reply
16 replies to this topic

#1 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 26 June 2012 - 03:49 PM

Hi,

I am pretty new to Game Maker, and I am having this problem. It is probably easy to fix, but I cant figure it out. I need a sprite to play an animation, which I figured out how to do with a timeline, but I need the sprite to not be able to move while the animation is playing. How could I do this? Coding and blocking are both desired since I need to learn GML anyway.

Thanks!!

Edited by KrytixKrytic, 26 June 2012 - 03:52 PM.

  • 0

#2 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3230 posts
  • Version:Unknown

Posted 26 June 2012 - 03:58 PM

if( image_speed != 0 ) // image_speed handles how fast the animation cycles.. if it is zero the sprite will not animate
   speed = 0; // so if the sprite is animating, stop speed
This works fine unless you are moving the object via relative position ( x += someAmount ).
If that is the case then have a variable that controls movement before you move will help
example:
// create event
canMove = true;

// key event
if( canMove )
   x += someValue;

  • 0

#3 Swifty

Swifty

    Shadow

  • GMC Member
  • 362 posts
  • Version:GM:Studio

Posted 26 June 2012 - 04:09 PM

Hi,

I am pretty new to Game Maker, and I am having this problem. It is probably easy to fix, but I cant figure it out. I need a sprite to play an animation, which I figured out how to do with a timeline, but I need the sprite to not be able to move while the animation is playing. How could I do this? Coding and blocking are both desired since I need to learn GML anyway.

Thanks!!

In the sprite editor, if you open up a sprite and before you go into it to edit, you can copy and paste/add a new 'frame'. So you do you first frame, then when you're finished editing, copy it and paste another frame and edit that one etc until you have your animation. I'm just checking this is infact how you're doing it because if so - you won't need a timeline.

Now if you assign that sprite to an object, and put it in the room, the animation should play.. If you don't want it to play at the start in the animated objects Create Event put:
image_single = 0
That will set it to the first image of the animation. Now when you want it to play the animation you can put on say key press right arrow:
image_single = -1
This will make it start animating. To stop it, just put the first code on a key release.

To not have it able to move while the animation is playing make a variable that you can toggle to true/false in the create event.
So say.. Adding to objects Create Event you would now have:
image_single = 0
animating = false
Then when you use your image_single = -1 code to animate it change the animating = true variable to true. Then when you want to stop the animation you'd put:
image_single = 0
animating = false
Now that you have that set up you can do a check before you move your object, so say:
if animating = false{
  //movement code here
}
You can also check the animating variable in Drag&Drop by using that purple hexagon icon, I believe.. :GM073:

Swifty

Edited by Swifty, 26 June 2012 - 04:12 PM.

  • 0

#4 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 26 June 2012 - 05:22 PM

thanks guys,

now the problem that I have is that I need the animation to play for a specific amount of time while being unmovable when it collides with an object. How would I do this?

Edited by KrytixKrytic, 26 June 2012 - 05:44 PM.

  • 0

#5 Swifty

Swifty

    Shadow

  • GMC Member
  • 362 posts
  • Version:GM:Studio

Posted 26 June 2012 - 10:16 PM

In the collision event with the object that triggers the animation put in code:
if animating = false{
 animating = true
 image_single = -1
 alarm[0] = 60
}
Now to work out how long it'll go for, you need to check what the room speed is set at in the room settings, be default it will be 30. So that means it runs 30 frames a second so that alarm would be 2 seconds. If you wanted it to go for 5 seconds just work out 5*30 (150).

To stop it animating use the stop code I linked you earlier in the Alarm[0] Event:
animating = false
image_single = 0

Switty

Edited by Swifty, 26 June 2012 - 10:18 PM.

  • 0

#6 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 26 June 2012 - 10:21 PM

In the collision event with the object that triggers the animation put in code:

if animating = false{
 animating = true
 image_single = -1
 alarm[0] = 60
}
Now to work out how long it'll go for, you need to check what the room speed is set at in the room settings, be default it will be 30. So that means it runs 30 frames a second so that alarm would be 2 seconds. If you wanted it to go for 5 seconds just work out 5*30 (150).

To stop it animating use the stop code I linked you earlier in the Alarm[0] Event:
animating = false
image_single = 0

Switty


Thanks and how would I set up the alarm[0] event? Sorry for the many questions, it's my first game maker game
  • 0

#7 Swifty

Swifty

    Shadow

  • GMC Member
  • 362 posts
  • Version:GM:Studio

Posted 26 June 2012 - 10:41 PM

Thanks and how would I set up the alarm[0] event? Sorry for the many questions, it's my first game maker game

It's no problem, we're always happy to help - All events are at the same place, so double click your object to open it, and then click Add Event and then look for Alarm and pick Alarm 0.
This event triggers when the alarm goes off, so for example if you set the alarm to 60 by using the "alarm[0] = 60" code, after the 60frames it'll trigger the Alarm 0 Event and so that's when you want to stop animating :)


Switty

Edited by Swifty, 26 June 2012 - 10:46 PM.

  • 0

#8 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 27 June 2012 - 01:11 PM

THANKS THAT WORKED PERFECTLY!!! Now for the next problem lol. The game that I have has a reverse health bar, so when the health bar reaches its max, I have a sprite animation play, but now the sprite animation that I have plays quickly followed by the animation that I was asking about earlier. How do I stop the animation from the first sprite and start the other?

EDIT: nvm I forgot to delete the timeline, it works now. Thanks guys

ReEdit: OK so the question this time. I have a reverse health bar (as mentioned above) and I would like to have a score counter that increases with the amount of health that you have accumulated. How exactly would I go about doing this??? Like if the health is a one, I would like the score to go up by one, but if the health is at 50, i would like the score to go up by 50. Is this possible???

Edited by KrytixKrytic, 27 June 2012 - 02:10 PM.

  • 0

#9 Untaken

Untaken

    GMC Member

  • GMC Member
  • 106 posts
  • Version:GM8

Posted 27 June 2012 - 03:27 PM

Entirely possible.
Event triggering score increasing:
myScore+=myHealth

  • 0

#10 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 27 June 2012 - 05:22 PM

The problem is that I want the health bar to decrease over time, so I dont completely know how I would write that code.

The premise of the game is that the character collects items and when he collects the items, his health increases, however when he doesnt, his health decreases, and the higher his health, the more points he scores.

Edited by KrytixKrytic, 27 June 2012 - 05:26 PM.

  • 0

#11 Untaken

Untaken

    GMC Member

  • GMC Member
  • 106 posts
  • Version:GM8

Posted 27 June 2012 - 09:59 PM

The best way would be through collisions, an score object parent and alarms then. Create a parent object like parScoreObject then make any items that the player collects have the parent object as its parent.
Player object
Create
alarm[0]=1 //Can make this any alarm, place in any event and can stop by setting alarm to -1

Alarm[0] event
<object with health variable>.myHealth-=<amount to decrease by>
alarm[0]=1 //Will do code above first before setting the alarm

Collision with parScoreObject
<object with health variable>.myHealth+= <amount to increase by>
<object with score variable>.myScore+=myHealth

Item object
Collision with player object
instance_destroy()
Bear in mind with the alarms that room_speed is the equivalent of 1 second.

Edited by Untaken, 27 June 2012 - 10:00 PM.

  • 0

#12 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 28 June 2012 - 01:53 PM

the game is saying that the myHealth variable is not recognized. I tried setting the variables, but the game didnt quite like that for some reason....
  • 0

#13 Untaken

Untaken

    GMC Member

  • GMC Member
  • 106 posts
  • Version:GM8

Posted 29 June 2012 - 11:49 AM

What was happening with the game after setting myHealth? It's always a good idea to declare a variable first (which wasn't mentioned in the code above) and the Create event's a common place to declare them e.g
Create:
myHealth=<starting value>

  • 0

#14 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 29 June 2012 - 02:16 PM

ok thanks I got it. The problem was that I had forgotten to parent the objects. I think the only problem that I have now is that the animations that I had a problem with before will now only play if I hold down my directional keys. it will pause all movement but the animation itself wont play unless I continue to hold down the movement key
  • 0

#15 Untaken

Untaken

    GMC Member

  • GMC Member
  • 106 posts
  • Version:GM8

Posted 29 June 2012 - 10:51 PM

Does the object show the first sprite of the moving animation as well? As far as I can understand the code, the object should only animate when moving or if it's hit something that can't be moved and not do anything else otherwise. Sounds like you might be using draw_sprite(<sprite name>,image_single,<x location>,<y location>) and if the second parameter in draw_sprite's -1 then it'll animate but anything else and the sprite will stay at the given sub-image if it exists.
  • 0

#16 KrytixKrytic

KrytixKrytic

    GMC Member

  • New Member
  • 9 posts
  • Version:GM8

Posted 30 June 2012 - 01:43 PM

the character moves to the collision with the object and runs his animation, but it only continues running if I hold down the movement key. If i dont hold down the key, he just stands there but he wont move (which is what I wanted).
  • 0

#17 Untaken

Untaken

    GMC Member

  • GMC Member
  • 106 posts
  • Version:GM8

Posted 30 June 2012 - 11:45 PM

So if I've understood this correctly, the object only animates when it hits an unmovable object when a movement key's held? The collision code given looks like it only works if a movement key is released when you reach the unmovable object since it checks if animating's false before doing anything.
Collision with unmovable object
if animating=false then exit else animating=true
image_single=-1 //Keep object animating
if alarm[0]=-1 then alarm[0]=<number of seconds>*room_speed
Might not be any better than what's there right now and there's the off chance that someone might just stop short of the object and nothing'll happen because of the first if statement but this code will work with the alarm[0] already given and will keep animating until the alarm goes and triggers animating to be false.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users