Game Development Community

Problem with New t2dStaticSprite()

by Matthew M. White · in Torque Game Builder · 05/08/2010 (4:22 pm) · 13 replies

Hey there folks,

I know I'm doing something wrong here, I'm just not exactly sure what it is.
These objects spawn (at least according to the console), but they never appear... do you guys have any idea why?

function SpawnTraffic(%CarType, %RoadSide) // as integers.  CarType = 1 to 8, Roadside 1=left, 2=right
{
	//Don't spawn more than one car on one side of the road
	//at a time.  Would break the game.
	if (%RoadSide == 1 && $CarsOnLeft >= 1)
	{	
		echo("too many cars on left, not spawning");
		return;
	}
	
	if (%RoadSide == 2 && $CarsOnRight >= 1)
	{
		 echo("too many cars on right, not spawning");
		 return;
	}

		echo("Spawntraffic()");
		
	if (%RoadSide == 1)
	{
		$CarsOnLeft++;
		%this.spawnX = 3;
		%this.spawnY = -31;
	}
	else 
	{
		$CarsOnRight++;
		%this.spawnX = 10;
		%this.spawyY = -31;
	}
	
	
	
	switch (%CarType)
	{
		case 1:
		//Classic Car
		echo("Spawning a classic car...");
		   $GreenSUVObject = new t2dStaticSprite() {
		   imageMap = "Car2ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   
		case 2:
		//Green SUV
		echo("Spawning a Green SUV...");
		   $GreenSUVObject = new t2dStaticSprite() {
		   imageMap = "Car2ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		
		case 3:
		//Transport
		echo("Spawning a Transport...");
		   $TransportObject = new t2dStaticSprite() {
		   imageMap = "Car3ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   
		case 4:
		//Green Pickup
		echo("Spawning a Green Pickup...");
		   $GreenPickupObject = new t2dStaticSprite() {
		   imageMap = "Car4ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   
		case 5:
		//BlueSedan
		echo("Spawning a Blue Sedan...");
		   $BlueSedanObject = new t2dStaticSprite() {
		   imageMap = "Car5ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   
		case 6:
		//Orange SUV
		echo("Spawning an orange SUV...");
		   $OrangeSUVObject = new t2dStaticSprite() {
		   imageMap = "Car6ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   
		case 7:
		//Red Pickup
		echo("Spawning a Red Pickup...");
		   $RedPickupObject = new t2dStaticSprite() {
		   imageMap = "Car7ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   
		case 8:
		//Red Sedan	
		   echo("Spawning a Red Sedan...");
		   $RedSedanObject = new t2dStaticSprite() {
		   imageMap = "Car8ImageMap";
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %this.spawnX SPC %this.spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
	}
	
	
	
}

Thanks in advance for any help.

#1
05/08/2010 (4:28 pm)
You have to set the scenegraph for each sprite (made this mistake many times myself)

$GreenSUVObject = new t2dStaticSprite() {  
  scenegraph = %this.scengraph;
  imageMap = "Car2ImageMap";  
  ...

#2
05/08/2010 (4:37 pm)
Also... I recommend you do something like this:

$GreenSUVObject = createCar("Car2ImageMap");
$TransportObject = createCar("Car3ImageMap");
...

function createCar(%carimage)
{
   %car = new t2dStaticSprite() {
      scenegraph = %this.scenegraph;  
      imageMap = %carimage;  
      frame = "0";  
      mUseSourceRect = "0";  
      sourceRect = "0 0 0 0";  
      canSaveDynamicFields = "1";  
      class = "TrafficClass";  
      UseMouseEvents = "1";  
      Position = %this.spawnX SPC %this.spawnY;  
      size = "5 5";  
      Layer = "20";  
      BlendIgnoreTextureAlpha = "0";  
      mountID = "2";  
   };  

   return %car;
}

You'll save yourself a ton of work in the long run.
#3
05/09/2010 (3:41 pm)
What's odd here is I made your suggested changes, and it still doesn't seem to spawn... I have no idea what the problem is.. here's the whole stinking code.

function SpawnTraffic(%CarType, %RoadSide) // as integers.  CarType = 1 to 8, Roadside 1=left, 2=right
{
	//Don't spawn more than one car on one side of the road
	//at a time.  Would break the game.
	if (%RoadSide == 1 && $CarsOnLeft >= 1)
	{	
		echo("too many cars on left, not spawning");
		return;
	}
	
	if (%RoadSide == 2 && $CarsOnRight >= 1)
	{
		 echo("too many cars on right, not spawning");
		 return;
	}

		echo("Spawntraffic()");
		
	if (%RoadSide == 1)
	{
		$CarsOnLeft++;
		%this.spawnX = 3;
		%this.spawnY = -31;
	}
	else 
	{
		$CarsOnRight++;
		%this.spawnX = 10;
		%this.spawyY = -31;
	}
	
	
	
	switch (%CarType)
	{
		case 1:
		//Classic Car
		$ClassicCarObject = createCar("Car1ImageMap", %this.spawnX, %this.spawnY);
		   
		case 2:
		//Green SUV
		$GreenSUVObject = createCar("Car2ImageMap", %this.spawnX, %this.spawnY);
		
		case 3:
		//Transport
		$TransportObject = createCar("Car3ImageMap", %this.spawnX, %this.spawnY);
		
		case 4:
		//Green Pickup
		$GreenPickupObject = createCar("Car4ImageMap", %this.spawnX, %this.spawnY);
		   
		case 5:
		//BlueSedan
		$BlueSedanObject = createCar("Car5ImageMap", %this.spawnX, %this.spawnY);
		   
		case 6:
		//Orange SUV
		$OrangeSUVObject = createCar("Car6ImageMap", %this.spawnX, %this.spawnY);
		   
		case 7:
		//Red Pickup
		$RedPickupObject = createCar("Car7ImageMap", %this.spawnX, %this.spawnY);
		   
		case 8:
		//Red Sedan	
		$RedSedanObject = createCar("Car8ImageMap", %this.spawnX, %this.spawnY);
		
	}
	
	
	
}

function createCar(%carimage, %spawnX, %spawnY)
{
	echo("Spawning a car...");
		   %car = new t2dStaticSprite() {
		   scenegraph = %this.scenegraph;
		   imageMap = %carimage;
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %spawnX SPC %spawnY;
		   size = "5 5";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
}

It says it's spawning them
Then I get the output of _behavior0
and... then nothing.

M
#4
05/09/2010 (5:55 pm)
What is %this? Is %this.scenegraph really the scenegraph? You assign %this.spawnX and %this.spawnY which will work, but %this can't be the scenegraph because its not being passed into the function. Use whatever global name you have for the scenegraph such as:

scenegraph = $MySceneGraph;

Also don't forget to return %car at the end of createCar()

function createCar(%carimage, %spawnX, %spawnY)  
{  
...
   return %car;
}


#5
05/10/2010 (3:59 pm)
You know, I checked this today and set it to $MainSceneGraph, which is called by another onLevelLoaded
(i.e.: $MainSceneGraph = %this.scenegraph from that onLevelLoaded, I made sure it works with an echo)...

And they're STILL not spawning. I think I'm really screwing something up.

edit:
OHHH I DIDN'T CHANGE RETURN CAR, I'll try that out now
#6
05/10/2010 (4:17 pm)
Update:
I changed return car and it's =still= not returning an object.
I must be doing something tragically stupid.
#7
05/10/2010 (6:05 pm)
Post your latest code.
#8
05/10/2010 (6:10 pm)
Try:
$MainSceneGraph = %this.getSceneGraph();
#9
05/11/2010 (3:48 pm)
Hey again...
This is really a lingering problem for me for some reason that I know will be really obvious.
I tried implementing the %this.getSceneGraph(); function but it hasn't worked. There's a script that's called right at the beginning where everything is loaded, and I've set this as one of the globals.. Still no change.

For reference, here is the latest code... sorry for long code dump.

function PlayScreenBGClass::onTimer(%this)
{
	//Generate level of randomness
	%this.MaxNumber = 100 - $DifficultyFactor;
	
	//Generate a random piece of traffic based on current difficulty
	%this.randomTrafficSeed = getRandom(1, %this.MaxNumber); 
	%this.roadSide = getRandom(1,2); 
	
	switch (%this.randomTrafficSeed)
	{
		case range(%this.randomTrafficSeed, 1, 8):
			//Spawn Hardest Car (transport truck)
			SpawnTraffic(3, %this.roadSide);
			
		case range(%this.randomTrafficSeed, 9, 18):
			//Spawn Very Hard Car (Green Pickup)
			SpawnTraffic(4, %this.roadSide);
			
		case range(%this.randomTrafficSeed, 19, 30):
			//Spawn Very Hard Car (Classic Car)
			SpawnTraffic(1, %this.roadSide);
			
		case range(%this.randomTrafficSeed, 31, 41):
			//Spawn hard car (Red Pickup)
			SpawnTraffic(8, %this.roadSide);
			
		case range(%this.randomTrafficSeed, 42, 52):
			//Spawn medium car (Orange SUV)
			SpawnTraffic(6, %this.roadSide);
		
		case range(%this.randomTrafficSeed, 42, 52):
			//Spawn Medium Car (Green SUV)
			SpawnTraffic(2, %this.roadSide);
		
		case range(%this.randomTrafficSeed, 53, 75):
			//Spawn Easy Car (Blue Sedan)
			SpawnTraffic(5, %this.roadSide);
			
		case range(%this.randomTrafficSeed, 76, 100):
			//Spawn Easy Car (Red Sedan)
			SpawnTraffic(9, %this.roadSide);
			
	}
}

//range function for select case
function range(%value, %min, %max)
{
	if ((%value <= %max) && (%value >= %min))
		return %value;
}

function SpawnTraffic(%CarType, %RoadSide) // as integers.  CarType = 1 to 8, Roadside 1=left, 2=right
{
	//Don't spawn more than one car on one side of the road
	//at a time.  Would break the game.
	if (%RoadSide == 1 && $CarsOnLeft >= 1)
	{	
		echo("too many cars on left, not spawning");
		return;
	}
	
	if (%RoadSide == 2 && $CarsOnRight >= 1)
	{
		 echo("too many cars on right, not spawning");
		 return;
	}

		echo("Spawntraffic()");
		
	if (%RoadSide == 1)
	{
		$CarsOnLeft++;
		%spawnX = 3;
		%spawnY = -31;
	}
	else 
	{
		$CarsOnRight++;
		%spawnX = 10;
		%spawyY = -31;
	}
	
	
	
	switch (%CarType)
	{
		case 1:
		//Classic Car
		$ClassicCarObject = createCar("Car1ImageMap", %spawnX, %spawnY);
		   
		case 2:
		//Green SUV
		$GreenSUVObject = createCar("Car2ImageMap", %spawnX, %spawnY);
		
		case 3:
		//Transport
		$TransportObject = createCar("Car3ImageMap", %spawnX, %spawnY);
		
		case 4:
		//Green Pickup
		$GreenPickupObject = createCar("Car4ImageMap", %spawnX, %spawnY);
		   
		case 5:
		//BlueSedan
		$BlueSedanObject = createCar("Car5ImageMap", %spawnX, %spawnY);
		   
		case 6:
		//Orange SUV
		$OrangeSUVObject = createCar("Car6ImageMap", %spawnX, %spawnY);
		   
		case 7:
		//Red Pickup
		$RedPickupObject = createCar("Car7ImageMap", %spawnX, %spawnY);
		   
		case 8:
		//Red Sedan	
		$RedSedanObject = createCar("Car8ImageMap", %spawnX, %spawnY);
		
	}
	
	
	
}

function createCar(%carimage, %spawnX, %spawnY)
{
	echo("Spawning a car at " @ %spawnX @ ", " @ %spawnY);
		   %car = new t2dStaticSprite() {
		   scenegraph = $MainSceneGraph;
		   imageMap = %carimage;
		   frame = "0";
		   mUseSourceRect = "0";
		   sourceRect = "0 0 0 0";
		   canSaveDynamicFields = "1";
		   class = "TrafficClass";
		   UseMouseEvents = "1";
		   Position = %spawnX SPC %spawnY;
		   size = "20.000 20.000";
		   Layer = "20";
		   BlendIgnoreTextureAlpha = "0";
		   mountID = "2";
		   };
		   return %car;
}

function TrafficClass::onLevelLoaded(%this, %scenegraph)
{
	%sizex = %this.getSizeX();
	%sizey = %this.getSizeY();
	%posX = %this.getPositionX();
	%posY = %this.getPositionY();
	%img = %this.imageMap();
	
	echo("Traffic spawned at " @ %posX @ ", " @ %posY);
	echo("Size of " @ %sizeX @ ", " @ %sizeY);
	echo("with imagemap " @ %img);
}

Any help is wonderfully appreciated. You guys are great.
#10
05/11/2010 (5:01 pm)
1. One thing about OnLevelLoaded is I'm not sure how useful that is after the level is loaded. I would put this code in OnAdd() instead.

2. Try putting your cars on layer 0 just to see if maybe something else is covering them.

3. if that doesn't help, I would create an OnUpdate callback for your TrafficClass and then echo getPosition to see what it reports.

#11
05/17/2010 (2:53 pm)
Are you adding it to the scenegraph?

$mySceneGraph.addToScene(%obj);

where "$mySceneGraph" is the scenegraph you're using. I declare it in a singleton-like object, such as:
function Player::onLevelLoaded(%this,%scenegraph){
...
$mySceneGraph = %scenegraph;
...
}

?

I was having a similar problem, where they would be 'physically' created, where echo(%obj) would return it's number but I couldn't see them.
#12
05/18/2010 (5:48 am)
I agree with Josiah. The scenegraph is the most likely culprit here.
#13
08/10/2010 (9:15 pm)
I had a similar problem that turned out to be something really simple: the image was too small to be noticeable. Maybe your image was too small and might even blend with the background, making it hard to see.