Skip to main content
May 13, 2014
Question

How do i make the program stop when there is no bullets?

  • May 13, 2014
  • 1 reply
  • 468 views

package  {

  import flash.display.MovieClip;

  import flash.ui.Mouse;

  import flash.events.MouseEvent;

  import flash.events.TimerEvent;

  import flash.utils.Timer;

  import flash.events.Event;

  import flash.net.URLLoader;

  import flash.net.URLRequest;

  public class main extends MovieClip {

  var req:URLRequest;

  var loader:URLLoader;

  var cursor:Cursor = new Cursor();

  var gameTimer:Timer = new Timer(1200);

  var score:Number;

  var bullet:Number;

  public function main() {

  stop();

  instructionBtn.addEventListener(MouseEvent.CLICK,doIntro);

  Mouse.hide();

  cursor.mouseEnabled = false;

  stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMouse);

  startBtn.addEventListener(MouseEvent.CLICK,duckGame);

  stage.addEventListener(MouseEvent.CLICK,setBullet);

  addChild(cursor);

  }

  function moveMouse(evt:MouseEvent):void

  {

  cursor.x = this.mouseX;

  cursor.y = this.mouseY;

  }

  function doIntro(evt:MouseEvent):void

  {

  gotoAndStop(2);

  doDisplay1();

  backBtn.addEventListener(MouseEvent.CLICK,doBack);

  addChild(cursor);

  }

  function doBack(evt:MouseEvent):void

  {

  gotoAndStop(1);

  instructionBtn.addEventListener(MouseEvent.CLICK,doIntro);

  startBtn.addEventListener(MouseEvent.CLICK,duckGame);

  }

  function doDisplay1():void

  {

  req = new URLRequest("introduction.txt");

  loader = new URLLoader();

  loader.addEventListener(Event.COMPLETE,fileLoaded);

  loader.load(req);

  addChild(cursor);

  }

  function fileLoaded(evt:Event):void

  {

  txtToload.text = loader.data;

  }

  function duckGame(evt:MouseEvent):void

  {

  gotoAndStop(3);

  gameTimer.addEventListener(TimerEvent.TIMER,startGame);

  gameTimer.start();

  score = 0;

  scoreTxt.text = score.toString();

  gameOverTxt.visible = false;

  bullet = 21;

  bulletsTxt.text = bullet.toString();

  addChild(cursor);

 

  }

  function gameOver(evt:TimerEvent):void

  {

  gameOverTxt.visible = true;

  gameOverTxt.text = "Game Over!";

  }

  function startGame(evt:TimerEvent):void

  {

  var duck:Duck = new Duck();

  duck.x +=  stage.stageWidth;

  duck.y = Math.random() * stage.stageHeight;

  addChild(duck);

  addChild(cursor);

  }

  function setScore():void

  {

  score++;   // score = score + 1

  scoreTxt.text = score.toString();

  }

  function setBullet(evt:MouseEvent):void

  {

  bullet--;

  bulletsTxt.text = bullet.toString();

  }

  }

}

This topic has been closed for replies.

1 reply

Inspiring
May 13, 2014

Do this in setBullet after it takes away 1 bullet: if(bullet <= 0) {gameOver();}

May 13, 2014

function setBullet(evt:MouseEvent):void

  {

  bullet--;

  bulletsTxt.text = bullet.toString();

  if(bullet <= 0);

  gameOverTxt.visible = true;}

  }

like this?

Inspiring
May 13, 2014

function setBullet(evt:MouseEvent):void

  {

  bullet--;

  bulletsTxt.text = bullet.toString();

  if(bullet == 0)

  {

  gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE,gameOver);

  }}

wuds wrong with this


How is it not working? Is gameover still not happening? If so, then make sure that gameTimer is turned on ( gameTimer.start() )