(AS3) Functions - actionscript-3

I managed to fix the whole null stage error by following MJW's guide on debugging Error #1009. But now the function that initializes the bullets doesn't get called.
Snippets:
if (stage) {
 init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
...
private function init(event:Event = null) {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.ENTER_FRAME, shoot);
}
...
private function shoot(event:Event) {
var bullet:EnemyBullet = new EnemyBullet();
stage.addChild(bullet);
bullet.x = enemy.x;
bullet.y = enemy.y;
bullet.theta = Math.random() * 360;
bManager.bulletVector.push(bullet);
}
Note that when I put trace() within the second two functions, nothing happens, but the addEventListener() in the first snippet does get called (or so I think).

As a general practice, stage should not be referenced - especially in your case, where your reference is solely to add instances of your bullet class. If it's a matter of z-index, you could instead have a layer in which bullets are placed on top of other display objects on the display list.
Besides complexities loading multiple SWFs on a single stage, your code would become nice isolated functional units by adding display objects to their own hierarchy of the display list. Or, you could leverage a MVC pattern whereby a controller manipulated views.
In order for your code to work, that class must either be the main function of the SWF or added to stage.
If it's the main function of the SWF, init() will be called.
Otherwise, assure it's getting added to the display list via an addChild().
Do you really intend to fire a bullet every frame? That could be 24 to 60 bullets a second. You might want to throttle that with some probability whether a bullet with fire.
Say this was a battlefield, and your battlefield class was added to stage, it could be implemented as this:
package
{
import flash.display.Sprite;
import flash.events.Event;
[SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0x0, frameRate = 30)]
public class Battlefield extends Sprite
{
public function Battlefield()
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
protected function addedToStageHandler(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
init();
}
protected function init():void
{
addEventListener(Event.ENTER_FRAME, frameHandler);
}
protected function frameHandler(event:Event):void
{
var odds:Number = Math.random();
trace((odds < 0.1 ? "Fire! " : "Skip...") + "Odds were: " + odds);
}
}
}
Which would output:
Skip... Odds were: 0.3539872486144304
Skip... Odds were: 0.742108017206192
Fire! Odds were: 0.025597115512937307
Skip... Odds were: 0.7608889108523726
Fire! Odds were: 0.08514392375946045
Skip... Odds were: 0.27881692815572023
Beyond Stage3D, I've never been fond of this initialization pattern, as you could just as easily rely on stage events, as in:
package
{
import flash.display.Sprite;
import flash.events.Event;
[SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0x0, frameRate = 30)]
public class Battlefield extends Sprite
{
public function Battlefield()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
protected function addedToStageHandler(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
addEventListener(Event.ENTER_FRAME, frameHandler);
}
protected function removedFromStageHandler(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, frameHandler);
removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
protected function frameHandler(event:Event):void
{
var odds:Number = Math.random();
trace((odds < 0.1 ? "Fire! " : "Skip...") + "Odds were: " + odds);
}
}
}
Therefore, upon added to stage the class initiates its actions, and upon removed from stage the class terminates its actions.

I think the issue is in the first block.
you are checking for stage, if stage is not null, then use a added to stage listener.
you should only be using addEventListener(Event.ADDED_TO_STAGE, init);
however, this is assuming that the class is DisplayObject subclass, objects that do not get added to stage cannot call the ADDED_TO_STAGE listener

Related

making a symbol move by keyboard not showing result and when published it not reads stop(); but replays it again and again

