Game Development Community

[Solved]Scale up and down an object , confused...

by Vlad I · in Torque Game Builder · 09/23/2012 (2:02 pm) · 12 replies

Im trying to make an object scale up and down onMouseDown, and stop all scaling after 2 pulses.
Just like fading out function.
So far I got only scaling up working and scaling down won't take place.

function Size(%this)
{
   if(%this.getsize() != 11)
   {  %this.setsize(%this.getsize() * 1.05);
      schedule(10, 0, "Size", %this);
   }
   else if (%this.getsize() == 11)
   {
      %this.setsize(%this.getsize() * 0.95);
      schedule(10, 0, "Size", %this);
   }
}

Why scaling down doesn't work?

#1
09/23/2012 (3:09 pm)
And when I do this it crashes
function SizeUp(%this)
{ 
  
   if(%this.getsize() != 10)
   {  %this.setsize(%this.getsize() * 1.01);
      schedule(5, 0, "SizeUp", %this);
   }
    if (%this.getsize() >= 10)
   {
       schedule(5, 0, "SizeDown", %this);
   }
}
function SizeDown(%this)
{ 
   if(%this.getsize() != 5)
   {
      %this.setsize(%this.getsize() * 0.99);
      schedule(5, 0, "SizeDown", %this);
   }
    if (%this.getsize() <= 5)
   {
       schedule(5, 0, "SizeUp", %this);
   }
}

What gives?
#2
09/23/2012 (6:58 pm)
Do you want a gradual pulse or a sudden size change? I can't quite tell which one you are after.
#3
09/24/2012 (3:08 am)
Gradual one, not the one where the object suddenly changes to size 10 from 5.
So you could see how the object grows up and down.
Like in Scale behavior, only here it is continues and it is not onMouseDown.tdn.garagegames.com/wiki/TGB/Behaviors/Scale_Object

While I want to be able to have only 2 pulses and then stop any scaling
#4
09/24/2012 (10:01 am)
In this implementation it uses the onUpdate function but it's easy to convert it to schedule if you still want that. I made a quick class and was dealing with a black square.

function blackie::onLevelLoaded(%this,%scenegraph)
{
	%this.setUseMouseEvents(true);
	%this.enableUpdateCallback();
	
	%this.setSize(10,10);
	
	%this.maxSize 		  = 20;
	%this.minSize 		  = 10;
	%this.sizeIncrement = 1;//Speed at which the object pulses.
	%this.maxPulses	  = 2;//Incase you want to have it pulse again.
	%this.pulseNum		  = %this.maxPulses;
	%this.doPulse		  = false;
}

function blackie::onUpdate(%this)
{
	if(%this.doPulse && %this.pulseNum)//As it pulses pulseNum goes down and it won't continue with to zeroes/falses.
	{
		if(%this.getWidth() > %this.maxSize)
			%this.sizeIncrement *= -1;
			
		if(%this.getWidth() < %this.minSize)
		{
			%this.sizeIncrement *= -1;
			%this.pulseNum -= 1;//Have the pulseNum in this if statement for it to stop at it's smallest.
		}
		
		%newWidth  = %this.getWidth()+%this.sizeIncrement;
		%newHeight = %this.getHeight()+%this.sizeIncrement;
		%this.setSize(%newWidth,%newHeight);
	}
}

function blackie::onMouseDown( %this, %modifier, %worldPos, %mouseClicks )
{
	%this.doPulse = true;	
}

function blackie::onMouseUp( %this, %modifier, %worldPos, %mouseClicks )
{
	%this.doPulse = false;
	%this.pulseNum = %this.maxPulses;
}

function sceneWindow2D::onMouseUp( %this, %modifier, %worldPos, %mouseClicks )
{
	//If you want to register no matter where the mouse is.
	BlackieObject.doPulse = false;
	BlackieObject.pulseNum = %this.maxPulses;
}

Hopefully you can figure it out from here. :)
#5
09/24/2012 (11:20 am)
Thanks Alpha I'm gonna check this out right now!
I was afraid of onUpdate. Everyone seems to be talking about the evil onUpdate. I heard it is memory consuming and bug riddled.
Did you have any problems using it in the past?

I'll let you know how it went.

#6
09/24/2012 (12:49 pm)
I look at your code and it works beautifully!
But how would I pass or schedule it as a fade function.
I have objects which use different classes.
For instance:
function Logs01Pt02Class::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
   //if we click on an item we need to check if we can pick this item up, the cursor is active and our inventory is not full.
   if(%this.pickup == true && $cursorActive == true && $inventoryTotal != 21)
   {  
     %this.safedelete();
     playFoundSoundEffect(%this);
     LogsIcon01Dummy.setVisible(true);
     LogsIcon01Dummy.setBlendAlpha(0);
     fadeInObject(LogsIcon01Dummy);
// So here I'd want to pass to LogsIcon01Dummy Scale Up and Down before //I schedule it to MoveToInventory and then delete for good
// something like ScalePulses(LogsIcon01Dummy); ?
     schedule( 3500, 0, "MoveToInventory",LogsIcon01Dummy);    
     LogsIcon01.setSizeY(6.250);
     LogsIcon01.setSizeX(6.250); 
     //and we add this item to the inventory.
     addToInventory(LogsIcon01);
     updateSceneSave(%this);
     return;
   }
}


