Game Development Community

Trying to delete an object after a set ammount of time

by DiegoGdVU · in Torque 2D Beginner · 09/15/2013 (2:00 pm) · 4 replies

Hi, i'm trying to delete an object after some time using schedules to simulate a punch, but it's not working, the hitbox appears after the keypress, but the object won't dissapear after 500 ms.

function playerGolem::punch(%this)
{
	%hit= new sceneObject()
	{
		SceneLayer   = "1";
		SceneGroup = "2";		
		CollisionCallback = true;
		size = "1 1";
		Class="playerPunch";
	};
	
	%hit.setBodyType( dynamic );
	
	%hit.createPolygonBoxCollisionShape(1,1);
	
	%hit.Position = playerGolem.getPosition();
	
	%hit.setCollisionGroups(3, 31); 
	
	%hit.setCollisionLayers(3);
	
	if (strcmp("down",playerGolem.facing)==0)
	{
		%hit.LinearVelocity="0 -10";
	}
	if (strcmp("up",playerGolem.facing)==0)
	{
		%hit.LinearVelocity="0 10";
	}
	if (strcmp("right",playerGolem.facing)==0)
	{
		%hit.LinearVelocity="10 0";
	}
	if (strcmp("left",playerGolem.facing)==0)
	{
		%hit.LinearVelocity="-10 0";
	}
	
	myScene.add(%hit);
	
	//falta borrarlo
	schedule(500,deletePunch);
}

function playerPunch::deletePunch(%this)
{
	%this.owner.safeDelete();
}

#1
09/15/2013 (2:19 pm)
When you write schedule(500,deletePunch); you don't associate the schedule with anything.
You have to do:
%obj.schedule(timeMS, methodName);
#2
09/15/2013 (2:50 pm)
Thanks Lukas, that did the trick.
#3
09/15/2013 (6:43 pm)
Alternatively:

//%this=handle of object calling its method
%Handle_Schedule=schedule(1000,0,"Class_Name::Method_Name",%this);
#4
09/15/2013 (9:48 pm)
And if you need to pause scheduled events that aren't attached to scene objects - http://www.roostertailgames.com/ScheduleManager.aspx