I am new to actionscript ,
My document class is ,
package
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class engine extends MovieClip
{
// moved ourShip to a class variable.
private var Circle:circle = new circle()
//our constructor function. This runs when an object of
//the class is created
public function engine()
{
addFrameScript(0, frame1);
addFrameScript(1, frame2);
}
// frame 1 layer 1 --------------------------------------------------
public function frame1()
{
stop();
}
//-------------------------------------------------------------------
// frame 2 layer 1 --------------------------------------------------
public function frame2()
{
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
//-------------------------------------------------------------------
}
}
i made two frames first contains button and the other circle which i want to move but it not moves and it stays in the middle on second frame
My button class is
package
{
//imports
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.display.MovieClip;
//-------
public class start extends SimpleButton
{
public function start()
{
addEventListener(MouseEvent.CLICK, onTopClick);
addEventListener(MouseEvent.MOUSE_OVER, onBottomOver);
}
function onTopClick(e:MouseEvent):void
{
MovieClip(root).gotoAndStop(2)
}
function onBottomOver(e:MouseEvent):void
{
}
}
}
And my as of circle movieclip is
package
{
//imports
import flash.display.MovieClip;
import flash.display.Stage;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class circle extends MovieClip
{
private var speed:Number = 0.5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.93;
private var maxspeed:Number = 8;
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
x+=vx;
y+=vy
}
function keyHit(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.RIGHT :
vx+=speed;
break;
case Keyboard.LEFT :
vx-=speed;
break;
case Keyboard.UP :
vy-=speed;
break;
case Keyboard.DOWN :
vy+=speed;
break;
}
}
}
}
I am sorry to post so much for you guys to read but stackoverflow is the only website where anyone helps me !
You have made several major errors. First, addFrameScript() isn't a proper way to place code on frames, use Flash's editor to place code on timeline. (IIRC you will have to make a single call out of your two in order to have all the code you add to function) And, whatever code you added to a frame of a MC is executed each frame if the MC's currentFrame is the frame with code. Thus, you are adding a function "frame2()" that places the Circle in the center of the stage each frame! You should instead place it at design time (link it to a property) into the second frame, or in a constructor, or you can use one single frame and Sprite instead of MovieClip, and instead of using frames you can use container sprites, adding and removing them at will, or at an action.
The other major mistake is adding an event listener inside an enterframe listener - these accumulate, not overwrite each other, so you can have multiple functions be designated as listeners for a particular event, or even one function several times. The latter happens for you, so each frame another instance of a listening keyHit function is added as a listener. The proper way to assign listeners is either in constructor, or in any function that listens for manually triggered event (say, MouseEvent.CLICK), but then you have to take precautions about listening for more than once with each function, and listening only with those functions you need right now.
EDIT:
Okay. Your code was:
addFrameScript(0, frame1);
addFrameScript(1, frame2);
The more correct way should be:
addFrameScript(0,frame1,1,frame2);
The reason is, the call to addFrameScript replaces all the timeline code with what you supply within here. The function is undocumented, perhaps by the reason of its affects on the stage and AS3 environment. The closest thing to the documentation on addFrameScript() so far is this link.
Next: Your code is:
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
x+=vx;
y+=vy
}
The correct way of writing this is as follows:
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
}
public function loop(e:Event) : void
{
x+=vx;
y+=vy
}
The listeners should be assigned in constructor, if they are permanent or you want them to be active as soon as you create an object. The KeyboardEvent listeners are separate case, as in order for them to function you have to assign them to stage, which is not available right at the time of creating the object, so you need an intermediate layer - the init() function, that is only called when the object is added to stage. At this point stage is no longer null, and you can assign an event listener there. Note, if you want to make your circles eventually disappear, you have to remove the listener you assigned to stage at some point of your removal handling code.
Next: Your code:
public function frame2()
{
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
Correct code should be:
public function frame2():void
{
if (Circle.parent) return; // we have added Circle to stage already!
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
See, you are calling this every time your MC is stopped at second frame, thus you constantly reset Circle's coordinates to stage center, so you just cannot see if it moves (it doesn't, as you have assigned the keyboard listener not to stage).
Perhaps there are more mistakes, but fixing these will make your MC tick a little bit.

When does stage get loaded?

I'm building a project in Adobe Flash Professional CS5 (using ActionScript 3.0).
In one of the classes, I want to add some objects to the scene based on the size of the scene.
I'm using the following code in the constructor:
stageWidthint = stage.stageWidth;
stageHeightint = stage.stageHeight;
startMenu.x = stageWidthint / 2;
startMenu.y = ((stageHeightint / 2) - 40);
instructionsMenu.x = stageWidthint / 2;
instructionsMenu.y = ((stageHeightint / 2) + 2);
highscoreMenu.x = stageWidthint / 2;
highscoreMenu.y = ((stageHeightint / 2) + 44);
quitMenu.x = stageWidthint / 2;
quitMenu.y = ((stageHeightint / 2) + 86);
this.addChild(startMenu);
this.addChild(instructionsMenu);
this.addChild(highscoreMenu);
this.addChild(quitMenu);
I'm getting a null reference on stage. After a quick search I found out stage is not yet loaded at that time. Still, I'd like to add those childs to the class. When does the stage get loaded? How can I solve this problem and still show all the stuff when the game starts?
Use the ADDED_TO_STAGE event in your constructor.
public function ConstructorName():void
{
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
// add init code here
}
I'm not sure how things work when using scenes, but you could try:
package {
import flash.display.*;
import flash.events.*;
public class Test extends MovieClip {
public function Test() {
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
protected function init(event:Event = null):void {
trace("init");
if (event) { removeEventListener(Event.ADDED_TO_STAGE, init) };
//your code here
}
}
}
The Stage object is not globally available.
A class that extends a MovieClip will have a property "stage"(lowercase)
The "stage" property is null until the instance of your class has been added to stage.
That is why we listen for the ADD_TO_STAGE event.
There are work-arounds to stage = null one of which would be to pass in the stage as a parameter in the constructor IE: var xxx:MyClass = new MyClass(stage);
That is if the class creating the instance already has a reference to stage.
I would like to add that accessing stage in your custom class is not a good OOP practice since classes should be only concerned with themselves . What I would suggest is overriding the width setter/getters. This is because sizing can change for example landscape to portrait rotation.

Movieclip attraction/repulsion to mouse

I'm really new to flash and as3...
I'm trying to create 2 scenes in flash - one where my movieclip moves away from the mouse whenever it goes near it, and the other where the movie clip is attracted to the mouse. I have found an answer on actionscript 2 but i cannot use this in my as3 file...
Any help or ideas?
Cheers!
Here is an example I made of a display object being "pushed" and "pulled" in relation to the mouse's position.
Main.as(Document class):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
public class Main extends Sprite
{
public static var PULL:String = "pull";
public static var PUSH:String = "push";
private var _circle:Circle;
private var _force:String;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_circle = new Circle();
addChild(_circle);
_force = PULL;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
}// end function
private function onStageMouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame);
}// end function
private function onStageMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
stage.removeEventListener(Event.ENTER_FRAME, onStageEnterFrame);
_force = (_force == PULL) ? PUSH : PULL;
}// end function
private function onStageEnterFrame(e:Event):void
{
var point1:Point = new Point(_circle.x, _circle.y);
var point2:Point = new Point(stage.mouseX, stage.mouseY);
var point3:Point = point2.subtract(point1);
point3.normalize(10);
if (_force == PULL) {
_circle.x += point3.x;
_circle.y += point3.y;
} else if (_force == PUSH) {
_circle.x -= point3.x;
_circle.y -= point3.y;
}// end else if
}// end function
}// end class
}// end package
import flash.display.Sprite;
import flash.events.Event;
class Circle extends Sprite {
public function Circle() {
draw();
}// end function
private function draw():void {
this.graphics.lineStyle(1);
this.graphics.beginFill(0xFFFFFF);
this.graphics.drawCircle( 0, 0, 20);
this.graphics.endFill();
}// end function
}// end class
In the init() method we add a new display object to the stage, this is the display object we will be "pulling" and "pushing" in relation to the mouse's position.
_circle = new Circle();
addChild(_circle);
Then we set the _force property to our PULL constant. The _force property will determine whether the display object is "pulled" or "pushed".
_force = PULL;
Next we add our mouse event listeners to the stage. On MouseEvent.MOUSE_DOWN we call the onStageMouseDown() event handler. When the handler is called, we add an Event.ENTER_FRAME and MouseEvent.MOUSE_UP event listeners to the stage.
private function onStageMouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame);
}// end function
When the MouseEvent.MOUSE_UP event handler is called, the previous Event.ENTER_FRAME and MouseEvent.MOUSE_UP event listeners are removed from the stage. Then depending on the _force property's value, its value is alternated between PUSH and PULL.
private function onStageMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
stage.removeEventListener(Event.ENTER_FRAME, onStageEnterFrame);
_force = (_force == PULL) ? PUSH : PULL;
}// end function
Lastly, the onStageEnterFrame event handler. This is where we calculate the new position of the display object in relation to the mouse.
There are different ways to go about this, but I decided to use the Point class to simplify things. First we have to get a Point object for the display object's position and another Point object for mouse's position.
var point1:Point = new Point(_circle.x, _circle.y);
var point2:Point = new Point(stage.mouseX, stage.mouseY);
Next we have to get the difference between the points using the Point object's subtract() method.
var point3:Point = point2.subtract(point1);
With this new point, we can use the Point object's normalize() method to scale the line segment between the display object's position and the mouse's position to a set length.
point3.normalize(10);
Finally depending on the _force property's value we either subtract or add the point's x and y properties from the display object's x and y properties.

