I Wanna Community

Growing and Shrinking Object

0 Members and 1 Guest are viewing this topic.

Offline RedSalamando

  • 5
  • I make games as good as I play them. Mediocrely.
  • OS:
  • Windows 10 Windows 10
  • Browser:
  • Chrome 52.0.2743.116 Chrome 52.0.2743.116
  • Playstyle: Keyboard
Growing and Shrinking Object
« 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".

Offline Nogard

  • 26
  • OS:
  • Windows 8.1/Server 2012 Windows 8.1/Server 2012
  • Browser:
  • Chrome 52.0.2743.116 Chrome 52.0.2743.116
    • Twitch
  • Playstyle: Keyboard
Re: Growing and Shrinking Object
« Reply #1 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);
Twitch channel: http://www.twitch.tv/nogard_
My not very used twitter account: https://twitter.com/Nogard__
Clear list: https://goo.gl/rnoqiz

Offline WetWookie

  • 90
  • OS:
  • Windows 10 Windows 10
  • Browser:
  • Firefox 48.0 Firefox 48.0
  • Playstyle: Keyboard
Re: Growing and Shrinking Object
« Reply #2 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:
« Last Edit: September 07, 2016, 11:38:01 AM by WetWookie »

Offline Nogard

  • 26
  • OS:
  • Windows 8.1/Server 2012 Windows 8.1/Server 2012
  • Browser:
  • Chrome 52.0.2743.116 Chrome 52.0.2743.116
    • Twitch
  • Playstyle: Keyboard
Re: Growing and Shrinking Object
« Reply #3 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.
Twitch channel: http://www.twitch.tv/nogard_
My not very used twitter account: https://twitter.com/Nogard__
Clear list: https://goo.gl/rnoqiz

Offline RedSalamando

  • 5
  • I make games as good as I play them. Mediocrely.
  • OS:
  • Windows 10 Windows 10
  • Browser:
  • Chrome 52.0.2743.116 Chrome 52.0.2743.116
  • Playstyle: Keyboard
Re: Growing and Shrinking Object
« Reply #4 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?

Offline Nogard

  • 26
  • OS:
  • Windows 8.1/Server 2012 Windows 8.1/Server 2012
  • Browser:
  • Chrome 52.0.2743.116 Chrome 52.0.2743.116
    • Twitch
  • Playstyle: Keyboard
Re: Growing and Shrinking Object
« Reply #5 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.
« Last Edit: September 07, 2016, 12:04:21 PM by Nogard »
Twitch channel: http://www.twitch.tv/nogard_
My not very used twitter account: https://twitter.com/Nogard__
Clear list: https://goo.gl/rnoqiz