Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 4.64 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Actionscript3: Countdown number of ducks
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. import flash.events.MouseEvent;
  5. import flash.text.TextField;
  6.  
  7. [SWF(width="800", height="600", backgroundColor="#E6FCFF")]
  8.  
  9. public class Main extends Sprite
  10. {
  11.     private var _sittingDucks:Array = []; //always set your arrays with [] at the top
  12.     public var _scoreDisplay:TextField
  13.  
  14.  
  15.     public function Main()
  16.     {
  17.         //adding the background, and positioning it
  18.         var background:Background = new Background();
  19.         this.addChild(background);
  20.         background.x = 30;
  21.         background.y = 100;
  22.  
  23.         for(var i:uint = 0; i < 5; i++)
  24.         {
  25.             //adding the first cloud, and positioning it
  26.             var clouds:Clouds = new Clouds();
  27.             this.addChild(clouds);
  28.             clouds.x = 130 + Math.random() * 600; //130 to 730
  29.             clouds.y = 230;
  30.             clouds.speedX = Math.random() * 3;
  31.             clouds.width = clouds.height = 200 * Math.random()//randomly changes the clouds demensions
  32.         }  
  33.  
  34.         var waves:Waves = new Waves();
  35.         this.addChild(waves);
  36.         waves.x = 0;
  37.         waves.y = 510;
  38.         waves.speedX = Math.random() * 3;
  39.  
  40.  
  41.         for(var j:uint = 0; j < 8; j++)
  42.         {
  43.             var ducks:Ducks = new Ducks();
  44.             this.addChild(ducks);
  45.             ducks.x = 100 + j * 100;
  46.             ducks.y = 475;
  47.             _sittingDucks.push(ducks);
  48.             ducks.addEventListener(MouseEvent.CLICK, ducksDestroy);
  49.         }
  50.  
  51.         var waves2:Waves = new Waves();
  52.         this.addChild(waves2);
  53.         waves2.x = 0;
  54.         waves2.y = 520;
  55.         waves2.speedX = Math.random() * 3;
  56.  
  57.         var setting:ForeGround = new ForeGround();
  58.         this.addChild(setting);
  59.         setting.x = 0;
  60.         setting.y = 50;
  61.         setting.width = 920;
  62.  
  63.         var board:ScoreDisplay = new ScoreDisplay();
  64.         this.addChild(board);
  65.         board.x = 570;
  66.         board.y = 35;
  67.  
  68.     }
  69.     private function ducksDestroy(event:MouseEvent):void
  70.     {
  71.         //store the crow we clicked on in a new array
  72.         var clickedDuck:Ducks = Ducks(event.currentTarget);
  73.  
  74.         //remove it from the crows array
  75.         //find the address of the crow we are removing
  76.         var index:uint = _sittingDucks.indexOf(clickedDuck);
  77.  
  78.         //remove it from the array with splice
  79.         _sittingDucks.splice(index, 1);
  80.  
  81.         //remove it from my document's display list
  82.         this.removeChild(clickedDuck);
  83.     }
  84. }
  85.  
  86.  
  87. import flash.events.Event;
  88. import flash.events.MouseEvent;
  89. import flash.text.TextField;
  90. import ScoreDisplayBase; // always import the classes you are using
  91.  
  92. public class ScoreDisplay extends ScoreDisplayBase
  93. {
  94.     private var txt:TextField; // where is it initialized?
  95.     private var score:uint = 0;
  96.  
  97.     public function ScoreDisplay()
  98.     {
  99.         super(); // do you init txt here?
  100.     }
  101.  
  102.     public function scoreUpdate():void
  103.     {
  104.         score += 10; // ok, so I suppose that your score does not represent the remaining ducks as you said, just only a score
  105.         txt.text = score.toString();
  106.     }
  107. }
  108.        
  109. //you want a reference to the ScoreDisplay, not this
  110. public var _scoreDisplay:TextField //no
  111. public var _scoreDisplay:ScoreDisplay //yes
  112.        
  113. _scoreDisplay = :ScoreDisplay = new ScoreDisplay();
  114. this.addChild(_scoreDisplay );
  115. _scoreDisplay .x = 570;
  116. _scoreDisplay .y = 35;
  117.        
  118. private function spawnDucks() {
  119.     for(var j:uint = 0; j < 8; j++)
  120.     {
  121.         var ducks:Ducks = new Ducks();
  122.         this.addChild(ducks);
  123.         ducks.x = 100 + j * 100;
  124.         ducks.y = 475;
  125.         _sittingDucks.push(ducks);
  126.         ducks.addEventListener(MouseEvent.CLICK, ducksDestroy);
  127.     }
  128. }
  129.        
  130. private function ducksDestroy(event:MouseEvent):void
  131. {
  132.     //store the crow we clicked on in a new array
  133.     var clickedDuck:Ducks = Ducks(event.currentTarget);
  134.  
  135.     //remove it from the crows array
  136.     //find the address of the crow we are removing
  137.     var index:uint = _sittingDucks.indexOf(clickedDuck);
  138.  
  139.     //remove it from the array with splice
  140.     _sittingDucks.splice(index, 1);
  141.  
  142.     //remove it from my document's display list
  143.     this.removeChild(clickedDuck);
  144.  
  145.     //update the score
  146.     _scoreDisplay.scoreUpdate();
  147.  
  148.     //Check if all the ducks are gone
  149.     if (_sittingDucks.length == 0) {
  150.         //All the ducks are dead, we've won the game!
  151.  
  152.         //create some kind of popup to display.
  153.         //add it to the screen, have some form
  154.         //of button (or a timer) take it away
  155.  
  156.         //whatever takes the popup away, have it call 'reset'
  157.  
  158.     }
  159. }
  160.  
  161. private function reset():void
  162. {
  163.     //write a reset method to clear the score
  164.     _scoreDisplay.reset();
  165.  
  166.     //create some ducks and you're ready to go!
  167.     spawnDucks();
  168. }