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

Help with simple code to make sprite shrink and grow!

K

Kamon145

Guest
Hello everyone, I'm trying to come up with a code that will slowly shrink a sprite to about 75%, and than have it grow back and repeat, like its sort of pulsing.
The code i came up with will shrink the sprite but it just gets stuck after that and dosen't grow, also its kind of an ugly code... I'm sure I'm doing more than necessary to achieve this effect, but I am still fairly new to coding and could use a professionals opinion..
This is my current code in the step event, note that the variable "size" is set to 1 in the create event:
Code:
if paused
{
    var shrink = true
    if size > .75 and shrink = true
    {
        size -=.01
        if size <=.75
        shrink = false
    }
    else
    if size < 1 and shrink = false
    {
        size +=.01
        if size >=1
        shrink = true
    }
    
}
image_xscale = size;
image_yscale = size;
I would also like to move this into a script once I get it working, so I can use it on other objects if necessary, oh and if anyone is curious, the object i am working on is the pause button and i want my pause sprite to pulse. Any help or suggestions would be greatly appreciated, thanks guys! =]
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
"shrink" should be an instance variable (and first set in Create), not a local variable - as it stands, it is reset to "true" on every step.
 
K

Kamon145

Guest
Doh... I knew it would be something simple like that... It works perfectly now, thank you so much for the quick reply and easy fix!
 

OnLashoc

Member
Sorry old thread bump, but was looking for a way to shrink an object during the opening sequence and this was perfect. I had to make some modifications for what I needed, but it's working flawlessly.

I put an alarm on my story object to change the shrink to true.

On the object I wanted to shrink at a specific period in the story explanation, I used this:

Create Event:
Code:
shrink = false;
size = 1;
Step Event:
Code:
 //shrinks window during opening story 
if size >=.10 and shrink = true
    {
        size -=.0005
           if size <=.10{
               shrink = false;
           }
    }
    
    
image_xscale = size;
image_yscale = size;
 

jackquake

Member
The sin function is wonderful for shrinking/growing sprites. Play around with image_xscale = 2*sin(counter). Increment counter by .01 or more/less to control rate of shrink/growth. Add a similar line for the image_yscale.

Ok, just missed the 75% shrink above. Maybe this will work.

Code:
size = abs(sin(counter))*.75 + .25;
Untested. Im away from computer.
 
Last edited:
Top