Game Development Community

TGB Sprite Help

by Mike Q · in Technical Issues · 11/24/2008 (3:16 pm) · 6 replies

Does anybody know how to swap sprites during gameplay? I'm making a game where the avatar shoots paintballs and the paintball changes the enemies color when it hits them. The way i wanted to do this was to swap sprites when the paintball collided with the enemy collision box. Does anyone know how i can go about doing this?

About the author


#1
11/24/2008 (3:30 pm)
If it's a static sprite, then you can use setImageMap( ) calls on the sprite to change to a new image. If it's an animated sprite, then you can use playAnimation( ).
#2
11/24/2008 (4:03 pm)
If you just want to change the color, I suggest you play around with the setBlendColor(); function. But as far as changing sprites, the playAnimation( ); is the way to go.
#3
12/02/2008 (10:48 am)
Can you show me an example of how to properly use the playAnimation( ) function? I've been trying for a couple of days and I can quite figure it out. Like say I have a character that has one animated sprite when he's stagnant and when you press a key to move a different one is activated. Can someone show me an example of how I would implement the playAnimation( ) function in that situation?
#4
12/04/2008 (12:25 pm)
Sure. Simply type the name of the animation in the parenthesis, like this:
%this.playAnimation();

The name of the animation is whatever TGB calls it. Usually something like "playerAnimation" or "bad_guyAnimation".
#5
12/05/2008 (12:34 pm)
Argh! I still cant figure this out. Okay my gameScript for the player movement looks as follows:


function PlayerFish::onLevelLoaded(%this, %scenegraph)

function PlayerFish::updateMovement(%this)
{
   if(%this.moveLeft)
   {
      $FishPlayer.setFlipX(true);
      $FishPlayer.setLinearVelocityX( -$FishPlayer.hSpeed );

   %this.playAnimation(<name of animation>);
   }

   if(%this.moveRight)
   {
      $FishPlayer.setFlipX(false);
      $FishPlayer.setLinearVelocityX( $FishPlayer.hSpeed );
   }

   if(%this.moveUp)
   {
      %this.setLinearVelocityY( -$FishPlayer.vSpeed );
   }


   if(%this.moveDown)
   {
      %this.setLinearVelocityY( $FishPlayer.vSpeed );
   }


   if(!%this.moveLeft && !%this.moveRight)
   {
      %this.setLinearVelocityX( 0 );
   }


   if(!%this.moveUp && !%this.moveDown)
   {
      %this.setLinearVelocityY( 0 );
   }



}








{
   $FishPlayer = %this;


   moveMap.bindCmd(keyboard, "w", "fishPlayerUp();", "fishPlayerUpStop();");
   moveMap.bindCmd(keyboard, "s", "fishPlayerDown();", "fishPlayerDownStop();");
   moveMap.bindCmd(keyboard, "a", "fishPlayerLeft();", "fishPlayerLeftStop();");
   moveMap.bindCmd(keyboard, "d", "fishPlayerRight();", "fishPlayerRightStop();");

   moveMap.bindCmd(keyboard, "space", "fishPlayerBoost();", "fishPlayerBoostStop();");

}








function fishPlayerLeft()
{
   $FishPlayer.setFlipX(true);
   $FishPlayer.setLinearVelocityX( -$FishPlayer.hSpeed );
}

function fishPlayerUp()
{
   $FishPlayer.setLinearVelocityY( -$FishPlayer.vSpeed );
}


function fishPlayerDown()
{
   $FishPlayer.setLinearVelocityY( $FishPlayer.vSpeed );
}


function fishPlayerRight()
{
   $FishPlayer.setFlipX(false);
   $FishPlayer.setLinearVelocityX( $FishPlayer.hSpeed );
}




function fishPlayerUpStop()
{
   $FishPlayer.setLinearVelocityY( 0 );
}


function fishPlayerDownStop()
{
   $FishPlayer.setLinearVelocityY( 0 );
}


function fishPlayerLeftStop()
{
   $FishPlayer.setLinearVelocityX( 0 );
}


function fishPlayerRightStop()
{
   $FishPlayer.setLinearVelocityX( 0 );
}







function fishPlayerBoost()
{
   %flipX = $FishPlayer.getFlipX();


   if(!%flipX)
   {
      %hSpeed = $FishPlayer.hSpeed * 3;
   } else
   {
      %hSpeed = -$FishPlayer.hSpeed * 3;
   }


   $FishPlayer.setLinearVelocityX(%hSpeed);
}


function fishPlayerBoostStop()
{
   $FishPlayer.setLinearVelocityX(0);
}

where in this code would i insert the

%this.playAnimation(<name of animation>);

lines? the three animations i am going to have are named:


PlayerStagnant
PlayerSwimming
PlayerShooting


the PlayerStagnant is the default animated sprite that shows up when the game starts and the player isn't moving or shooting. The PlayerSwimming is the sprite i want activated and looping when the player holds one of the moving keys and stops playing when the keys are let go. And the PlayerShooting is the sprite i want activated whenever the player presses any of the degsignated shooting keys...say "C key," "V key," and "B key."

What lines should I add too make this happen and where do I add them?
#6
12/05/2008 (12:56 pm)
I'll assume your first line is a bad copy+paste and move on in to the updateMovement(); function.

Where you have Jeffrey's sample code is the right place if you want to change the animate on move left, but that line isn't going to work as it is. You'll need to define an animation by either adding it in the builder or loading it in a datablock, then replace "" with the name you gave the animation.