Game Development Community

Need help with scripting

by Nils Nansen · in General Discussion · 11/14/2009 (10:09 pm) · 0 replies

Hi

I'm new to Torque Game Builder and C# programming. Never the less I'm trying to build a simple game but I need a little help with the scripting. Maybe someone are kind enough to give me a nudge in the right direction:-)

First of I have a problem with dealing damage on a sprite. I'm using the templates dealsDamage and takesDamage.cs from BehaviorPlayground (TorqueGameBuilder-1.7.4/games/Behavior/Playground/game/behaviors/game). They work great as is, but I've made a sprite which punches when dealing damage (I've used playerClass.cs (source code on the bottom (need help for that too:-P )) and therefore I want the sprite to only deal damage when in the "%this.playAnimation(manPunch)" state (defined in playerClass.cs). I have been trying to wrap the code that are inside dealsDamage.cs (after the template codes) in a if statement. Something like this:

if($player.Punch){
All the DealsDamageBehavior code
}

Needless to say it does not work. I guess that to start with you can't really access functions from playerClass.cs in dealsDamage.cs. Am I right? If so, how do I go about to just putting the template code in the bottom of playerClass.cs? I tried that too but it didn't even work to just collide with the enemy sprite (as opposed to when dealsDamage is an behavior).

I also have a problem with playerClass.cs:

I've set up a sprite to be dragged towards left when it jumps but how can I get it to only do that when it's in a spesific layer? Or maybe only stop when it is on a colliding scene object (because it stands on a platform)?

I'm thinking about something like this:

if(!player == collide with Layer30){
$player.setConstantForceX(0);}

(As you can see in the source code I have the sprite move towards left when jumping, with "$player.setConstantForceX(-50);")

Excerpt of the source code follows (because I don't really understand the functions updateVertical() and setCurrentAnimation():

function playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
...
moveMap.bindCmd(keyboard, "up", "playerJump();", "");
...
%this.enableUpdateCallback();
}
...
function playerJump()
{
if(!$player.airborne)
{
$player.setLinearVelocityY(-135);
$player.setConstantForceX(-50); // This drags the sprite towards left when jumping.
$player.airborne = true;
}
}
...
function playerClass::updateHorizontal(%this)
{
...
}
function playerClass::updateVertical(%this)
{
%yVelocity = %this.getLinearVelocityY();
%this.setLinearVelocityY(5);
%collision = %this.castCollision(0.005);
%normalX = getWord(%collision, 4);
%normalY = getWord(%collision, 5);
// no collision
if (%collision $= "")
{
%this.airborne = true;
%this.setConstantForceY(100);
%this.setLinearVelocityY(%yVelocity);
return;
}
// collides with wall to the left
if (%normalX == 1 && %normalY == 0)
{
%this.againstLeftWall = true;
%this.setLinearVelocityX(0);
%this.setLinearVelocityY(%yVelocity);
return;
}
// collides with wall to the right
if (%normalX == -1 && %normalY == 0)
{
%this.againstRightWall = true;
%this.setLinearVelocityX(0);
%this.setLinearVelocityY(%yVelocity);
return;
}
// on ground with no wall collisions
if (%normalX == 0 && %normalY == -1)
{
%this.airborne = false;
%this.againstLeftWall = false;
%this.againstRightWall = false;
%this.setConstantForceY(0);
$player.setConstantForceX(0); // This sets the sprite to stand-still when not jumping
%this.setLinearVelocityY(%yVelocity);
return;
}
// in air and hits platform with head
if (%normalY == 1)
{
%this.airborne = true;
%this.setLinearVelocityX(0);
%this.setConstantForceY(100);
%this.setLinearVelocityY(%yVelocity);
return;
}
// in case another type of collision normal was detected
error("another collison type" SPC %normalX SPC %normalY);
%this.airborne = false;
%this.againstLeftWall = false;
%this.againstRightWall = false;
%this.setLinearVelocityY(%yVelocity);
}
function playerClass::onUpdate(%this)
{
%this.updateHorizontal();
%this.updateVertical();
%this.setCurrentAnimation();
}
function playerClass::setCurrentAnimation(%this)
{
%xVelocity = %this.getLinearVelocityX();
%yVelocity = %this.getLinearVelocityY();
if (%xVelocity > 0)
{
%this.setFlip(false, false);
}
else if (%xVelocity < 0)
{
%this.setFlip(true, false);
}
if (%this.airborne && %this.Punch)
{
%this.playAnimation(manPunchUp);
return;
}
if (%this.airborne)
{
if(%yVelocity < 0)
{
%this.playAnimation(manJump);
return;
}else
{
%this.playAnimation(manJump);
return;
}
}
if (%this.Duck && %this.Puch)
{
%this.playAnimation(manPunchDown);
return;
}
if (%this.Duck)
{
%this.playAnimation(mannDuck);
return;
}
if ($player.moveLeft == true || $player.moveRight == true)
{
if (%this.getAnimationName() $= "manRun")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(manRun);
return;
}
}else
{
%this.playAnimation(manRun);
}
}else
{
%this.playAnimation(manStand);
}
if (%this.Punch)
{
%this.playAnimation(manPunch);
return;
}
}

About the author

Recent Threads