Guest User

Untitled

a guest
Jun 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     import flash.events.TimerEvent;
  6.     import flash.geom.Rectangle;
  7.     import flash.events.MouseEvent;
  8.     import flash.text.TextField;
  9.     import flash.text.TextFormat;
  10.     import flash.text.Font;
  11.     import flash.utils.Timer;
  12.     import flash.media.Sound;
  13.     import flash.media.SoundChannel;
  14.     import flash.net.URLRequest;
  15.    
  16.     /**
  17.      * ...
  18.      * @author Pedro
  19.      */
  20.    
  21.     public class Main extends Sprite
  22.     {  
  23.         private var limites:Rectangle;
  24.         private var bola:Bola;
  25.         private var button:Button;
  26.         private var click1:Boolean = false;
  27.         private var click2:Boolean = false;
  28.         private var restartflag:Boolean = false;
  29.         private var clicktimer:Timer = new Timer(2000);
  30.         private var balltimer:Array = new Array(30);
  31.         private var newx:int;
  32.         private var arraybolas:Array = new Array();
  33.         private var arraynums:Array = new Array(50);
  34.         private var num:int;
  35.         private var aux:int = 0;
  36.         private var primecounter:int = 0;
  37.         private var numstring:String = new String();
  38.         private var primetext:TextField;
  39.         private var primescore:TextField;
  40.  
  41.         [Embed(source="../lib/arial.ttf", fontName="arial", embedAsCFF="false")]
  42.         private var Carnevalee:Class;
  43.         private static const textformat:TextFormat = new TextFormat("arial", 12, 0x000000);
  44.  
  45.        
  46.         public function Main():void
  47.         {
  48.             limites = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
  49.            
  50.             for (var i:int = 0; i < 30; i++)
  51.             {
  52.                 balltimer[i] = new Timer(Math.floor((Math.random()*150)+1));
  53.             }
  54.            
  55.             button = new Button();
  56.             button.x = limites.x + 750;
  57.             button.y = limites.y + 650;
  58.             addChild(button);
  59.            
  60.             primetext = new TextField();
  61.             primescore = new TextField();
  62.             primetext.embedFonts = true;
  63.             primetext.text = "NĂºmeros Primos: ";
  64.             primetext.setTextFormat(textformat);
  65.             primetext.x = limites.x + 600;
  66.             primetext.y = limites.y + 100;
  67.             primescore.x = limites.x + 700;
  68.             primescore.y = limites.y + 100;
  69.             addChild(primetext);
  70.             addChild(primescore);
  71.            
  72.             addEventListener(Event.ENTER_FRAME, EnterFrame);
  73.             button.addEventListener(MouseEvent.CLICK, ButtonClick);
  74.             button.addEventListener(MouseEvent.CLICK, PixelaPixel);
  75.             button.addEventListener(MouseEvent.MOUSE_DOWN, BeginTimer);
  76.             button.addEventListener(MouseEvent.MOUSE_UP, ReleaseTimer);
  77.            
  78.             arraybolas = Randomize();
  79.            
  80.             for (var j:int = 0; j < 30; j++)
  81.             {
  82.                 addChild(arraybolas[j]);
  83.             }
  84.         }
  85.        
  86.         private function EnterFrame(evt:Event):void
  87.         {  
  88.            
  89.             if (restartflag == true)
  90.             {
  91.                 arraybolas = Randomize();
  92.                
  93.                 for (var j:int = 0; j < 30; j++)
  94.                 {
  95.                     balltimer[j].addEventListener(TimerEvent.TIMER,arraybolas[j].BallTimer);
  96.                     parent.addChild(arraybolas[j]);
  97.                 }
  98.                
  99.                 restartflag = false;
  100.             }
  101.            
  102.             if (primecounter == 0)
  103.             {
  104.                 primescore.text = "";
  105.             }
  106.            
  107.             for (var i:int = 0; i < 30; i++)
  108.             {
  109.                 arraybolas[i].PlaySound();
  110.             }
  111.            
  112.             if (arraybolas.every(AllFinish) == true)
  113.             {
  114.                 primescore.x = limites.x + 700;
  115.                 primescore.y = limites.y + 100;
  116.                 primescore.setTextFormat(textformat);
  117.                 primescore.text = primecounter.toString();
  118.                
  119.                 for (var k:int = 0; k < 30; k++)
  120.                 {
  121.                     if (primecounter > 3)
  122.                     {
  123.                         if (arraybolas[k].isprime == false)
  124.                         {
  125.                             arraybolas[k].Destroy();
  126.                         }
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.        
  132.         private function PixelaPixel(e:MouseEvent):void
  133.         {
  134.             if (click2 == true)
  135.             {
  136.                 for (var k:int = 0; k < 30; k++)
  137.                 {
  138.                     if (arraybolas[k].EndLine() == false)
  139.                     {
  140.                         arraybolas[k].x += 1;
  141.                     }
  142.                 }
  143.             }
  144.            
  145.         }
  146.        
  147.         public function ButtonClick(e:MouseEvent):Boolean
  148.         {
  149.             if (click1 == false && click2 == false)
  150.             {
  151.                 for (var j:int = 0; j < 30; j++)
  152.                 {
  153.                     balltimer[j].addEventListener(TimerEvent.TIMER, arraybolas[j].BallTimer);
  154.                     balltimer[j].start();
  155.                 }
  156.                 click1 = true;
  157.                 click2 = false;
  158.                 trace("click1 true, click2 false");
  159.             }
  160.             else if (click1 == true && click2 == false)
  161.             {
  162.                 for (var i:int = 0; i < 30; i++)
  163.                 {
  164.                     balltimer[i].stop();
  165.                     balltimer[i].removeEventListener(TimerEvent.TIMER, arraybolas[i].BallTimer);
  166.                 }
  167.                 click1 = false;
  168.                 click2 = true;
  169.                 trace("click1 false, click2 true");
  170.             }
  171.            
  172.             if (arraybolas.every(AllFinish) == true)
  173.             {
  174.                 click1 = false;
  175.                 click2 = false;
  176.                 restartflag = true;
  177.                 trace("restart flag is true!");
  178.             }
  179.            
  180.             return click1, click2, restartflag;
  181.         }
  182.        
  183.         private function BeginTimer(e:MouseEvent):void
  184.         {
  185.             clicktimer.reset();
  186.             clicktimer.addEventListener(TimerEvent.TIMER, LongPress);
  187.             clicktimer.start();
  188.         }
  189.        
  190.         private function ReleaseTimer(e:MouseEvent):void
  191.         {
  192.             clicktimer.stop();
  193.         }
  194.        
  195.         private function LongPress(e:TimerEvent):Boolean
  196.         {
  197.             clicktimer.stop();
  198.            
  199.             if (click1 == false && click2 == true)
  200.             {
  201.                 for (var j:int = 0; j < 30; j++)
  202.                 {
  203.                     balltimer[j].start();
  204.                     balltimer[j].addEventListener(TimerEvent.TIMER, arraybolas[j].BallTimer);
  205.                 }
  206.                 click1 = true;
  207.                 click2 = false;
  208.                 trace("Resuming animation!");
  209.             }
  210.            
  211.             return click1, click2;
  212.         }
  213.        
  214.         private function Randomize():Array
  215.         {
  216.             primecounter = 0;
  217.            
  218.             for (var a:int = 0; a < 30; a++)
  219.             {
  220.                 if (arraybolas[a] != null && arraynums[a] != null)
  221.                 {
  222.                     arraybolas[a].Destroy();
  223.                     arraybolas[a] = null;
  224.                     arraynums[a] = null;
  225.                 }
  226.             }
  227.            
  228.             for (var i:int = 0; i < 30; i++)
  229.             {
  230.                 num = Math.floor(Math.random() * 50) + 1;
  231.                 numstring = num.toString();
  232.                
  233.                 arraynums[i] = numstring;
  234.                
  235.                 for (var j:int = 0; j < i; j++)
  236.                 {
  237.                     trace("Checking array[" + j + "]");
  238.                     if (numstring == arraynums[j])
  239.                     {
  240.                         trace("arraynums[" + j + "] = " + num +", numstring = " + numstring);
  241.                         num = Math.floor(Math.random() * 50) + 1;
  242.                         numstring = num.toString();
  243.                         arraynums[i] = numstring;
  244.                         j = 0;
  245.                         trace("new arraynums[" + i + "] = " + numstring +"!");
  246.                     }
  247.                 }
  248.                
  249.                 var cor:uint;
  250.                 if (num <= 10)
  251.                 {
  252.                     cor = 0x0033CC;
  253.                 }
  254.                 else if (num > 10 && num <= 20)
  255.                 {
  256.                     cor = 0xFF0000;
  257.                 }
  258.                 else if (num > 20 && num <= 30)
  259.                 {
  260.                     cor = 0x00CC00;
  261.                 }
  262.                 else if (num > 30 && num <= 40)
  263.                 {
  264.                     cor = 0xFFFF00;
  265.                 }
  266.                 else if (num > 40 && num <= 50)
  267.                 {
  268.                     cor = 0xFF6600;
  269.                 }
  270.                
  271.                 arraybolas[i] = new Bola(cor, numstring);
  272.                 arraybolas[i].isprime = isPrime(numstring);
  273.                
  274.                 if (arraybolas[i].isprime == true)
  275.                 {
  276.                     primecounter = primecounter + 1;
  277.                 }
  278.                
  279.                 arraybolas[i].x = limites.x + 30;
  280.                 arraybolas[i].y = limites.y + ((i * 22) + 20);
  281.             }
  282.            
  283.             return arraybolas;
  284.         }
  285.        
  286.         private function AllFinish(element:*, index:int, arr:Array):Boolean
  287.         {
  288.             return (element.x == 300);
  289.         }
  290.        
  291.         private function isPrime(num:String):Boolean
  292.         {
  293.             if (num == "2" || num == "3" || num == "5" || num == "7"
  294.             || num == "11" || num == "13" || num == "17" || num == "19"
  295.             || num == "23" || num == "29" || num == "31" || num == "37"
  296.             || num == "41" || num == "43" || num == "47" || num == "53")
  297.             {
  298.                 return true;
  299.             }
  300.             else return false;
  301.         }
  302.     }
  303. }
  304.  
  305.  
  306. // CLASSE BOLA
  307.  
  308. package  
  309. {
  310.     import flash.display.Sprite;
  311.     import flash.events.Event;
  312.     import flash.media.SoundTransform;
  313.     import flash.text.TextFormat;
  314.     import flash.text.TextFormatAlign;
  315.     import flash.text.TextField;
  316.     import flash.events.MouseEvent;
  317.     import flash.events.TimerEvent;
  318.     import flash.utils.Timer;
  319.     import flash.media.Sound;
  320.     import flash.media.SoundChannel;
  321.     import flash.net.URLRequest;
  322.    
  323.     /**
  324.      * ...
  325.      * @author Pedro
  326.      */
  327.    
  328.     public class Bola extends Sprite
  329.     {
  330.         public var isprime:Boolean = false;
  331.         public var texto:TextField;
  332.         public var numero:String;
  333.         public var soundflag:Boolean = false;
  334.         public var sndchannel:SoundChannel = new SoundChannel();
  335.         public var sndtrnsf:SoundTransform = new SoundTransform(0.2, -1);
  336.         public var sndchannel2:SoundChannel = new SoundChannel();
  337.         public var sndtrnsf2:SoundTransform = new SoundTransform(0.2, 1);
  338.        
  339.         [Embed(source="../lib/arial.ttf", fontName="arial", embedAsCFF="false")]
  340.         private var Carnevalee:Class;
  341.         private static const textformat:TextFormat = new TextFormat("arial", 12, 0x000000);
  342.        
  343.         [Embed(source = "../lib/notprimesfx.mp3")]
  344.         private const Notprimesound:Class;
  345.         private var notprimesfx:Sound = new Notprimesound() as Sound;
  346.        
  347.         [Embed(source = "../lib/primesfx.mp3")]
  348.         private const Primesound:Class;
  349.         private var primesfx:Sound = new Primesound() as Sound;
  350.        
  351.      
  352.         public function Bola(cores:uint, num:String)
  353.         {  
  354.             addEventListener(MouseEvent.CLICK, EventClick);
  355.             addEventListener(TimerEvent.TIMER, BallTimer);
  356.             addEventListener(Event.ENTER_FRAME, PlaySound);
  357.            
  358.             texto = new TextField();
  359.             numero = num;
  360.             texto.embedFonts = true;
  361.             texto.text = numero;
  362.             texto.setTextFormat(textformat);
  363.             texto.x = texto.x - 7;
  364.             texto.y = texto.y - 9;
  365.             addChild(texto);
  366.        
  367.             graphics.lineStyle(1);
  368.             graphics.beginFill(cores);
  369.             graphics.drawCircle(0, 0, 10);
  370.         }
  371.        
  372.         public function EventClick(e:MouseEvent):void
  373.         {
  374.             trace("Clicking the ball!");
  375.         }
  376.        
  377.         public function Destroy():void
  378.         {  
  379.             if (parent)
  380.             {
  381.                 parent.removeChild(this);
  382.             }
  383.         }
  384.        
  385.         public function EndLine():Boolean
  386.         {
  387.             if (this.x == 300)
  388.             {
  389.                 return true;
  390.             }
  391.             else return false;
  392.         }
  393.        
  394.         public function BallTimer(e:TimerEvent):void
  395.         {
  396.             if (this.x != 300)
  397.             {
  398.                 this.x = this.x + 1;
  399.             }
  400.            
  401.         }
  402.        
  403.         public function PlaySound():void
  404.         {
  405.             if (this.EndLine() == true)
  406.             {
  407.                 if (this.isprime == true && soundflag == false)
  408.                 {
  409.                     sndchannel = primesfx.play(0, 0, sndtrnsf);
  410.                     //sndchannel.soundTransform = sndtrnsf;
  411.                     primesfx.play();
  412.                     soundflag = true;
  413.                 }
  414.                 else if (this.isprime == false && soundflag == false)
  415.                 {
  416.                     sndchannel2 = notprimesfx.play(0, 0, sndtrnsf2);
  417.                     //sndchannel2.soundTransform = sndtrnsf2;
  418.                     notprimesfx.play();
  419.                     soundflag = true;
  420.                 }
  421.             }
  422.             removeEventListener(Event.ENTER_FRAME, PlaySound);
  423.         }
  424.     }
  425. }
  426.  
  427.  
  428.  
  429. // CLASSE BOTAO
  430.  
  431. package  
  432. {
  433.     import flash.display.Sprite;
  434.     import flash.geom.Rectangle;
  435.     import flash.events.MouseEvent;
  436.    
  437.     /**
  438.      * ...
  439.      * @author Pedro
  440.      */
  441.    
  442.     public class Button extends Sprite
  443.     {
  444.         public function Button()
  445.         {
  446.             addEventListener(MouseEvent.CLICK, ButtonClick);
  447.             graphics.lineStyle(1);
  448.             graphics.beginFill(0x000000);
  449.             graphics.drawRect(0, 0, 30, 30);
  450.             this.buttonMode = true;
  451.         }
  452.    
  453.         public function ButtonClick(e:MouseEvent):void
  454.         {
  455.             //trace("Button Clicked!");
  456.         }
  457.    
  458.     }
  459. }
Add Comment
Please, Sign In to add comment