Beruflich Dokumente
Kultur Dokumente
Download Source Files Follow the straight-forward steps of this Premium Tutorial to create an entertaining shoot-em-up with Flash and AS3.
Step 3: Interface
Our interface will be composed of several sprites, text fields and movie clips. Continue on to the next steps and well look at how to create it.
Step 4: Background
The background will be very simple, as the stars are generated using ActionScript. Create a 320480 px rectangle and fill it with black. You could add a slight radial gradient.
Step 5: Sprites
Ive used a great sprite library in the demo of this tutorial, these are part of the SpriteLib by Flying Yogi.
Well use Sound Effects to enhance the feeling of the game, you can find the sounds used in this example in Soungle.com using the keywords space, explosion and laser.
Well use a different tween engine from the default included in Flash, this will increase performace and be easier to use. You can download Tween Nano from its official website.
import flash.display.Sprite; public class Main extends Sprite { public function Main():void { // constructor code } }
stars = new Sprite(); for(var i:int = 0; i < n; i++) { var star:Star = new Star(); star.alpha = Math.random() * 1; star.scaleX = Math.random() * 1; star.scaleY = star.scaleX; star.x = Math.floor(Math.random() * stage.stageWidth); star.y = Math.floor(Math.random() * stage.stageHeight-20); stars.addChild(star); } /* Create another stars sprite to make animation */ starsCopy = new Sprite(); for(var j:int = 0; j < n; j++) { var star2:Star = new Star(); star2.alpha = Math.random() * 1 + 0.2; star2.scaleX = Math.random() * 1; star2.scaleY = star.scaleX; star2.x = Math.floor(Math.random() * stage.stageWidth); star2.y = Math.floor(Math.random() * stage.stageHeight-20); starsCopy.addChild(star2); } starsCopy.y = -stage.stageHeight; addChild(starsCopy); addChild(stars); addShip(); //Add ship (player) to stage
{ ship = new Ship(); ship.x = stage.stageWidth * 0.5; ship.y = stage.stageHeight + ship.height; addChild(ship); TweenNano.to(ship, 2, {y: (stage.stageHeight - ship.height) - 10, ease:Expo.easeOut, onComplete:listeners()}); addLives(); }
{ lives = new Vector.<Sprite>(); for(var i:int = 0; i < 3; i++) { var live:Ship = new Ship(); live.stop(); live.width = 16; live.height = 16; live.x = (stage.stageWidth - live.width * 0.7) - (5 * i+1) live.y = stage.stageHeight - live.height * 0.7; addChild(live); lives.push(live); }
live.width * i;
if(action == 'add') { stage.addEventListener(MouseEvent.MOUSE_MOVE, moveShip); stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot); timer.addEventListener(TimerEvent.TIMER, addEnemy); stage.addEventListener(Event.ENTER_FRAME, update); timer.start(); } else { stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveShip); stage.removeEventListener(MouseEvent.MOUSE_DOWN, shoot); timer.removeEventListener(TimerEvent.TIMER, addEnemy); stage.removeEventListener(Event.ENTER_FRAME, update); timer.stop(); }
ship.x = mouseX;
var bullet:Bullet = new Bullet(); bullet.x = ship.x; bullet.y = ship.y - (ship.height * 0.5); laserSound.play(); //Play sound bullets.push(bullet); addChild(bullet);
update() function.
152private final function addEnemy(e:TimerEvent):void 153{ 154 155 156 157 158 159 160 161 162}
var enemy:Enemy = new Enemy(); enemy.x = Math.floor(Math.random() * (stage.stageWidth - enemy.width)); enemy.y = -enemy.height; enemies.push(enemy); addChild(enemy);
listeners('remove'); /* Create and show alert */ alertView = new AlertView(); alertView.x = stage.stageWidth * 0.5; alertView.y = stage.stageHeight * 0.5; alertView.titleTF.text = t; alertView.msgTF.text = m; alertView.addEventListener(MouseEvent.MOUSE_UP, restart); addChild(alertView);
//code
stars.y = -stars.height;
starsCopy.y = -stars.height;
for(var i:int = 0; i < bullets.length; i++) { bullets[i].y -= 10; /* Destroy offstage bullets */ if(bullets[i].y < 0) { removeChild(bullets[i]); bullets[i] = null; bullets.splice(i, 1); } }
boss = new Boss(); bossSound.play(); boss.x = stage.stageWidth * 0.5; boss.y = -boss.height; TweenNano.to(boss, 3, {y: 80}); addChild(boss);
exSound.play();
if(lives.length == 0) { alert('Game Over', 'Click to continue'); } else { /* Tween Ship */ ship.y = stage.stageHeight + ship.height; TweenNano.to(ship, 2, {y: (stage.stageHeight - ship.height),
ease:Expo.easeOut}); }
/* Hit Boss */ if(boss != null && bullets[k].hitTestObject(boss)) { exSound.play(); removeChild(bullets[k]); bullets[k] = null; bullets.splice(k, 1); bossHealth--; scoreTF.text = String(int(scoreTF.text) + 50); } if(bossHealth <= 0 && boss != null) { removeChild(boss); boss = null; alert('You Won', 'Click to continue'); }
exSound.play(); //Play sound removeChild(enemies[j]); enemies[j] = null; enemies.splice(j, 1); removeChild(bullets[k]); bullets[k] = null; bullets.splice(k, 1); scoreTF.text = String(int(scoreTF.text) + 50); //Add score to the
314textfield in stage
//code
The first part of the restart() function handles the sprites, the next lines of code remove all the images from the stage. 1322/* Remove Graphics */ 1323 1324removeChild(ship); 1325ship = null; 1326 1327for(var i:int = 0; i < bullets.length; i++) 1328{ 1329 1330 1331} 1332 1333bullets.length = 0; 1334 1335for(var j:int = 0; j < enemies.length; j++) 1336{ 1337 1338 1339} 1340 1341enemies.length = 0; 1342 1343for(var k:int = 0; k < lives.length; k++) 1344{ 1345 1346 1347} 1348 1349lives.length = 0; 1350 1351removeChild(stars); 1352stars = null; 1353removeChild(starsCopy); 1354starsCopy = null; 1355 1356if(boss != null) 1357{ 1358 1359 1360}
Add the class name to the Class field in the Publish section of the Properties panel to associate the FLA with the Main document class.
Conclusion
Youve learned how to create a Space Shooter game with all its basic features, try to expand it using what you already know! I hope you liked this tutorial, thank you for reading!