Fish moves up and down even if i let go of the key
by linkbob · in Game Design and Creative Issues · 08/13/2009 (4:52 am) · 2 replies
hey guys....this is my player.cs file
i had i working fine earlier in the tutorial but it isnt working anymore and im not sure what i did
///--------------------------------------------------------------------------------------------
///CONTROLS
///--------------------------------------------------------------------------------------------
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();
}
///-----------------------------------------------------------------------------------
///Update Movement Function
///-----------------------------------------------------------------------------------
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.setLinearVelocityYt2dSceneObject::setLinearVelocityY(float velocityY)( 0 );
}
}
///--------------------------------------------------------------------------------------
///Modify Life Function
///--------------------------------------------------------------------------------------
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();
}
}
///---------------------------------------------------------------------------
///Life Size Function
///---------------------------------------------------------------------------
function PlayerFish::updateLifeSize(%this)
{
%lifeMultiplier = %this.life / 100;
%newWidth = %this.maxWidth * %lifeMultiplier;
%newHeight = %this.maxHeight * %lifeMultiplier;
%this.setSize(%newWidth, %newHeight);
}
///----------------------------------------------------------------------------
///Dead Function
///----------------------------------------------------------------------------
function PlayerFish::dead(%this)
{
%this.setFlipY(true);
%this.setLinearVelocityY(-10);
%this.dead = true;
}
///--------------------------------------------------------------------------------
///Lower Life Function
///--------------------------------------------------------------------------------
function PlayerFish::lowerLife(%this)
{
%this.modifyLife(%this.lifeDrain);
if(!%this.dead)
{
%this.schedule(500, "lowerLife");
}
}
///-----------------------------------------------------------------------------------------
///Fish Stop Function
///-----------------------------------------------------------------------------------------
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();
}
///---------------------------------------------------------------------------------
///Fish Move Funtion
///---------------------------------------------------------------------------------
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();
}
///-----------------------------------------------------------------------
///Fish Boost Function
///-----------------------------------------------------------------------
function fishPlayerBoost()
{
if($FishPlayer.dead)
return;
%flipX = $FishPlayer.getFlipX();
if(!%flipX)
{
%hSpeed = $FishPlayer.hSpeed * 3;
} else
{
%hSpeed = -$FishPlayer.hSpeed * 3;
}
$FishPlayer.setLinearVelocityX(%hSpeed);
}
///--------------------------------------------------------------------
///Fish BoostStop Function
///--------------------------------------------------------------------
function fishPlayerBoostStop()
{
$FishPlayer.setLinearVelocityX(0);
}
i had i working fine earlier in the tutorial but it isnt working anymore and im not sure what i did
///--------------------------------------------------------------------------------------------
///CONTROLS
///--------------------------------------------------------------------------------------------
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();
}
///-----------------------------------------------------------------------------------
///Update Movement Function
///-----------------------------------------------------------------------------------
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.setLinearVelocityYt2dSceneObject::setLinearVelocityY(float velocityY)( 0 );
}
}
///--------------------------------------------------------------------------------------
///Modify Life Function
///--------------------------------------------------------------------------------------
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();
}
}
///---------------------------------------------------------------------------
///Life Size Function
///---------------------------------------------------------------------------
function PlayerFish::updateLifeSize(%this)
{
%lifeMultiplier = %this.life / 100;
%newWidth = %this.maxWidth * %lifeMultiplier;
%newHeight = %this.maxHeight * %lifeMultiplier;
%this.setSize(%newWidth, %newHeight);
}
///----------------------------------------------------------------------------
///Dead Function
///----------------------------------------------------------------------------
function PlayerFish::dead(%this)
{
%this.setFlipY(true);
%this.setLinearVelocityY(-10);
%this.dead = true;
}
///--------------------------------------------------------------------------------
///Lower Life Function
///--------------------------------------------------------------------------------
function PlayerFish::lowerLife(%this)
{
%this.modifyLife(%this.lifeDrain);
if(!%this.dead)
{
%this.schedule(500, "lowerLife");
}
}
///-----------------------------------------------------------------------------------------
///Fish Stop Function
///-----------------------------------------------------------------------------------------
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();
}
///---------------------------------------------------------------------------------
///Fish Move Funtion
///---------------------------------------------------------------------------------
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();
}
///-----------------------------------------------------------------------
///Fish Boost Function
///-----------------------------------------------------------------------
function fishPlayerBoost()
{
if($FishPlayer.dead)
return;
%flipX = $FishPlayer.getFlipX();
if(!%flipX)
{
%hSpeed = $FishPlayer.hSpeed * 3;
} else
{
%hSpeed = -$FishPlayer.hSpeed * 3;
}
$FishPlayer.setLinearVelocityX(%hSpeed);
}
///--------------------------------------------------------------------
///Fish BoostStop Function
///--------------------------------------------------------------------
function fishPlayerBoostStop()
{
$FishPlayer.setLinearVelocityX(0);
}
Torque Owner Justin Proffitt
{
%this.setLinearVelocityYt2dSceneObject::setLinearVelocityY(float velocityY)( 0 );
}
Shouldn't that statement just be "%this.setLinearVelocityY( 0 );"? As it is, I wouldn't be surprised if it was giving you an error in your console. If that's the case then no matter what changes you make it will use the last script that was working, which would be the one where the fish keeps going up/down.
(Note: to see the console you have to modify the "commonConfig.xml" file in your Common folder, specifically change the console key from "~" to something else, like "`" or "ctrl z". Then save/close that file and mark the "read only" box in its properties.)
Besides that your script looks good, I hope you figure it out soon :)