Fish Unable to Gain Health
by Julian Whatley · in Technical Issues · 06/09/2009 (2:56 pm) · 12 replies
Hello,
In the fish game tutorial (part 6), I am supposed to program it so that the player-controlled fish gains health every time he collides with the fish food bubble. While the fish food bubble disappears and respawns when the fish collides with, it provides the fish with no health. Instead, he eventually dies due to health drain, no matter how many fish food bubbles he collides with. Any suggestions on how to solve this problem would greatly be appreciated. Here is my fishfood.cs script file (please let me know if there is anything wrong with it):
function FishFood::onLevelLoaded(%this, %scenegraph)
{
%this.startPositionY = %this.getPositionY();
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function FishFood::onWorldLimit(%this, %mode, %limit)
{
if(%limit $= "bottom")
{
%this.spawn();
}
}
function FishFood::spawn(%this)
{
%this.setPosition(getRandom(-50, 50), %this.startPositionY);
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifyLife(%srcObj.lifeValue);
} else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}
Thanks!
In the fish game tutorial (part 6), I am supposed to program it so that the player-controlled fish gains health every time he collides with the fish food bubble. While the fish food bubble disappears and respawns when the fish collides with, it provides the fish with no health. Instead, he eventually dies due to health drain, no matter how many fish food bubbles he collides with. Any suggestions on how to solve this problem would greatly be appreciated. Here is my fishfood.cs script file (please let me know if there is anything wrong with it):
function FishFood::onLevelLoaded(%this, %scenegraph)
{
%this.startPositionY = %this.getPositionY();
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function FishFood::onWorldLimit(%this, %mode, %limit)
{
if(%limit $= "bottom")
{
%this.spawn();
}
}
function FishFood::spawn(%this)
{
%this.setPosition(getRandom(-50, 50), %this.startPositionY);
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifyLife(%srcObj.lifeValue);
} else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}
Thanks!
#2
Thanks.
06/16/2009 (11:33 am)
Thanks for your suggestion. I did what you told me, but unfortunately I am still stuck. Do you have any other suggestions?Thanks.
#3
06/17/2009 (3:53 pm)
Try flipping the %srcObj.spawn(); and the %dstObj.modifyLife(%srcObj.lifeValue); functions. My guess is that the code is deleting the fish food before it heals the fish.
#4
06/30/2009 (1:43 pm)
I tried that, but it still does not work. Any more suggestions?
#5
The key is understanding this line of code. WHAT is the Name of your FISH? if it is PlayerFish you will heal if it is not you will not.
if(%dstObj.class $= "PlayerFish")
07/06/2009 (5:30 am)
according to what I have read. The key is understanding this line of code. WHAT is the Name of your FISH? if it is PlayerFish you will heal if it is not you will not.
if(%dstObj.class $= "PlayerFish")
#6
07/06/2009 (9:07 am)
Thanks for trying to help. It is named PlayerFish. Any other ideas?
#7
{
%srcObj.spawn();
%dstObj.modifyLife(%srcObj.lifeValue);
}
in this if statement I would add a number of echos, try echoing %srcobj.lifeValue for example. I don't see life value being set anywhere so its possible that it isn't, I've not done this tutorial though so I could be misinformed.
It is also possible the issue is with the modifyLife function, it could be that all of this is working right but there is an issue there causing it to not be properly modified.
07/06/2009 (1:30 pm)
if(%dstObj.class $= "PlayerFish"){
%srcObj.spawn();
%dstObj.modifyLife(%srcObj.lifeValue);
}
in this if statement I would add a number of echos, try echoing %srcobj.lifeValue for example. I don't see life value being set anywhere so its possible that it isn't, I've not done this tutorial though so I could be misinformed.
It is also possible the issue is with the modifyLife function, it could be that all of this is working right but there is an issue there causing it to not be properly modified.
#8
07/06/2009 (1:59 pm)
How do you echo?
#9
Put that line inside the if statement and every time you collide with food if your class is playerfish it will echo out something along the lines of:
life value = 50
Whatever value %srcObj.lifevalue has will go where I put 50 as a sample number.
07/07/2009 (11:41 am)
echo("life value = " @ %srcObj.lifeValue);Put that line inside the if statement and every time you collide with food if your class is playerfish it will echo out something along the lines of:
life value = 50
Whatever value %srcObj.lifevalue has will go where I put 50 as a sample number.
#10
function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifyLife echo("life value = " @ %srcObj.lifeValue); ## ## } else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}
Thanks again!
07/15/2009 (4:02 pm)
Thanks for the suggestion. I put in the echo and sure enough, I found what line of code is faulty. The problem is, I still don't know exactly WHAT IS wrong with it. Could you help me? Here is the line of code (with ## ## used to indicate the faulty line):function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifyLife echo("life value = " @ %srcObj.lifeValue); ## ## } else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}
Thanks again!
#11
function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifyLife(%srcObj.lifeValue);
echo("life value = " @ %srcObj.lifeValue);
} else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}
07/17/2009 (11:25 am)
Try this, let me know how it goes:function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifyLife(%srcObj.lifeValue);
echo("life value = " @ %srcObj.lifeValue);
} else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}
#12
Thanks again!
07/19/2009 (10:45 am)
Thanks for the suggestion. Unfortunatley, it still did not not work. Your thoughts?Thanks again!
Torque 3D Owner Joshua Janssen