Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.TimerEvent;
- import flash.geom.Rectangle;
- import flash.events.MouseEvent;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.text.Font;
- import flash.utils.Timer;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- import flash.net.URLRequest;
- /**
- * ...
- * @author Pedro
- */
- public class Main extends Sprite
- {
- private var limites:Rectangle;
- private var bola:Bola;
- private var button:Button;
- private var click1:Boolean = false;
- private var click2:Boolean = false;
- private var restartflag:Boolean = false;
- private var clicktimer:Timer = new Timer(2000);
- private var balltimer:Array = new Array(30);
- private var newx:int;
- private var arraybolas:Array = new Array();
- private var arraynums:Array = new Array(50);
- private var num:int;
- private var aux:int = 0;
- private var primecounter:int = 0;
- private var numstring:String = new String();
- private var primetext:TextField;
- private var primescore:TextField;
- [Embed(source="../lib/arial.ttf", fontName="arial", embedAsCFF="false")]
- private var Carnevalee:Class;
- private static const textformat:TextFormat = new TextFormat("arial", 12, 0x000000);
- public function Main():void
- {
- limites = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
- for (var i:int = 0; i < 30; i++)
- {
- balltimer[i] = new Timer(Math.floor((Math.random()*150)+1));
- }
- button = new Button();
- button.x = limites.x + 750;
- button.y = limites.y + 650;
- addChild(button);
- primetext = new TextField();
- primescore = new TextField();
- primetext.embedFonts = true;
- primetext.text = "NĂºmeros Primos: ";
- primetext.setTextFormat(textformat);
- primetext.x = limites.x + 600;
- primetext.y = limites.y + 100;
- primescore.x = limites.x + 700;
- primescore.y = limites.y + 100;
- addChild(primetext);
- addChild(primescore);
- addEventListener(Event.ENTER_FRAME, EnterFrame);
- button.addEventListener(MouseEvent.CLICK, ButtonClick);
- button.addEventListener(MouseEvent.CLICK, PixelaPixel);
- button.addEventListener(MouseEvent.MOUSE_DOWN, BeginTimer);
- button.addEventListener(MouseEvent.MOUSE_UP, ReleaseTimer);
- arraybolas = Randomize();
- for (var j:int = 0; j < 30; j++)
- {
- addChild(arraybolas[j]);
- }
- }
- private function EnterFrame(evt:Event):void
- {
- if (restartflag == true)
- {
- arraybolas = Randomize();
- for (var j:int = 0; j < 30; j++)
- {
- balltimer[j].addEventListener(TimerEvent.TIMER,arraybolas[j].BallTimer);
- parent.addChild(arraybolas[j]);
- }
- restartflag = false;
- }
- if (primecounter == 0)
- {
- primescore.text = "";
- }
- for (var i:int = 0; i < 30; i++)
- {
- arraybolas[i].PlaySound();
- }
- if (arraybolas.every(AllFinish) == true)
- {
- primescore.x = limites.x + 700;
- primescore.y = limites.y + 100;
- primescore.setTextFormat(textformat);
- primescore.text = primecounter.toString();
- for (var k:int = 0; k < 30; k++)
- {
- if (primecounter > 3)
- {
- if (arraybolas[k].isprime == false)
- {
- arraybolas[k].Destroy();
- }
- }
- }
- }
- }
- private function PixelaPixel(e:MouseEvent):void
- {
- if (click2 == true)
- {
- for (var k:int = 0; k < 30; k++)
- {
- if (arraybolas[k].EndLine() == false)
- {
- arraybolas[k].x += 1;
- }
- }
- }
- }
- public function ButtonClick(e:MouseEvent):Boolean
- {
- if (click1 == false && click2 == false)
- {
- for (var j:int = 0; j < 30; j++)
- {
- balltimer[j].addEventListener(TimerEvent.TIMER, arraybolas[j].BallTimer);
- balltimer[j].start();
- }
- click1 = true;
- click2 = false;
- trace("click1 true, click2 false");
- }
- else if (click1 == true && click2 == false)
- {
- for (var i:int = 0; i < 30; i++)
- {
- balltimer[i].stop();
- balltimer[i].removeEventListener(TimerEvent.TIMER, arraybolas[i].BallTimer);
- }
- click1 = false;
- click2 = true;
- trace("click1 false, click2 true");
- }
- if (arraybolas.every(AllFinish) == true)
- {
- click1 = false;
- click2 = false;
- restartflag = true;
- trace("restart flag is true!");
- }
- return click1, click2, restartflag;
- }
- private function BeginTimer(e:MouseEvent):void
- {
- clicktimer.reset();
- clicktimer.addEventListener(TimerEvent.TIMER, LongPress);
- clicktimer.start();
- }
- private function ReleaseTimer(e:MouseEvent):void
- {
- clicktimer.stop();
- }
- private function LongPress(e:TimerEvent):Boolean
- {
- clicktimer.stop();
- if (click1 == false && click2 == true)
- {
- for (var j:int = 0; j < 30; j++)
- {
- balltimer[j].start();
- balltimer[j].addEventListener(TimerEvent.TIMER, arraybolas[j].BallTimer);
- }
- click1 = true;
- click2 = false;
- trace("Resuming animation!");
- }
- return click1, click2;
- }
- private function Randomize():Array
- {
- primecounter = 0;
- for (var a:int = 0; a < 30; a++)
- {
- if (arraybolas[a] != null && arraynums[a] != null)
- {
- arraybolas[a].Destroy();
- arraybolas[a] = null;
- arraynums[a] = null;
- }
- }
- for (var i:int = 0; i < 30; i++)
- {
- num = Math.floor(Math.random() * 50) + 1;
- numstring = num.toString();
- arraynums[i] = numstring;
- for (var j:int = 0; j < i; j++)
- {
- trace("Checking array[" + j + "]");
- if (numstring == arraynums[j])
- {
- trace("arraynums[" + j + "] = " + num +", numstring = " + numstring);
- num = Math.floor(Math.random() * 50) + 1;
- numstring = num.toString();
- arraynums[i] = numstring;
- j = 0;
- trace("new arraynums[" + i + "] = " + numstring +"!");
- }
- }
- var cor:uint;
- if (num <= 10)
- {
- cor = 0x0033CC;
- }
- else if (num > 10 && num <= 20)
- {
- cor = 0xFF0000;
- }
- else if (num > 20 && num <= 30)
- {
- cor = 0x00CC00;
- }
- else if (num > 30 && num <= 40)
- {
- cor = 0xFFFF00;
- }
- else if (num > 40 && num <= 50)
- {
- cor = 0xFF6600;
- }
- arraybolas[i] = new Bola(cor, numstring);
- arraybolas[i].isprime = isPrime(numstring);
- if (arraybolas[i].isprime == true)
- {
- primecounter = primecounter + 1;
- }
- arraybolas[i].x = limites.x + 30;
- arraybolas[i].y = limites.y + ((i * 22) + 20);
- }
- return arraybolas;
- }
- private function AllFinish(element:*, index:int, arr:Array):Boolean
- {
- return (element.x == 300);
- }
- private function isPrime(num:String):Boolean
- {
- if (num == "2" || num == "3" || num == "5" || num == "7"
- || num == "11" || num == "13" || num == "17" || num == "19"
- || num == "23" || num == "29" || num == "31" || num == "37"
- || num == "41" || num == "43" || num == "47" || num == "53")
- {
- return true;
- }
- else return false;
- }
- }
- }
- // CLASSE BOLA
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.media.SoundTransform;
- import flash.text.TextFormat;
- import flash.text.TextFormatAlign;
- import flash.text.TextField;
- import flash.events.MouseEvent;
- import flash.events.TimerEvent;
- import flash.utils.Timer;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- import flash.net.URLRequest;
- /**
- * ...
- * @author Pedro
- */
- public class Bola extends Sprite
- {
- public var isprime:Boolean = false;
- public var texto:TextField;
- public var numero:String;
- public var soundflag:Boolean = false;
- public var sndchannel:SoundChannel = new SoundChannel();
- public var sndtrnsf:SoundTransform = new SoundTransform(0.2, -1);
- public var sndchannel2:SoundChannel = new SoundChannel();
- public var sndtrnsf2:SoundTransform = new SoundTransform(0.2, 1);
- [Embed(source="../lib/arial.ttf", fontName="arial", embedAsCFF="false")]
- private var Carnevalee:Class;
- private static const textformat:TextFormat = new TextFormat("arial", 12, 0x000000);
- [Embed(source = "../lib/notprimesfx.mp3")]
- private const Notprimesound:Class;
- private var notprimesfx:Sound = new Notprimesound() as Sound;
- [Embed(source = "../lib/primesfx.mp3")]
- private const Primesound:Class;
- private var primesfx:Sound = new Primesound() as Sound;
- public function Bola(cores:uint, num:String)
- {
- addEventListener(MouseEvent.CLICK, EventClick);
- addEventListener(TimerEvent.TIMER, BallTimer);
- addEventListener(Event.ENTER_FRAME, PlaySound);
- texto = new TextField();
- numero = num;
- texto.embedFonts = true;
- texto.text = numero;
- texto.setTextFormat(textformat);
- texto.x = texto.x - 7;
- texto.y = texto.y - 9;
- addChild(texto);
- graphics.lineStyle(1);
- graphics.beginFill(cores);
- graphics.drawCircle(0, 0, 10);
- }
- public function EventClick(e:MouseEvent):void
- {
- trace("Clicking the ball!");
- }
- public function Destroy():void
- {
- if (parent)
- {
- parent.removeChild(this);
- }
- }
- public function EndLine():Boolean
- {
- if (this.x == 300)
- {
- return true;
- }
- else return false;
- }
- public function BallTimer(e:TimerEvent):void
- {
- if (this.x != 300)
- {
- this.x = this.x + 1;
- }
- }
- public function PlaySound():void
- {
- if (this.EndLine() == true)
- {
- if (this.isprime == true && soundflag == false)
- {
- sndchannel = primesfx.play(0, 0, sndtrnsf);
- //sndchannel.soundTransform = sndtrnsf;
- primesfx.play();
- soundflag = true;
- }
- else if (this.isprime == false && soundflag == false)
- {
- sndchannel2 = notprimesfx.play(0, 0, sndtrnsf2);
- //sndchannel2.soundTransform = sndtrnsf2;
- notprimesfx.play();
- soundflag = true;
- }
- }
- removeEventListener(Event.ENTER_FRAME, PlaySound);
- }
- }
- }
- // CLASSE BOTAO
- package
- {
- import flash.display.Sprite;
- import flash.geom.Rectangle;
- import flash.events.MouseEvent;
- /**
- * ...
- * @author Pedro
- */
- public class Button extends Sprite
- {
- public function Button()
- {
- addEventListener(MouseEvent.CLICK, ButtonClick);
- graphics.lineStyle(1);
- graphics.beginFill(0x000000);
- graphics.drawRect(0, 0, 30, 30);
- this.buttonMode = true;
- }
- public function ButtonClick(e:MouseEvent):void
- {
- //trace("Button Clicked!");
- }
- }
- }
Add Comment
Please, Sign In to add comment