/* * A package represents a hierarchical directory structure used to organize the class files. This class * is located in the "edu" directory, which is equivalent to the package "edu". * Obs. All class definitions must begin with the definition of the package to which the class belongs. */ package edu { /* * Import the libraries that will be used inside the class */ import flash.display.MovieClip; import flash.display.Stage; import flash.display.Sprite; import flash.display.SimpleButton; import fl.controls.Button; import flash.display.Shape; import flash.events.Event; import flash.events.MouseEvent; import MyCircle; /* * Because this class is the document class of our main .fla file it has to extend the Sprite class. By * extending the Sprite class it will inherit(have access to) all the properties(fields) and methods defined * in the Sprite class. */ public class MyStage extends Sprite { // Define a Button component of this class public var my_btn:Button; public var maxRadius:Number; /* * This is a special method, called a contructor. It must have the same name as the class and it be * automatically called when we publsh the .fla file * */ public function MyStage() { super(); trace("MyStage constructor called"); maxRadius = 0; intializeInterface(); } /* * Perform the creation and the initialization of the button * */ public function intializeInterface():void { var btnLabel = "Create circles"; addButton(btnLabel, 0); } /* * Create a new button and add to the stage */ public function addButton(btnLabel:String, i:Number):void { // create button instance my_btn = new Button(); // define button coordinates my_btn.x = stage.stageWidth - 150; my_btn.y = stage.stageHeight - 125; // define button dimensions my_btn.width = 130; my_btn.height = 50; // define button label my_btn.label = btnLabel; my_btn.addEventListener(MouseEvent.CLICK, btnClick); function btnClick(e:MouseEvent) { // create a new circle each time the button is pressed createCircle(); } // add the newly created button to the stage addChild(my_btn); } /* * Handle the creatin of new circles * */ public function createCircle():void { // compute x and y coordinated of the new circle var xCoord:Number = (Math.round(Math.random()*stage.stageWidth)) - 200; var yCoord:Number = (Math.round(Math.random()*stage.stageWidth)) - 100; // create a new object(i.e. a new instance) of the MyCircle class. // The two parameters, namely xCoord and yCoord will be passed to the // contructor of the class var c:MyCircle = new MyCircle(xCoord, yCoord, this); setMaxRadius(c.getRadius()); // add the circle to the stage addChild(c); } /* * Set the maximum radius for the circles created so far */ private function setMaxRadius(radius:Number):void { if (radius > maxRadius) maxRadius = radius; } /* * Check if largest circle has been selected */ public function checkGuess(radius:Number):void { trace("check guess "+maxRadius); if (radius == maxRadius) { var noChildren = this.numChildren; // clear stage for(var i:uint = this.numChildren-1; i > 0; i--){ this.removeChildAt(i); } trace("Good guess"); } } } }