10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 2-38. HeroWeapon.as (continued)<br />

}<br />

}<br />

}<br />

ShipCreator<br />

return new HeroCannonBall( );<br />

} else {<br />

throw new Error("Invalid kind of projectile specified");<br />

return null;<br />

}<br />

The concrete class ShipCreator (Example 2-39) encapsulates ship creation. We don’t<br />

need to encapsulate knowledge about hero ships and alien ships at this point. After<br />

all, we have only one hero ship and one kind of alien ship.<br />

Example 2-39. ShipCreator.as<br />

package ships<br />

{<br />

import flash.display.Stage;<br />

}<br />

public class ShipCreator<br />

{<br />

public static const HERO :uint = 0;<br />

public static const ALIEN :uint = 1;<br />

}<br />

public function addShip(cShipType:uint, target:Stage, xLoc:int, yLoc:int):void<br />

{<br />

var ship:Ship = this.createShip(cShipType);<br />

ship.drawShip( ); // draw ship<br />

ship.setLoc(xLoc, yLoc); // set the x and y location<br />

target.addChild(ship); // add the sprite to the stage<br />

ship.initShip( ); // initialize ship<br />

}<br />

private function createShip(cShipType:uint):Ship<br />

{<br />

if (cShipType == HERO)<br />

{<br />

trace("Creating new hero ship");<br />

return new HeroShip( );<br />

} else if (cShipType == ALIEN) {<br />

trace("Creating new alien ship");<br />

return new AlienShip( );<br />

} else {<br />

throw new Error("Invalid kind of ship specified");<br />

return null;<br />

}<br />

}<br />

Example: Vertical Shooter Game | 99

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!