Game Development Community

Changing Animation

by Rachel P · in Torque Game Builder · 05/07/2009 (9:16 pm) · 5 replies

Hey all! Should be a simple question...

I simply want to change a sprite's animation on collision. I have this bit of code I'm trying, but it doesn't seem to be working.



function playerClass::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
// Collision with Path
if(distObj.class $= "pathClass")
{
%this.playAnimation(pathChange);
}
}


I actually want the "pathClass" animation to change (when playerClass colides into it), so not sure if I should do %pathClass.playAnimation(pathChange);

Regardless, it isn't working.

What am I missing here? Changing an animation of a sprite shouldn't be too hard! :P

#1
05/07/2009 (9:59 pm)
One possible problem is right here:

%this.playAnimation(pathChange);

%this is undefined, you should call play animation on %dstObj, so change the code to:

%dstObj.playAnimation(pathChange);

Give that a try, I hope it works for you.
#2
05/08/2009 (2:34 pm)
Thanks for the suggestion, Darlus! Unfortunately, it still doesn't work... but I was able to find a typo in my code that I fixed (o put "distObj" once instead of "dstObj"). Hmm... I wonder what I'm doing wrong?
Just to make sure I have the collision settings for each object correct, here's what I have checked (in the "Edit" menu of TGB)

Send Collision
Receive Collision
Callback


Is that all that would be needed to activate the script? If so, then I guess there is still something wrong with the coding. FYI, I have this code in a "collision.cs" file which I call upon in the main.cs file by using the exec script. I see in the console that it loads the file, so I guess there is just something wrong with the collision script...
#3
05/08/2009 (2:49 pm)
You would also need to make sure that you had receive collision set in the object you were trying to collide with.

One thing you might consider, is putting an echo statement in your onCollision method. That way you'll know if your on collision method is even being called.

Just a line like:

echo("playerClass collision");

Then if you see that printed in the console, you know that your collision method is getting called.
#4
05/08/2009 (3:03 pm)
Ah, that echo statement can come in handy! But guess what? I was able to get it to work!!
I actually had to change the code so the focus was on the path object (as opposed to the player), but once I did I got it to work!



function pathClass::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
// Collision with Path
if(%dstObj.class $= "playerClass")
{
%srcObj.playAnimation(pathChange);
}

}

Guess I had to refer to it as %srcObj as well. Well, I'm so happy it's working now! Thanks for your help, Darlus!
#5
05/08/2009 (3:27 pm)
You're welcome, I'm glad it's working for you.