AS3 - Move MovieClip across screen

I've 3 different MovieClips that I need to move at the same time across the screen. (From bottom to top)
What is the best way of doing this without using a tweening class like Caurina?
Thank you for tips.
You could add an event listener to the parent container of the display objects which listens for the Event.ENTER_FRAME event. On each Event.ENTER_FRAME event you simply decrement the y property of the display objects like in the following example.
package
{
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="600", height="500")]
public class Main extends Sprite
{
private var _squares:Vector.<Square>;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_squares = new Vector.<Square>();
var redSquare:Square = new Square(0xFF0000, 100);
redSquare.x = 0;
redSquare.y = 400;
addChild(redSquare);
var greenSquare:Square = new Square(0x00FF00, 100);
greenSquare.x = 300;
greenSquare.y = 300;
addChild(greenSquare);
var blueSquare:Square = new Square(0x0000FF, 100);
blueSquare.x = 500;
blueSquare.y = 100;
addChild(blueSquare);
_squares.push(redSquare, greenSquare, blueSquare);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}// end function
private function onEnterFrame(e:Event):void
{
for each(var square:Square in _squares)
{
if (square.y > 0) square.y -= 5;
}// end for
}// end function
}// end class
}// end package
import flash.display.Sprite;
internal class Square extends Sprite
{
public function Square(color:uint, size:Number)
{
graphics.beginFill(color);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
}// end function
}// end function
I think you'd be making life easier for yourself though if you simply used Greensock's Tweening platform instead.
You can animate them in Flash IDE with frames and tweenings.
Also you can animate them programmatically yourself. Place every movie clip at the bottom of the screen, write some code that moves your movieClips a little to the top of the screen and gets called periodically (using Timer, EnterFrame event listener or setInterval), stop calling this code when all movieClips reached the top (using Timer.stop(), removeEventListener or clearInterval).
I don't see why you might need to do that because there are many tweening libraries that do all this for you.

