----------------- Part 6 - Section 1 ----------------- Code Sample 6.1.1 " function enemyShip::fireMissile(%this) { %this.enemyMissile = new t2dStaticSprite() { scenegraph = %this.scenegraph; class = enemyMissile; missileSpeed = %this.missileSpeed; enemy = %this; }; %this.enemyMissile.fire(); } " Code Sample 6.1.2 " function enemyShip::spawn(%this) { %this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed)); %this.setPositionY(getRandom(%this.minY, %this.maxY)); %this.setPositionX(%this.startX); %this.fireMissile(); } " Code Sample 6.1.3 " function enemyMissile::fire(%this) { %this.setLinearVelocityX(%this.missileSpeed); %this.setPosition(%this.enemy.getPosition()); %this.setImageMap(enemyMissileImageMap); %this.setSize(22, 10); %this.setCollisionActive( true, true ); %this.setCollisionPhysics(false, false); %this.setCollisionCallback(true); } " Code Sample 6.1.4 " function enemyMissile::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts ) { if(%dstObj.class $= "playerShip") { %srcObj.explode(); %dstObj.explode(); } } " Code Sample 6.1.5 " function enemyMissile::explode(%this) { %this.safeDelete(); } " ----------------- Part 6 - Section 2 ----------------- Code Sample 6.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();"); %this.isDead = false; } " Code Sample 6.2.2 " function playerShip::createMissile(%this) { %this.playerMissile = new t2dStaticSprite() { scenegraph = %this.scenegraph; class = playerMissile; missileSpeed=%this.missileSpeed; player = %this; }; %this.playerMissile.fire(); } " Code Sample 6.2.3 " function playerShip::createMissile(%this, %isDead) { if(!%this.isDead) { %this.playerMissile = new t2dStaticSprite() { scenegraph = %this.scenegraph; class = playerMissile; missileSpeed=%this.missileSpeed; player = %this; }; %this.playerMissile.fire(); } } " Code Sample 6.2.4 " function playerShip::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts ) { if(%this.isDead) return; if(%dstObj.class $= "enemyShip") { %srcObj.explode(); %dstObj.explode(); } } " Code Sample 6.2.5 " function playerShip::explode(%this) { %this.isdead = true; %this.setEnabled(false); %this.schedule(2000, "spawn"); } " Code Sample 6.2.6 " function playerShip::spawn(%this) { %this.isDead = false; %this.setPosition(%this.startX, %this.startY); %this.setEnabled(true); } " Code Sample 6.2.7 " function playerShip::onLevelLoaded(%this, %scenegraph) { //set the player's ship name to the instance $pShip = %this; %this.isDead = false; //These commands bind our keys to our functions moveMap.bindCmd(keyboard, "w", "pShipUp();", "pShipUpStop();"); moveMap.bindCmd(keyboard, "s", "pShipDown();", "pShipDownStop();"); moveMap.bindCmd(keyboard, "a", "pShipLeft();", "pShipLeftStop();"); moveMap.bindCmd(keyboard, "d", "pShipRight();", "pShipRightStop();"); moveMap.bindCmd(keyboard, "space", "$pShip.createMissile();", ""); } " Code Sample 6.2.8 " //save the players starting position %this.startX = %this.getPositionX(); %this.startY = %this.getPositionY(); " Code Sample 6.2.9 " function playerShip::onLevelLoaded(%this, %scenegraph) { //set the player's ship name to the instance $pShip = %this; %isDead = false; //save the players starting position %this.startX = %this.getPositionX(); %this.startY = %this.getPositionY(); //These commands bind our keys to our functions moveMap.bindCmd(keyboard, "w", "pShipUp();", "pShipUpStop();"); moveMap.bindCmd(keyboard, "s", "pShipDown();", "pShipDownStop();"); moveMap.bindCmd(keyboard, "a", "pShipLeft();", "pShipLeftStop();"); moveMap.bindCmd(keyboard, "d", "pShipRight();", "pShipRightStop();"); moveMap.bindCmd(keyboard, "space", "$pShip.createMissile();", ""); } " Code Sample 6.2.10 " // Exec game scripts. exec("./player.cs"); exec("./enemy.cs"); exec("./playerMissile.cs"); exec("./enemyMissile.cs"); "