Fangames > Programming Questions

Growing and Shrinking Object

(1/2) > >>

RedSalamando:
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".

Nogard:
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: ---image_xscale = 0.75;
image_yscale = 0.75;
growing = true;
growing_rate = 0.1;
max_size = 1.25;
min_size = 0.75;

--- End code ---

"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: ---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;

--- End code ---

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: ---time = 0;
growth_rate = 0.1;

--- End code ---

In the step event:

--- Code: ---time += growth_rate;
image_xscale = 1+0.25*sin(time);
image_yscale = 1+0.25*sin(time);

--- End code ---

WetWookie:
(click to show/hide)
* (click to show/hide)MaximumScale = 1.25;
MinimumScale = 0.75;
CurrentScale = MinimumScale;
ScaleChangePerStep = 0.01;

* (click to show/hide)CurrentScale += ScaleChangePerStep;
if CurrentScale >= MaximumScale
{
     CurrentScale = MaximumScale;
     ScaleChangePerStep = - ScaleChangePerStep;
}
if CurrentScale <= MinimumScale
{
     CurrentScale = MinimumScale;
     ScaleChangePerStep = - ScaleChangePerStep;
}
image_xscale = CurrentScale;
image_yscale = CurrentScale;

(click to show/hide)
* (click to show/hide)MaximumScale = 1.25;
MinimumScale = 0.75;
ResizeDistance = (MaximumScale - MinimumScale) / 2;
ResizeSpeed = 5 / room_speed ;
CurrentScale = MinimumScale;
StepVal = 0;

* (click to show/hide)StepVal += ResizeSpeed;
CurrentScale = MinimumScale + ResizeDistance * sin(StepVal) + ResizeDistance;
image_xscale = CurrentScale;
image_yscale = CurrentScale;

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:

Nogard:
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.

RedSalamando:
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?

Navigation

[0] Message Index

[#] Next page

Go to full version