create one addEventListener for all array elements in actionscript

I have an array of movie clips that represent buttons the user can click on, and so I need to use addEventListener function so the click can be handled.
I can use a loop and create an addEventListener for each element, I have 26 elements in the array, but I want to try another solution by using only one addEventListener and apply it on the array instead of the elements.
I want to know how to recognize which button was clicked, I mean what's its index in the array.
Thanks.
This is probably a good time to learn about event bubbling. You could just add one listener to the common parent of all your buttons
buttonContainer.addEventListener(MouseEvent.CLICK, buttonContainerClickHandler);
Then try and work out what was clicked
private function buttonContainerClickHandler(e:MouseEvent):void
{
var targetButton:Sprite = e.target as Sprite;
//do something with targetButton.
}
To find out what button was clicked, you could use the indexOf method of your array, and pass it the targetButton.
One thing you will have to do is make sure each of your buttons has mouseChildren set to false, or e.target will return the child assets of the buttons.
Add it to the movieclip. Adding an event listener to an array doesn't make a whole lot of sense. You are basically saying "Hey array, let me know when something about you changes" and Array isn't a subclass of EventDispatcher so that is a no-go. But in any case you don't want to know about the array, you want to know about the movieclip so the logical thing to do is to make the loop and add it to the movieclip.
You can't assign an event listener to an array.
What I think you're doing is applying a different event listener function to each clip in the array.
For each of the clips you can add the same event listener:
clips[i].addEventListener(MouseEvent.CLICK, handleClick);
and the handleClick function looks something like:
function handleClick(e:MouseEvent):void {
trace(clips.indexOf(e.target)) // outputs index the movieclip that was clicked on
}
Create a class for your buttons and add the event listener to the class. This way you don't even have to loop through your movie clips as the method will be part of the class
You can't get out of looping directly -- some loop will have to apply somewhere, but you can get out of looping indirectly -- you can let the VM loop for you. You should look at Array.forEach.
A simple application might be:
// assuming myArr is your array.
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, myClickHandler );
} );
To get the item's index, you might make something more complicated:
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, function( event:Event ):void
{
trace( "my index is", index );
} );
} );
I do have to recommend that you simply cache the array someplace accessible to your listener function and then use Array.indexOf along with event.currentTarget, but if you don't find that acceptable, you can use forEach this way:
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, function( event:Event ):void
{
trace( "my index is", array.indexOf( item ) );
} );
} );
Depending on how often you call those methods, it might not be faster to simply us this:
// probably not best to have this public
protected var myArr:Array = [/* whatever goes in here */]
public function register():void
{
myArr.forEach( addHandler );
}
protected function addHandler( item:IEventListener, index:int, arr:Array ):void
{
item.addEventListener( MouseEvent.CLICK, myClickHandler );
}
protected function myClickHandler( event:MouseEvent ):void
{
trace( event.currentTarget, "is at", myArr.indexOf( event.currentTarget ) );
}
But, I can't be sure without knowing more about your particular use case.
You could create your own custom vector class for IEventDispatcher objects that has a custom method that adds an event listener to all its elements. The best way to do this is to create a proxy class that acts as a wrapper for a Vector.<IEventDispatcher> object. I've created an example to demonstrate this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.IEventDispatcher;
public class Main extends Sprite
{
private var _eventDispatcherVector:EventDispatcherVector;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var redCustomButton:CustomButton = new CustomButton("RED", 0xFF0000);
addChild(redCustomButton);
var blueCustomButton:CustomButton = new CustomButton("BLUE", 0x00FF00);
blueCustomButton.x = 100;
addChild(blueCustomButton);
var greenCustomButton:CustomButton = new CustomButton("GREEN", 0x0000FF);
greenCustomButton.x = 200;
addChild(greenCustomButton);
_eventDispatcherVector = new EventDispatcherVector(Vector.<IEventDispatcher>([redCustomButton,
blueCustomButton,
greenCustomButton]));
_eventDispatcherVector.addEventListener(MouseEvent.CLICK, onCustomButtonClick);
}// end function
private function onCustomButtonClick(e:Event):void
{
var customButton:CustomButton = e.target as CustomButton;
trace("You clicked: " + customButton.name + "\n" +
"Its index is: " + _eventDispatcherVector.indexOf(customButton));
}// end function
}// end class
}// end package
import flash.utils.Proxy;
import flash.utils.flash_proxy;
import flash.events.IEventDispatcher;
use namespace flash_proxy;
dynamic internal class EventDispatcherVector extends Proxy
{
private var _eventDispatcherVector:Vector.<IEventDispatcher>;
public function EventDispatcherVector(eventDispatcherVector:Vector.<IEventDispatcher>)
{
_eventDispatcherVector = eventDispatcherVector;
}// end function
override flash_proxy function getProperty(name:*):*
{
return _eventDispatcherVector[name];
}
override flash_proxy function setProperty(name:*, value:*):void
{
_eventDispatcherVector[name] = value;
}// end function
public function indexOf(searchElement:*, fromIndex:*= 0):int
{
return _eventDispatcherVector.indexOf(searchElement, fromIndex);
}// end function
public function addEventListener(type:String,
listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false):void
{
for each(var eventDispatcher:IEventDispatcher in _eventDispatcherVector)
{
eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}// end for each
}// end function
}// end class
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
internal class CustomButton extends Sprite
{
public function CustomButton(name:String, color:uint)
{
graphics.beginFill(color);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.text = name;
textField.mouseEnabled = false;
textField.x = (100 / 2) - (textField.width / 2);
textField.y = (100 / 2) - (textField.height / 2);
addChild(textField);
this.name = name;
}// end function
}// end class
The output from clicking on the CustomButton objects is the following:
You clicked: RED
Its index is: 0
You clicked: BLUE
Its index is: 1
You clicked: GREEN
Its index is: 2
You can check out Stigglers answer for the question Actionscript 3.0 Best Option for Subclassing Vector Class (Flash Player 10) for more detail.