Game Development Community

Controlling multiple sprites

by Andrew Bangs · in Torque Game Builder · 12/01/2005 (1:50 pm) · 2 replies

Hi,
Is there an easy way to run a command (such as setVisible(false) ) on all objects in the same Group? I've got a scene with several NPCs in it (and theoretically, I may have several towns), all of which I want to either hide or remove/delete (haven't really decided yet, but for now I'm going with hide) when it switches to combat mode.

function setupCombat(%opponent, %zone)
{
 $fighting = true;
 if (%zone $= "grass") {
	   drawWorld("battle");
	}
	alxStop($mainAmbientAudio);
	$mainCombatAudio = alxPlay( combatAudio );
	
	// Delete, or make hidden, all other sprites
	// That is, delete everything whose group matches $spriteCollisionGroup
	$player.setVisible(false);       // THIS WORKS
	$npc.setVisible(false);           // THIS WORKS (but only deletes the most recent NPC made (makes sense))
	$town.setVisible(false);        // THIS WORKS
	$spriteCollisionGroups.setVisible(false);  // THIS DOESN'T

Error:
pathname/combat.cs (28): Unknown command setVisible.
     Object LoadTextProfile(1090) GuiControlProfile -> SimObject

I know I've asked this before, but I can't remember the answer: Is there a way to loop through all the game objects (so that I can compare the Group parameter of each)? That's one way to do this. I also could put all my NPCs (and towns) on the scene into a structure, and then set the whole struct to hidden (but I don't know how to do that... might not even be possible).

Any help you guys could provide would be appreciated.

#1
12/01/2005 (4:18 pm)
Well, I searched a little more on the forums here, and found that if I keep all my sprites in an array when I make them, I can loop through that to set them invisible.

$town = new fxStaticSprite2D() { scenegraph = mainSceneGraph2D; };
	$spriteList[$spriteCounter++] = $town;

then

for ($i = 0; $i < $spriteCounter; $i++) {
	   echo ("spritelist[i] is: " SPC $spriteList[$i] SPC "and i is: " SPC $i);
	   $spriteList[$i].setVisible(false);
}

Make sure you reset $spriteCounter whenever necessary.

Good luck to anyone who finds this :)
#2
12/01/2005 (7:41 pm)
You could use a SimSet()

something like this

new SimSet(SpriteList);

$town = new fxStaticSprite2D() { scenegraph = mainSceneGraph2D; };

SpriteList.add($town);

to iterate through it you can do this

%count = SpriteList.getCount();

for(%i=0;%i<%count;%i++)
{
   %obj = SpriteList.getObject(%i);
   %obj.setVisible(false);
}