Game Development Community

Direct Input Joystick Fail. Fish Game Tutorial.

by Drillfoot · in Torque Game Builder · 05/06/2012 (5:01 pm) · 1 replies

The game works as planned as far as collecting the food, and the other fish and such. My Fish will also start big, and get smaller with time and not collecting food. However, I seem to have lost the control of the fish using the w,a,s,d keys. Im unsure where I went wrong. I think it has to do with the "playerboost" addition of if(%FishPlayer.dead) , but I'm unsure.

Console Error:
Activating DirectInput...
DirectInput joystick failed to enable!
Compiling C:/Users/Mike/Desktop/MyFishGame/MyFishGame/game/data/levels/MyFishLevel.t2d...
Loading compiled script C:/Users/Mike/Desktop/MyFishGame/MyFishGame/game/data/levels/MyFishLevel.t2d.
DirectInput deactivated.
Shutting down the OpenGL display device...
Making the GL rendering context not current...
Deleting the GL rendering context...
Releasing the device context...

Full Player.cs script to see if I went wrong?

Player.cs

function PlayerFish::onLevelLoaded(%this, %scenegraph)
{
$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();");

%this.lowerLife();

}

function PlayerFish::updateMovement(%this)
{
if(%this.dead)
return;
}


if(%this.moveLeft)
{
$FishPlayer.setFlipX(true);
$FishPlayer.setLinearVelocityX(-$FishPlayer.hSpeed);
}

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);
}


function PlayerFish::modifyLife(%this, %dmg)
{
%this.life +=%dmg;

if(%this.life > 100)
{
%this.life = 100;
} else if (%this.life < 0)
{
%this.life = 0;
}

if(%this.life <= 30)
{
%this.dead();
} else
{
%this.updateLifeSize();
}
}

function PlayerFish::updateLifeSize(%this)
{
%lifeMultiplier = %this.life / 100;

%newWidth = %this.maxWidth * %lifeMultiplier;
%newHeight = %this.maxHeight * %lifeMultiplier;

%this.setSize(%newWidth, %newHeight);
}

function PlayerFish::dead(%this)
{
%this.setFlipY(true);
%this.setLinearVelocityY(-10);
%this.dead = true;
}

function PlayerFish::lowerLife(%this)
{
%this.modifyLife(%this.lifeDrain);

if(!%this.dead)
{
%this.schedule(500, "lowerLife");
}
}


function fishPlayerUp()
{
$FishPlayer.moveUp = true;
$FishPlayer.updateMovement();
}

function fishPlayerDown()
{
$FishPlayer.moveDown = true;
$FishPlayer.updateMovement();
}

function fishPlayerLeft()
{
$FishPlayer.moveLeft = true;
$FishPlayer.updateMovement();
}

function fishPlayerRight()
{
$FishPlayer.moveRight = true;
$FishPlayer.updateMovement();
}

function fishPlayerUpStop()
{
$FishPlayer.moveUp = false;
$FishPlayer.updateMovement();
}

function fishPlayerDownStop()
{
$FishPlayer.moveDown = false;
$FishPlayer.updateMovement();
}

function fishPlayerLeftStop()
{
$FishPlayer.moveLeft = false;
$FishPlayer.updateMovement();
}

function fishPlayerRightStop()
{
$FishPlayer.moveRight = false;
$FishPlayer.updateMovement();
}

function fishPlayerBoost()

{
if($FishPlayer.dead)
return;


%flipX = $FishPlayer.getFlipX();

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

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

I appreciate any help.. =o)

#1
05/06/2012 (8:25 pm)
The fishPlayerBoost() does seem a bit odd. If you only want to boost when the fish "dead" value is false (assuming that means the fish is alive) - you might want to try something like:

function fishPlayerBoost()
{

 if($FishPlayer.dead == false)
{ 
%flipX = $FishPlayer.getFlipX();

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

I also notice that you are using it as a function inside modifyLife... you might want to try deleting the .dso files for your program and see if you get errors in the log trying to compile afterwards.