Help with simple code to make sprite shrink and grow!

Discussion in 'Programming' started by Kamon145, Jun 29, 2019.

  1. Kamon145

    Kamon145 Member

    Joined:
    Jun 29, 2019
    Posts:
    50
    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! =]
     
    OnLashoc likes this.
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,409
    "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.
     
    OnLashoc likes this.
  3. Kamon145

    Kamon145 Member

    Joined:
    Jun 29, 2019
    Posts:
    50
    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 likes this.
  4. OnLashoc

    OnLashoc Member

    Joined:
    Jan 15, 2018
    Posts:
    184
    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;
     
  5. jackquake

    jackquake Member

    Joined:
    Aug 17, 2016
    Posts:
    81
    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: Aug 27, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice