Picture swap/ behaviour edit
by Anthony Ratcliffe · in Torque Game Builder · 05/26/2008 (11:05 pm) · 34 replies
Ok i have added an avater to my screen called class "image1"what i want to do as when my health decreases i want to change the picture to my avater looking more destressed
this is the code i have editted into the behaviour takes damage, however i am really stumped how to change my image
the names of the image are
image1 - starting image
image 2 - 90 -70% damage
image 3 - 70% - 1%
then in the spawn i want the image to reset, can anyone tell me weather i'm on the right lines and how i go about chnaging the image
if (!isObject(TakesDamageBehavior))
{
%template = new BehaviorTemplate(TakesDamageBehavior);
%template.friendlyName = "Takes Damage";
%template.behaviorType = "Game";
%template.description = "Set the object to take damage from DealsDamage objects that collide with it";
%template.addBehaviorField(health, "The amount of health the object has", int, 100);
%template.addBehaviorField(lives, "The number of times the object can lose all its health", int, 3);
%template.addBehaviorField(tintRedForDamage, "Tint the object red as it takes damage", bool, 0);
%template.addBehaviorField(respawnTime, "The time between death and respawn (seconds)", float, 2.0);
%template.addBehaviorField(invincibleTime, "The time after spawning before damage is applied (seconds)", float, 1.0);
%template.addBehaviorField(respawnEffect, "The particle effect to play on spawn", object, "", t2dParticleEffect);
%template.addBehaviorField(explodeEffect, "The particle effect to play on death", object, "", t2dParticleEffect);
}
function TakesDamageBehavior::onAddToScene(%this)
{
%this.startHealth = %this.health;
%this.startFrame = %this.owner.getFrame();
%this.spawn();
}
function TakesDamageBehavior::takeDamage(%this, %amount, %assailant)
{
if (%this.invincible)
return;
%this.health -= %amount;
//code edited
if (%this.health <90 && this.health >70)
{
change image to one where girl looks destressed
}
else if (%this.health <70 && this.health >1)
}
show next picture
{
// code edit finshed
else if (%this.health <= 0)
{
%this.explode();
%this.kill();
return;
}
if(%this.tintRedForDamage)
{
%tint = %this.health / %this.startHealth;
%this.owner.setBlendColor(1, %tint, %tint, 1);
}
}
function TakesDamageBehavior::kill(%this)
{
%this.lives--;
if (%this.lives <= 0)
{
%this.owner.safeDelete();
levelOver("game/data/levels/end.t2d");
return;
}
%this.invincible = true;
%this.owner.visible = false;
%this.owner.collisionActiveReceive = false;
%this.schedule(%this.respawnTime * 1000, "spawn");
}
function TakesDamageBehavior::setVincible(%this)
{
%this.invincible = false;
}
function TakesDamageBehavior::spawn(%this)
{
%this.owner.collisionActiveReceive = true;
%this.schedule(%this.invincibleTime * 1000, "setVincible");
%this.health = %this.startHealth;
%this.owner.setBlendColor(1, 1, 1, 1);
%this.owner.visible = true;
%this.owner.setFrame(%this.startFrame);
//code edit
ADD Code to reset image
//code edit finshed
if (%this.isPlayer == true)
%sound = alxPlay(spawnAudio);
if (isObject(%this.respawnEffect))
{
%explosion = %this.respawnEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
}
}
function TakesDamageBehavior::explode(%this)
{
if (isObject(%this.explodeEffect))
{
%explosion = %this.explodeEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
if (%this.isPlayer == true)
%sound = alxPlay(explodeAudio);
}
}
this is the code i have editted into the behaviour takes damage, however i am really stumped how to change my image
the names of the image are
image1 - starting image
image 2 - 90 -70% damage
image 3 - 70% - 1%
then in the spawn i want the image to reset, can anyone tell me weather i'm on the right lines and how i go about chnaging the image
if (!isObject(TakesDamageBehavior))
{
%template = new BehaviorTemplate(TakesDamageBehavior);
%template.friendlyName = "Takes Damage";
%template.behaviorType = "Game";
%template.description = "Set the object to take damage from DealsDamage objects that collide with it";
%template.addBehaviorField(health, "The amount of health the object has", int, 100);
%template.addBehaviorField(lives, "The number of times the object can lose all its health", int, 3);
%template.addBehaviorField(tintRedForDamage, "Tint the object red as it takes damage", bool, 0);
%template.addBehaviorField(respawnTime, "The time between death and respawn (seconds)", float, 2.0);
%template.addBehaviorField(invincibleTime, "The time after spawning before damage is applied (seconds)", float, 1.0);
%template.addBehaviorField(respawnEffect, "The particle effect to play on spawn", object, "", t2dParticleEffect);
%template.addBehaviorField(explodeEffect, "The particle effect to play on death", object, "", t2dParticleEffect);
}
function TakesDamageBehavior::onAddToScene(%this)
{
%this.startHealth = %this.health;
%this.startFrame = %this.owner.getFrame();
%this.spawn();
}
function TakesDamageBehavior::takeDamage(%this, %amount, %assailant)
{
if (%this.invincible)
return;
%this.health -= %amount;
//code edited
if (%this.health <90 && this.health >70)
{
change image to one where girl looks destressed
}
else if (%this.health <70 && this.health >1)
}
show next picture
{
// code edit finshed
else if (%this.health <= 0)
{
%this.explode();
%this.kill();
return;
}
if(%this.tintRedForDamage)
{
%tint = %this.health / %this.startHealth;
%this.owner.setBlendColor(1, %tint, %tint, 1);
}
}
function TakesDamageBehavior::kill(%this)
{
%this.lives--;
if (%this.lives <= 0)
{
%this.owner.safeDelete();
levelOver("game/data/levels/end.t2d");
return;
}
%this.invincible = true;
%this.owner.visible = false;
%this.owner.collisionActiveReceive = false;
%this.schedule(%this.respawnTime * 1000, "spawn");
}
function TakesDamageBehavior::setVincible(%this)
{
%this.invincible = false;
}
function TakesDamageBehavior::spawn(%this)
{
%this.owner.collisionActiveReceive = true;
%this.schedule(%this.invincibleTime * 1000, "setVincible");
%this.health = %this.startHealth;
%this.owner.setBlendColor(1, 1, 1, 1);
%this.owner.visible = true;
%this.owner.setFrame(%this.startFrame);
//code edit
ADD Code to reset image
//code edit finshed
if (%this.isPlayer == true)
%sound = alxPlay(spawnAudio);
if (isObject(%this.respawnEffect))
{
%explosion = %this.respawnEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
}
}
function TakesDamageBehavior::explode(%this)
{
if (isObject(%this.explodeEffect))
{
%explosion = %this.explodeEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
if (%this.isPlayer == true)
%sound = alxPlay(explodeAudio);
}
}
About the author
#2
05/26/2008 (11:22 pm)
// code edit is where i plan to put code or i've added partcial code
#3
t2dStaticSprite::setImageMap, or setFrame.
05/27/2008 (6:51 am)
Well if your avatar is a t2dStaticSprite you could uset2dStaticSprite::setImageMap, or setFrame.
#4
05/27/2008 (6:56 pm)
Setimage map? dont understand that its 3 indivgual pics
#5
You can assign the image for a staticSprite to use in the editor, or you can do the same thing in script by calling:
t2dStaticSprite::setImageMap( %imageMapObj, %frame );
If it only has one "frame" then just pass 0 or nothing as the second parameter. You use the same function either way.
05/28/2008 (10:20 am)
All images you add to TGB in the editor can be found in managedDatablocks.cs or maybe its just managed.cs. Basically each imageMap has its own datablock with a name that you gave it in the editor.You can assign the image for a staticSprite to use in the editor, or you can do the same thing in script by calling:
t2dStaticSprite::setImageMap( %imageMapObj, %frame );
If it only has one "frame" then just pass 0 or nothing as the second parameter. You use the same function either way.
#6
06/01/2008 (6:57 pm)
I'm really confused can u explain that
#7
I remove some stuff from your onAddToScene method, and I made some changes to the section you marked with "//code edited".
I assumed in this code that your frames look like this...
frame 0 = healthy
frame 1 = distressed
frame 2 = damaged
06/02/2008 (12:25 pm)
Ok it looks like you are using one imageMap with multiple frames to represent your avatar's multiple damage states. So when you take damage you want to change the frame but not the imageMap.I remove some stuff from your onAddToScene method, and I made some changes to the section you marked with "//code edited".
I assumed in this code that your frames look like this...
frame 0 = healthy
frame 1 = distressed
frame 2 = damaged
function TakesDamageBehavior::onAddToScene(%this)
{
%this.startHealth = %this.health;
%this.spawn();
}//code edited
if (%this.health <90 && this.health >70)
{
[b]%this.owner.setFrame(1)[/b]
}
else if (%this.health <70 && this.health >1)
{
[b]%this.owner.setFrame(2);[/b]
}
// code edit finished
#8
also this is the datablock i'm using
new t2dImageMapDatablock(aaaImageMap) {
imageName = "~/data/images/aaa.png";
imageMode = "CELL";
frameCount = "-1";
filterMode = "SMOOTH";
filterPad = "1";
preferPerf = "1";
cellRowOrder = "1";
cellOffsetX = "0";
cellOffsetY = "0";
cellStrideX = "0";
cellStrideY = "0";
cellCountX = "3";
cellCountY = "-1";
cellWidth = "107";
cellHeight = "98";
preload = "1";
allowUnload = "0";
so in you code %this.owner.setFrame(0)
would become
%this.aaa.setFrame(0)
other wise i cant tell how aaa became owner, because this.owner was already part of the behaviour
thanks ant
06/02/2008 (9:28 pm)
Ah i see so if add them to one image and cell split them then set frames ya?also this is the datablock i'm using
new t2dImageMapDatablock(aaaImageMap) {
imageName = "~/data/images/aaa.png";
imageMode = "CELL";
frameCount = "-1";
filterMode = "SMOOTH";
filterPad = "1";
preferPerf = "1";
cellRowOrder = "1";
cellOffsetX = "0";
cellOffsetY = "0";
cellStrideX = "0";
cellStrideY = "0";
cellCountX = "3";
cellCountY = "-1";
cellWidth = "107";
cellHeight = "98";
preload = "1";
allowUnload = "0";
so in you code %this.owner.setFrame(0)
would become
%this.aaa.setFrame(0)
other wise i cant tell how aaa became owner, because this.owner was already part of the behaviour
thanks ant
#9
%this.owner is the object owning the behavior. I assume, that is a t2dStaticSprite.
SetFrame is called on the t2dStaticSprite. If you look in the class reference documentation, you can see that setFrame is a method of t2dStaticSprite and NOT of imageMap.
ImageMap(s) can be used by more than one sprite at the same time, so that is why you wouldn't set the frame there.
06/02/2008 (9:50 pm)
No, you should use the code as I typed it.%this.owner is the object owning the behavior. I assume, that is a t2dStaticSprite.
SetFrame is called on the t2dStaticSprite. If you look in the class reference documentation, you can see that setFrame is a method of t2dStaticSprite and NOT of imageMap.
ImageMap(s) can be used by more than one sprite at the same time, so that is why you wouldn't set the frame there.
#10
06/03/2008 (11:02 pm)
This is not the image the behavior will be on, when my main charecter is damaged the picture is changed so i want to change the picture within the health behaviour of the main char
#11
06/03/2008 (11:46 pm)
Maybe you should re-read my last post, I already explained that.
#12
i'll try to explain i have an animated figure when he is hit by the enemy the picture were changing is the health bar her face changes when hit, so the behaviour is on the animated figure not on the picture were changing, so surley the animated figure would be this.owner not the picture were chnaging which has no behaviour set
06/04/2008 (8:34 pm)
Yes the problem is i dont understand what you wrote, also i've done as you said and its not working,i'll try to explain i have an animated figure when he is hit by the enemy the picture were changing is the health bar her face changes when hit, so the behaviour is on the animated figure not on the picture were changing, so surley the animated figure would be this.owner not the picture were chnaging which has no behaviour set
#13
http://www.mediafire.com/?ztr4yr34cxg
06/04/2008 (8:46 pm)
I've uploaded this incase anyone could help only diffrence is insted of a animated sprite with the beaviour attached i've used a place holder world imagehttp://www.mediafire.com/?ztr4yr34cxg
#14
06/04/2008 (8:47 pm)
Quick edit theres an error line 28 cant work out why, also cant get the steamer to change level once it hits it world limit
#15
You can give that object a name (which you can do in the editor), and then call setFrame on it.
06/04/2008 (9:17 pm)
Are you saying the image you want to change is a different object than the avatar itself? Like a small mugshot picture of the character like in wolfenstein or doom for example?You can give that object a name (which you can do in the editor), and then call setFrame on it.
#16
06/04/2008 (9:28 pm)
Anthony, I think that you should go over the basic TGB tutorials, as I suspect that you have not. These tutorials explain the sorts of problems that you're facing in detail.
#17
yes james i think we understand each other now, so basically when my charecter recieves damage the avater changes to diffrent expressions, i'll quickly try that then post results
06/04/2008 (10:27 pm)
I do a games development course at university on tgb and tge so the basics have been covered.yes james i think we understand each other now, so basically when my charecter recieves damage the avater changes to diffrent expressions, i'll quickly try that then post results
#18
06/04/2008 (10:31 pm)
Ok changed this.owner to this.aaa and name of the avater to aaa aswell no luck so far
#19
ok so this is the errors i'm recieving as of now, i also dont understand why it cannot find the function levelover that was declared in game.cs (see above posts for game.cs
06/04/2008 (10:38 pm)
C:/Users/Ant/Desktop/platformer images and ideas/game/platformer/game/behaviors/takedamage.cs Line: 28 - parse error
>>> Advanced script error report. Line 28.
>>> Some error context, with ## on sides of error halt:
%template.addBehaviorField(explodeEffect, "The particle effect to play on death", object, "", t2dParticleEffect);
}
function TakesDamageBehavior::onAddToScene(%this)
{
%this.startHealth = %this.health;
%this.spawn();
}
function TakesDamageBehavior::takeDamage(%this, %amount, %assailant)
if (%this.invincible)
##{##
return;
%this.health -= %amount;
}
else if (%this.health <90 && this.health >70)
{
%this.aaa.setFrame(1)
}
else if (%this.health <70 && this.health >1)
}
%this.aaa.setFrame(2);
{
else if (%this.health <= 0)
{
%this.explode();
%this.kill();
>>> Error report complete.
Activating DirectInput...
DirectInput joystick failed to enable!
Compiling C:/Users/Ant/Desktop/platformer images and ideas/game/platformer/game/data/levels/level1.t2d...
Loading compiled script C:/Users/Ant/Desktop/platformer images and ideas/game/platformer/game/data/levels/level1.t2d.
t2dStaticSprite::setFrame() - Cannot set Frame without existing t2dImageMapDatablock Datablock!
game/behaviors/game/takesDamage.cs (56): Unable to find function levelOver
DirectInput deactivated.
Shutting down the OpenGL display device...
Making the GL rendering context not current...
Deleting the GL rendering context...
Releasing the device context...ok so this is the errors i'm recieving as of now, i also dont understand why it cannot find the function levelover that was declared in game.cs (see above posts for game.cs
#20
this is the behaviour script i'm using
06/04/2008 (10:43 pm)
if (!isObject(TakesDamageBehavior))
{
%template = new BehaviorTemplate(TakesDamageBehavior);
%template.friendlyName = "Takes Damage";
%template.behaviorType = "Game";
%template.description = "Set the object to take damage from DealsDamage objects that collide with it";
%template.addBehaviorField(health, "The amount of health the object has", int, 100);
%template.addBehaviorField(lives, "The number of times the object can lose all its health", int, 3);
%template.addBehaviorField(tintRedForDamage, "Tint the object red as it takes damage", bool, 0);
%template.addBehaviorField(respawnTime, "The time between death and respawn (seconds)", float, 2.0);
%template.addBehaviorField(invincibleTime, "The time after spawning before damage is applied (seconds)", float, 1.0);
%template.addBehaviorField(respawnEffect, "The particle effect to play on spawn", object, "", t2dParticleEffect);
%template.addBehaviorField(explodeEffect, "The particle effect to play on death", object, "", t2dParticleEffect);
}
function TakesDamageBehavior::onAddToScene(%this)
{
%this.startHealth = %this.health;
%this.spawn();
}
function TakesDamageBehavior::takeDamage(%this, %amount, %assailant)
if (%this.invincible)
{
return;
%this.health -= %amount;
}
else if (%this.health <90 && this.health >70)
{
%this.owner.setFrame(1)
}
else if (%this.health <70 && this.health >1)
}
%this.owner.setFrame(2);
{
else if (%this.health <= 0)
{
%this.explode();
%this.kill();
return;
}
if(%this.tintRedForDamage)
{
%tint = %this.health / %this.startHealth;
%this.owner.setBlendColor(1, %tint, %tint, 1);
}
}
function TakesDamageBehavior::kill(%this)
{
%this.lives--;
if (%this.lives <= 0)
{
%this.owner.safeDelete();
levelOver("game/data/levels/end.t2d");
return;
}
%this.invincible = true;
%this.owner.visible = false;
%this.owner.collisionActiveReceive = false;
%this.schedule(%this.respawnTime * 1000, "spawn");
}
function TakesDamageBehavior::setVincible(%this)
{
%this.invincible = false;
}
function TakesDamageBehavior::spawn(%this)
{
%this.owner.collisionActiveReceive = true;
%this.schedule(%this.invincibleTime * 1000, "setVincible");
%this.health = %this.startHealth;
%this.owner.setBlendColor(1, 1, 1, 1);
%this.owner.visible = true;
%this.owner.setFrame(%this.startFrame);
if (%this.isPlayer == true)
%sound = alxPlay(spawnAudio);
if (isObject(%this.respawnEffect))
{
%explosion = %this.respawnEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
}
}
function TakesDamageBehavior::explode(%this)
{
if (isObject(%this.explodeEffect))
{
%explosion = %this.explodeEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
if (%this.isPlayer == true)
%sound = alxPlay(explodeAudio);
}
}this is the behaviour script i'm using
Torque Owner Anthony Ratcliffe