Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 24, 2024, 10:34:14 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[SOLVED] image_speed question
Pages: [1]
Print
Author Topic: [SOLVED] image_speed question  (Read 611 times)
jabcog
Level 0
**


View Profile
« on: October 02, 2016, 09:12:55 PM »

Hi everyone

I'm kinda new to coding and have picked up a game jam game my friends and I worked on (I did the art) and I'm trying to add a little bit of code that adjusts the animation speed depending on which animation cycle is playing.

At the moment the code looks like this:

Code:
if (xspd == 0)
{
    image_speed = 0.2;
}
else
{
    image_speed = 0.3;
}

But if I add this simple if statement above it and indent it, the animation starts playing 10x faster, looks buggy and doesn't actually respect the values in the code. Here it is:

Code:
if image_index = walk_sprite {
    if (xspd == 0)
    {
        image_speed = 0.2;
    }
    else if (xspd > 0)
    {
        image_speed = 0.3;
    }
}

Does anyone have an idea what might be causing this?
« Last Edit: October 03, 2016, 01:15:54 AM by jabcog » Logged
alvarop
Level 9
****


ignorant


View Profile WWW
« Reply #1 on: October 02, 2016, 10:47:25 PM »

I haven't touched game maker in a while so i might be wrong but doesn,t image_index just refer to a specific frame ?
Logged

i make games that can only ever be played once on http://throwaway.fun
Grhyll
Level 2
**



View Profile WWW
« Reply #2 on: October 02, 2016, 11:11:00 PM »

Never touched Game Maker at all, but shouldn't the comparison in your first if be "==" instead of a single "="?
Logged

Programmer at The Game Bakers
3-50.net
Current project: oQo
jabcog
Level 0
**


View Profile
« Reply #3 on: October 02, 2016, 11:17:44 PM »

Grhyll I might be wrong but I think game maker reads them both the same. I tried it out and it didn't make any difference.

Thanks alvarop! Yeah it does... I changed it to sprite_index which it should have been and it's definitely working more like I was expecting, eg when it's moving the speed of the animation plays faster.

One thing though, but I think this is more of a generic programming question rather than game maker specific, is that the animation is playing faster as soon as the character is moving faster than 0 instead of only when 'walking_sprite' is playing. Can you see anything in the code that might be causing that? Maybe I'm not using the right commands in the if statement?

This is what I've got:

Code:
if sprite_index = walk_sprite {
    if (xspd == 0)
    {
        image_speed = 0.2;
    }
    else
    {
        image_speed = 1;
    }
}
Logged
alvarop
Level 9
****


ignorant


View Profile WWW
« Reply #4 on: October 02, 2016, 11:51:58 PM »

Maybe there's some conflict with the other animations you have? Is that all the code you have related to animations?

Try using the "and" operator (not sure what it is in GML anymore, maybe && ?) and check for both speed and animation at the same time. Just to check and let me know if that works.
Logged

i make games that can only ever be played once on http://throwaway.fun
jabcog
Level 0
**


View Profile
« Reply #5 on: October 03, 2016, 12:32:50 AM »

Alvarop, I went through all of the code and made sure that image_speed wasn't being set anywhere else. I did find that the during the Create event the idle image sequence had a slower rate than what I was wanting and then continued to play around with what I was actually wanting to code.

After a bit of trial and error I realised that what I was trying to do wasn't even going to be visible since in the previous examples when xspd = 0 idle is played and then when it's > 0 it was at full speed which didn't make sense at all.. :p

Anyways, here's what I've ended up with. I don't know if its bad to have two if statements without an else but it seems to work. Is that okay?

Code:
if sprite_index = walk_sprite {
    if (xspd < 1.5){
    image_speed = 0.2;
    }
   
    if (xspd > 1.5){
    image_speed = 1;
    }
}
Logged
alvarop
Level 9
****


ignorant


View Profile WWW
« Reply #6 on: October 03, 2016, 01:03:33 AM »

Alvarop, I went through all of the code and made sure that image_speed wasn't being set anywhere else. I did find that the during the Create event the idle image sequence had a slower rate than what I was wanting and then continued to play around with what I was actually wanting to code.

After a bit of trial and error I realised that what I was trying to do wasn't even going to be visible since in the previous examples when xspd = 0 idle is played and then when it's > 0 it was at full speed which didn't make sense at all.. :p

Anyways, here's what I've ended up with. I don't know if its bad to have two if statements without an else but it seems to work. Is that okay?

Code:
if sprite_index = walk_sprite {
    if (xspd < 1.5){
    image_speed = 0.2;
    }
    
    if (xspd > 1.5){
    image_speed = 1;
    }
}

Hehe, there you go. I don't wanna give you bad advice, but for now, I think you should worry about making things work the way you want them, for you, and not necessarly doing things "the best way" (which a lot of people have been telling me : there is no right way to do things, only "your" way).

So, if it works, you're doing it the right way Smiley
Learn about else if if you really want to change it up a bit more.
Logged

i make games that can only ever be played once on http://throwaway.fun
jabcog
Level 0
**


View Profile
« Reply #7 on: October 03, 2016, 01:14:51 AM »

Cheers. I'll look into it
Logged
emptyfortress
Level 0
**



View Profile WWW
« Reply #8 on: October 03, 2016, 07:54:45 AM »

Just wanted to say that it's perfectly OK to have if statements without an else clause, or nested if statements. As long as it does what you want it to do and it's readable for you it's fine Smiley
Logged
jabcog
Level 0
**


View Profile
« Reply #9 on: October 03, 2016, 02:42:50 PM »

Beer!  thanks emptyfortress.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic