----------------- Part 3 - Section 2 ----------------- Code Sample 3.2.1 " 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();"); } " Code Sample 3.2.2 " 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(); } " Code Sample 3.2.3 " function playerShip::updateMovement(%this) { if(%this.moveLeft) { %this.setLinearVelocityX( -$pShip.hSpeed ); } if(%this.moveRight) { %this.setLinearVelocityX( $pShip.hSpeed ); } if(%this.moveUp) { %this.setLinearVelocityY( -$pShip.vSpeed ); } if(%this.moveDown) { %this.setLinearVelocityY( $pShip.vSpeed ); } if(!%this.moveLeft && !%this.moveRight) { %this.setLinearVelocityX( 0 ); } if(!%this.moveUp && !%this.moveDown) { %this.setLinearVelocityY( 0 ); } } " Code Sample 3.2.4 " exec(“./player.cs”); " Code Sample 3.2.5 " %this.setLinearVelocityX( $pShip.hSpeed ); %this.setLinearVelocityY( -$pShip.vSpeed ); "