#7
09/24/2012 (2:08 pm)
All right, my bad on the onUpdate. I recommend you keep the specific numbers variables because it's much easier to tweak later. There are two ways to proceed:
1. Every object declares it's own variables. This allows stuff like different max sizes for different objects.
2. Make the specific variables global variables if every object will be treated the exact same.

My example here uses idea 1. I used one global because that seems logical for it's designated use.

$PULSEDELAY = 50;
function blackie::onLevelLoaded(%this,%scenegraph)
{
	%this.setUseMouseEvents(true);
	
	%this.maxSize 		  = 20;
	%this.minSize 		  = 10;
	%this.sizeIncrement = 1;//Speed at which the object pulses.
	%this.maxPulses	  = 2;//Incase you want to have it pulse again.
	%this.pulseNum		  = %this.maxPulses;
	%this.doPulse		  = false;
}

function pulseObject(%object)
{
	if(%object.doPulse && %object.pulseNum)//As it pulses pulseNum goes down and it won't continue with to zeroes/falses.
	{
		if(%object.getWidth() > %object.maxSize)
			%object.sizeIncrement *= -1;
			
		if(%object.getWidth() < %object.minSize)
		{
			%object.sizeIncrement *= -1;
			%object.pulseNum -= 1;//Have the pulseNum in this if statement for it to stop at it's smallest.
		}
		
		%newWidth  = %object.getWidth()+%object.sizeIncrement;
		%newHeight = %object.getHeight()+%object.sizeIncrement;
		%object.setSize(%newWidth,%newHeight);
		
		schedule($PULSEDELAY,0,"pulseObject",%object);
	}
}

function blackie::onMouseDown( %this, %modifier, %worldPos, %mouseClicks )
{
	%this.doPulse = true;
	pulseObject(%this);
}

function blackie::onMouseUp( %this, %modifier, %worldPos, %mouseClicks )
{
	%this.doPulse = false;
	%this.pulseNum = %this.maxPulses;
}

function sceneWindow2D::onMouseUp( %this, %modifier, %worldPos, %mouseClicks )
{
	//If you want to register no matter where the mouse is.
	BlackieObject.doPulse = false;
	BlackieObject.pulseNum = %this.maxPulses;
}

I think that function is general enough for any object with those variables.

I have personally never had any problems with "onUpdate". I think people are scared of it because if you have a LOT in the function and a LOT of objects using that busy function then it well slow down but I have had large 'for' loops with significant amount of looping with no problem. That and you can't tell which objects get updated in what order so don't assume one object will be executed before another.
#8
09/24/2012 (2:42 pm)
Alpha I just tried your new code, it doesn't work.

To test it I put a small sprite in the scene with size 6.250 by 6.250, gave it class "blackie".
In the game I click on it, it scales up very fast by a little bit as if it only changed to 6.350 by 6.350 and then back to 6.250 by 6.250 only 1 pulse.

Unless I don't set some additional parameters.
#9
09/24/2012 (3:25 pm)
Ok, my fault :) I overlooked the
%this.minSize         = 10;
while my object had 6.250

But why wouldn't it work if I execute it like this:
function Logs01Pt02Class::onMouseDown(%this, %modifier, %worldPosition, %clicks)  
    {  
         %this.safedelete();  // I delete the object the class it is assigned to 
         LogsIcon01Dummy.doPulse = true;  // and now I want this other object to scale up and down
         pulseObject(LogsIcon01Dummy);    //

      }
#10
09/24/2012 (3:26 pm)
You have to change the max and min size. I didn't know what size your objects were so I just picked random numbers. If you want to can replace the variables with this code so that never happens again. Just change the numbers to adjust the amount of pulse.

%this.setSize(6,6);
	
	%this.maxSize 		  = %this.getWidth()*2;
	%this.minSize 		  = %this.getWidth()/2;
//or have it whatever you like.
	%this.maxSize 		  = 4;
	%this.minSize 		  = 12;
#11
09/24/2012 (3:33 pm)
ok, sorry again I sorted the other problem out too.
Forgot to give class "blackie" to LogsIcon01Dummy :)

Thanks yet again for all your help Alpha!
#12
09/24/2012 (3:37 pm)
Cool, no problem!