I Wanna Community

Fangames => Game Design => Programming Questions => Topic started by: RedSalamando on September 07, 2016, 10:47:52 AM

Title: Growing and Shrinking Object
Post by: RedSalamando on September 07, 2016, 10:47:52 AM
Kind of a dumb question, but I really can't find it anywhere. Most I can find is sprite resizing. Its probably somewhere, but I've been searching using the wrong words.

I'm needing an object, say a fruit, to grow, or optionally to shrink when it caps at a certain size.
Something along the idea of: "starts off 75% scale (or 100%), grows every step until it hits a cap of 125%, then every step it starts to shrink until 75% is hit again, and repeats".
Title: Re: Growing and Shrinking Object
Post by: Nogard on September 07, 2016, 11:30:36 AM
Hi RedSalamando, what you're looking for are the variables image_xscale and image_yscale, which modify the size of an object.

A way to implement what you're asking for can be the following

In the create event:
Code: [Select]
image_xscale = 0.75;
image_yscale = 0.75;
growing = true;
growing_rate = 0.1;
max_size = 1.25;
min_size = 0.75;

"image_xscale" and "image_yscale" reflect the starting size
"growing" is boolean to keep track if the object is growing or shrinking
"growing_rate" is the change in size of the object
"max_size" and "min_size" are the size restrictions

In the step event:
Code: [Select]
if(growing == true)
{
  image_xscale += growing_rate;
  image_yscale += growing_rate;
}
else
{
  image_xscale -= growing_rate;
  image_yscale -= growing_rate;
}
if(image_xscale >= max_size || image_xscale <= min_size)
  growing = !growing;

Here we first update the size of the object, then we check if the size is still in the interval (min_size, max_size), if not we change the boolean value of "growing" to reverse the growth/shrinking process.

In this case the growth/shrinking is linear, but you can implement it in the way you prefer.


Another solution that uses the sine function is this

In the create event
Code: [Select]
time = 0;
growth_rate = 0.1;

In the step event:
Code: [Select]
time += growth_rate;
image_xscale = 1+0.25*sin(time);
image_yscale = 1+0.25*sin(time);
Title: Re: Growing and Shrinking Object
Post by: WetWookie on September 07, 2016, 11:35:07 AM
(click to show/hide)

(click to show/hide)

Nogard already posted while I was writing this up but it seemed a shame to do all that work and then not hit submit.   :ZreknarF:
Title: Re: Growing and Shrinking Object
Post by: Nogard on September 07, 2016, 11:42:09 AM
I was actually hoping no one would reply before me while I was writing the post.
If you're going for the smooth scaling WetWookie's code is easily customizable. The linear scaling is about the same.
Title: Re: Growing and Shrinking Object
Post by: RedSalamando on September 07, 2016, 11:44:28 AM
Thanks guys, this is what I was looking for.

Quick question on the topic: Now if I want an object to grow into infinity, would I just remove the cap from the code entirely, or as a more simpler method just make the cap absurd and just roll with it?
Title: Re: Growing and Shrinking Object
Post by: Nogard on September 07, 2016, 11:50:43 AM
Having a very high cap would work and wouldn't require modifying the step event. I would personally choose this option.
I remind you that you can change the variables in the creation code of instances in a room, so that you can have different behaviours with the same object.