Talk:TGB/BasicTutorial2
From TDN
Wouldn't it be better to use the bind() function in stead of the bindCmd() function?
Not only does it have better support for actionmap functions like getBinding(), but it would also result in half of the functions. To clarify:
function playerShip::onLevelLoaded(%this, %scenegraph)
{
//set the player's ship name to the instance
$pShip = %this;
moveMap.bindCmd(keyboard, "w", "pShipUp();", "pShipUpStop();");
moveMap.bindCmd(keyboard, "s", "pShipDown();", "pShipDownStop();");
moveMap.bindCmd(keyboard, "a", "pShipLeft();", "pShipLeftStop();");
moveMap.bindCmd(keyboard, "d", "pShipRight();", "pShipRightStop();");
}
Would become:
function playerShip::onLevelLoaded(%this, %scenegraph)
{
//set the player's ship name to the instance
$pShip = %this;
moveMap.bind(keyboard, "w", pShipUp);
moveMap.bind(keyboard, "s", pShipDown);
moveMap.bind(keyboard, "a", pShipLeft);
moveMap.bind(keyboard, "d", pShipRight);
}
and
function pShipUp()
{
$pShip.moveUp = true;
$pShip.updateMovement();
}
function pShipDown()
{
$pShip.moveDown = true;
$pShip.updateMovement();
}
function pShipLeft()
{
$pShip.moveLeft = true;
$pShip.updateMovement();
}
function pShipRight()
{
$pShip.moveRight = true;
$pShip.updateMovement();
}
function pShipLeftStop()
{
$pShip.moveLeft = false;
$pShip.updateMovement();
}
function pShipRightStop()
{
$pShip.moveRight = false;
$pShip.updateMovement();
}
function pShipUpStop()
{
$pShip.moveUp = false;
$pShip.updateMovement();
}
function pShipDownStop()
{
$pShip.moveDown = false;
$pShip.updateMovement();
}
Would become:
function pShipLeft(%keyPressed)
{
$pShip.moveLeft = %keyPressed;
$pShip.updateMovement();
}
function pShipRight(%keyPressed)
{
$pShip.moveRight = %keyPressed;
$pShip.updateMovement();
}
function pShipUp(%keyPressed)
{
$pShip.moveUp = %keyPressed;
$pShip.updateMovement();
}
function pShipDown(%keyPressed)
{
$pShip.moveDown = %keyPressed;
$pShip.updateMovement();
}
Would improve readability for the script as well imo.. Sabbie 17:03, 24 December 2007 (PST)



