Change format of any selected textfield in AS3? - actionscript-3

I create several textfields when the click event is fired. Now I want to change the text format of any selected textfield. But the format is just applied to the last created textfield. I tried the following:
function _txtbtn(e:*):void
{
myText = new TextField();
mc3 = new MovieClip();
myText.text = "text...";
myFormat.font = "Arial";
myFormat.color = txt_color()
myText.setTextFormat(myFormat);
mc3.addChild(myText);
addChild(mc3);
mc3.x = _can.x;
mc3.y = p;
p= mc3.y+mc3.height+10;
mc3.addEventListener(MouseEvent.MOUSE_DOWN,_select)
}
function _select(e:MouseEvent):void
{
tool_stage.combo.addEventListener(Event.CHANGE,_font)
}
function _font(e:Event):void
{
format.font = tool_stage.combo.selectedLabel;
myText.setTextFormat(format);
}

It is right, because the variable myText refers to the last Object.
Instead of this you can get the current TextField object from the Event.
Each event has currentTarget value, which refers to the Object that has fired the Event.
You then can cast the currentTarget to your type and do the action with it.
Unfortunately I don't have your whole code, that is why I have my own version.
Have a look at it, I think it can help you.
//Main.as
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var button:Sprite;
private var p:int = 50;
private var x0:int = 20;
public function Main()
{
init();
}
private function init():void
{
button = new Sprite();
button.graphics.beginFill(0xFFCC00);
button.graphics.drawRect(0, 0, 80, 20);
button.graphics.endFill();
button.addEventListener(MouseEvent.CLICK, onBtnClick);
this.addChild(button);
}
private function onBtnClick(e:*):void
{
var myFormat:TextFormat = new TextFormat();
var myText:TextField = new TextField();
var mc3:MovieClip = new MovieClip();
myText.text = "text...";
myFormat.font = "Arial";
myFormat.color = 0x000000;
myText.setTextFormat(myFormat);
mc3.addChild(myText);
addChild(mc3);
mc3.x = x0;
mc3.y = p;
p= mc3.y+mc3.height+10;
myText.addEventListener(MouseEvent.CLICK, onTextClick)
}
private function onTextClick(evt:MouseEvent):void
{
var newFormat:TextFormat = new TextFormat();
newFormat.size = 30;
newFormat.font = "Verdana";
(evt.currentTarget as TextField).setTextFormat(newFormat);
}
}
}

Related

[AS3]How to make text appear for a few seconds

Im having trouble keeping doing this. In my update function i have an if statement.
if (_score == 30)
{
timeIncreaseText();
}
timeIncreaseText function is
private function timeIncreaseText():void
{
var textformat:TextFormat = new TextFormat();
textformat.size = 18;
var mytextfield:TextField = new TextField();
mytextfield.defaultTextFormat = textformat;
addChild(mytextfield);
mytextfield.text = "Time has increased. Better hurry!";
mytextfield.textColor = 0xff0000;
mytextfield.width = 500;
mytextfield.x = 100;
mytextfield.y = 200;
}
This works great but i cant seem to make it go away after a few seconds. Help!
A custom subClass would be a good way to do this. You can extend the regular TextField class to include a timer that removes the text field after the specified time.
package
{
import flash.events.Event;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
//extend TextField so you get all it's functionality
public class TimedText extends TextField
{
private var timer:Timer;
//first parameter is the text you want to show, second is how many milliseconds before it disappears, third is a different textFormat if you wanted.
public function TimeText(startingText:String, time:Number = 5000, textFormat_:TextFormat = null):void {
super();
this.text = startingText;
if (!textFormat_) { //if a text format isn't passed in, create one with the default settings
textFormat_ = new TextFormat();
textFormat_.size = 18;
textFormat_.color = 0xff0000;
}
this.defaultTextFormat = textFormat_;
timer = new Timer(time, 1); //create a timer that runs only one time
timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true); //listen for the timer event
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
}
//use add to stage so the timer doesn't start until the text field is actually visible
private function addedToStage(e:Event):void {
timer.start();
}
private function timerTick(e:TimerEvent):void {
this.dispatchEvent(new Event(Event.COMPLETE)); //if you want something else to handle the removing
//or animate / fade out first
//or directly remove itself
if (this.parent) {
this.parent.removeChild(this);
}
}
}
}
Then you can just do this:
var mytextfield:TimedText = new TimedText("Time has increased. Better hurry!");
addChild(mytextfield);
mytextfield.width = 500;
mytextfield.x = 100;
mytextfield.y = 200;

create new movieclip from class [duplicate]

This question already exists:
Flash Action Script 3 design custom MovieClip as Button
Closed 9 years ago.
I am new to Flash AS3.I am trying to create a movie clip> I have created basic code but i need to convert it t package so i can create XX number of clips from it.
Can you help in making this simple package so I can use it like var btn:Button = new Button("title");
// mc_main
var mc_main:MovieClip = new MovieClip();
//mc.graphics.lineStyle(1,0x0000CC);
mc_main.graphics.beginFill(0x0000CC);
mc_main.graphics.drawRect(0, 0, 400, 40);
mc_main.graphics.endFill();
mc_main.x = 80;
mc_main.y = 60;
addChild(mc_main);
//mc_txt
var mc_txt:TextField = new TextField();
mc_main.addChild(mc_txt);
mc_txt.text = 'Hello!';
mc_txt.x = 50;
mc_txt.y = 8;
//mc_txt Color
var tf:TextFormat = new TextFormat();
tf.size = 18;
tf.bold = true;
tf.font = "Arial"
tf.color = 0xFFFFFF;
mc_txt.setTextFormat(tf);
I came up with code this but doesnt work
package com.fladev.button
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Button extends MovieClip
{
private var Title:String;
private var Image:String;
public static var mc_main:MovieClip = new MovieClip();
public function Button(Title:String, Image:String) {
this.Title = Title;
this.Image = Image;
Draw_main();
}
private function Draw_main()
{
mc_main.graphics.beginFill(0x0000CC);
mc_main.graphics.drawRect(0, 0, 400, 40);
mc_main.graphics.endFill();
mc_main.x = 80;
mc_main.y = 60;
this.stage.addChild(mc_main);
}
}
}
Your mc_main variable has no reason to be static.
Also, don't add it to the stage; add it to the instance of Button.

Error #2025: The supplied DisplayObject must be a child of the caller - Not sure how to fix

So I get an error saying that the supplied DisplayObject must be a child of the caller. What happens is my game works first time around in that clicking the 'Play' button calls the startGame function and removes the menu so that the game is shown, but then at the end of the game when the playAgainBtn is clicked, instead of simply playing the game again / restarting the game, I get this error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
It specifically points to this line:
menuLayer.removeChild(mainMenu);
Here is the code:
package {
import flash.display.MovieClip;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.AntiAliasType;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
import caurina.transitions.Tweener;
import flash.text.Font;
public class Main extends MovieClip {
public static var backgroundLayer:Sprite = new Sprite;
public static var gameLayer:Sprite = new Sprite;
public static var interfaceLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public static var gameOverLayer:Sprite = new Sprite;
public static var howToLayer:Sprite = new Sprite;
public static var scoresLayer:Sprite = new Sprite;
public static var aboutLayer:Sprite = new Sprite;
public var mainMenu:menuMain = new menuMain;
public var gameEnd:endGame = new endGame;
public var howtoPlay:howToPlay = new howToPlay;
public var gameAbout:aboutGame = new aboutGame;
public var intro:IntroSound = new IntroSound();
public var soundControl:SoundChannel = new SoundChannel();
public var gameTime:int;
public var levelDuration:int;
public var crosshair:crosshair_mc;
static var score:Number;
var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;
static var scoreHeader:TextField = new TextField();
static var scoreText:TextField = new TextField();
static var timeHeader:TextField = new TextField();
static var timeText:TextField = new TextField();
static var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
public var gameOverscoreFormat = new TextFormat("Arial Rounded MT Bold", 32, 0xFFFFFF);
public function Main()
{
addChild(gameLayer);
addChild(backgroundLayer);
addChild(interfaceLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
interfaceLayer.addChild(howtoPlay);
interfaceLayer.addChild(gameEnd);
interfaceLayer.addChild(gameAbout);
soundControl = intro.play(0, 100);
addMenuListeners();
}
public function menuReturn(e:Event)
{
addChild(gameLayer);
addChild(backgroundLayer);
addChild(interfaceLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
interfaceLayer.addChild(howtoPlay);
interfaceLayer.addChild(gameEnd);
interfaceLayer.addChild(gameAbout);
}
public function showAbout(e:Event)
{
menuLayer.removeChild(mainMenu);
interfaceLayer.addChild(gameAbout);
}
public function startGame(e:Event)
{
removeMenuListeners();
soundControl.stop();
interfaceLayer.removeChild(howtoPlay);
interfaceLayer.removeChild(gameAbout);
interfaceLayer.removeChild(gameEnd);
menuLayer.removeChild(mainMenu);
levelDuration = 30;
gameTime = levelDuration;
var gameTimer:Timer = new Timer(1000,levelDuration);
gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
gameTimer.start();
scoreHeader = new TextField();
scoreHeader.text = String("Score: ");
interfaceLayer.addChild(scoreHeader);
scoreHeader.x = 5;
scoreHeader.selectable = false;
scoreHeader.embedFonts = true;
scoreHeader.antiAliasType = AntiAliasType.ADVANCED;
scoreText = new TextField();
scoreText.text = String("0");
interfaceLayer.addChild(scoreText);
scoreText.x = 75;
scoreText.y = 0;
scoreText.selectable = false;
scoreText.embedFonts = true;
scoreText.antiAliasType = AntiAliasType.ADVANCED;
timeHeader = new TextField();
timeHeader.text = String("Time: ");
interfaceLayer.addChild(timeHeader);
timeHeader.x = 500;
timeHeader.y = 0;
timeHeader.selectable = false;
timeHeader.embedFonts = true;
timeHeader.antiAliasType = AntiAliasType.ADVANCED;
timeText = new TextField();
timeText.text = gameTime.toString();
interfaceLayer.addChild(timeText);
timeText.x = 558;
timeText.y = 0;
timeText.selectable = false;
timeText.embedFonts = true;
timeText.antiAliasType = AntiAliasType.ADVANCED;
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
timeHeader.setTextFormat(scoreFormat);
timeText.setTextFormat(scoreFormat);
var timeScorebg:Sprite = new Sprite();
backgroundLayer.addChild(timeScorebg);
timeScorebg.graphics.beginFill(0x333333);
timeScorebg.graphics.drawRect(0,0,600,30);
timeScorebg.graphics.endFill();
timeScorebg.y = 0;
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
gameLayer.addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
}
function addMenuListeners():void
{
mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
mainMenu.howToPlayBtn.addEventListener(MouseEvent.CLICK, showInstructions);
mainMenu.aboutBtn.addEventListener(MouseEvent.CLICK, showAbout);
howtoPlay.backBtn.addEventListener(MouseEvent.CLICK, menuReturn);
gameEnd.playagainBtn.addEventListener(MouseEvent.CLICK, startGame);
gameAbout.backBtn.addEventListener(MouseEvent.CLICK, menuReturn);
}
function removeMenuListeners():void
{
mainMenu.playBtn.removeEventListener(MouseEvent.CLICK, startGame);
mainMenu.howToPlayBtn.removeEventListener(MouseEvent.CLICK, showInstructions);
mainMenu.aboutBtn.removeEventListener(MouseEvent.CLICK, showAbout);
howtoPlay.backBtn.removeEventListener(MouseEvent.CLICK, menuReturn);
gameEnd.playagainBtn.removeEventListener(MouseEvent.CLICK, startGame);
gameAbout.backBtn.removeEventListener(MouseEvent.CLICK, menuReturn);
}
public function showInstructions(e:Event)
{
menuLayer.removeChild(mainMenu);
interfaceLayer.addChild(howtoPlay);
}
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
gameLayer.addChild(enemy);
gameLayer.addChild(crosshair);
}
function sendEnemyMed(e:Event)
{
var enemymed = new EnemyShipMed();
gameLayer.addChild(enemymed);
gameLayer.addChild(crosshair);
}
function sendEnemySmall(e:Event)
{
var enemysmall = new EnemyShipSmall();
gameLayer.addChild(enemysmall);
gameLayer.addChild(crosshair);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
scoreText.setTextFormat(scoreFormat);
}
function updateTime(e:TimerEvent):void
{
gameTime--;
timeText.defaultTextFormat = scoreFormat;
timeText.text = String(gameTime);
}
function timeExpired(e:TimerEvent):void
{
var gameTimer:Timer = e.target as Timer;
gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
interfaceLayer.addChild(gameEnd);
var thisFont:Font = new myFont();
var myFormat:TextFormat = new TextFormat();
myFormat.font = thisFont.fontName;
scoreText = new TextField();
scoreText.defaultTextFormat = myFormat;
scoreText.text = String(score);
interfaceLayer.addChild(scoreText);
scoreText.x = 278;
scoreText.y = 180;
scoreText.selectable = false;
scoreText.embedFonts = true;
scoreText.antiAliasType = AntiAliasType.ADVANCED;
scoreText.setTextFormat(gameOverscoreFormat);
Mouse.show();
removeChild(gameLayer);
addMenuListeners();
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
I'm not quite sure how to fix this, so any advice or solution will be welcome. I can't get it to work the way I intended without getting errors.
Thanks.
I believe the problem is calling menuLayer.removeChild(mainMenu); on the second play-through is throwing the error due to the fact that you'd already removed it once already. The quickest solution would be to do a check to ensure menuLayer contains mainMenu before you try and remove it:
if(menuLayer contains mainMenu)
menuLayer.removeChild(mainMenu);
(Note that I don't have access to the IDE right now, but I think this should work)
A more robust solution would be to call a different method when the play button is clicked from the main menu that removes mainMenu from menuLayer, then calls startGame (where as playAgain calls startGame directly).
EDIT
Ok I see what you mean. Perhaps something like this instead:
mainMenu.playBtn.addEventListener(MouseEvent.CLICK, playGame);
gameEnd.playagainBtn.addEventListener(MouseEvent.CLICK, playGameAgain);
...
public function playGame(e:Event)
{
menuLayer.removeChild(mainMenu);
startGame();
}
...
public function playGameAgain(e:Event)
{
startGame();
}
...
public function startGame()
I have no idea why your code is not working, but there is no need to fret, try:
MovieClip(menuLayer.parent).removeChild(menuLayer);
You remove mainMenu in two different locations. My guess is it is being removed once and then again moments later.
if ( mainMenu.parent == menuLayer ) {
menuLayer.removeChild( mainMenu );
}
This will verify that mainMenu is actually a child of menuLayer before removing it. You cannot remove a child from a parent that isn't actually its parent. Imagine the state taking a child away and taking custody of them from a kidnapper. It's not the prettiest comparison, but it gives the right idea.
I cannot verify this without seeing how the game over is handled, but I think potentially the problem is that you are not removing your event listeners each time the game is played. Therefore when you go back to the main menu and add them again, you now have TWO listeners for playAgainBtn.
So when you end a game and click on the playAgainBtn, startGame gets called TWICE. So the first time it removes things just fine, and the second time - there's nothing to remove. This issue will potentially exist with all of your event listeners given your current design.
If this is the case you simply need to remove your event listeners when the menu is removed.
I suggest that whenever you make the menu active you add the listeners, and then remove them whenever you hide it. Maybe have two methods, addMenuListeners and removeMenuListeners
You could create these two functions and use them where appropriate :
function addMenuListeners():void
{
mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
mainMenu.howToPlayBtn.addEventListener(MouseEvent.CLICK, showInstructions);
mainMenu.aboutBtn.addEventListener(MouseEvent.CLICK, showAbout);
howtoPlay.backBtn.addEventListener(MouseEvent.CLICK, menuReturn);
gameEnd.playagainBtn.addEventListener(MouseEvent.CLICK, startGame);
gameAbout.backBtn.addEventListener(MouseEvent.CLICK, menuReturn);
}
function removeMenuListeners():void
{
mainMenu.playBtn.removeEventListener(MouseEvent.CLICK, startGame);
mainMenu.howToPlayBtn.removeEventListener(MouseEvent.CLICK, showInstructions);
mainMenu.aboutBtn.removeEventListener(MouseEvent.CLICK, showAbout);
howtoPlay.backBtn.removeEventListener(MouseEvent.CLICK, menuReturn);
gameEnd.playagainBtn.removeEventListener(MouseEvent.CLICK, startGame);
gameAbout.backBtn.removeEventListener(MouseEvent.CLICK, menuReturn);
}
If you follow the rule of always removing the event listeners when not in use, you can avoid this issue.
if( mainMenu.parent ){ mainmenu.parent.removeChild( mainMenu );} Or perhaps its already removed / not added at all?
Change this line
menuLayer.removeChild(mainMenu);
to this one..
if (mainMenu.parent != null && mainMenu.parent == menuLayer)
{
menuLayer.removeChild(mainMenu);
}
Hope it will solve.

Alternative way to rearrange objects on a stage?

I want my menu to appear above everything else when my game opens but at the minute, the menu is on top at first until my timer starts then all of the other objects appear over the top of the menu. How can I change it so that the menu is on top and the game only starts playing and the timer only starts once the user clicks the 'Play' button and it takes them to the game?
Here is the code I have in my Main.as file. I have been experimenting to no avail as I have tried to figure out the easiest or most efficient way to do this, but I am so frustrated with it at the minute:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
public class Main extends MovieClip {
public var gameTime:int;
public var levelDuration:int;
public var crosshair:crosshair_mc;
static var score:Number;
var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;
var menu:menuMain = new menuMain;
static var scoreHeader:TextField = new TextField();
static var scoreText:TextField = new TextField();
static var timeHeader:TextField = new TextField();
static var timeText:TextField = new TextField();
public function Main()
{
levelDuration = 30;
gameTime = levelDuration;
var gameTimer:Timer = new Timer(1000,levelDuration);
gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
gameTimer.start();
scoreHeader = new TextField();
scoreHeader.x = 5;
scoreHeader.text = String("Score: ");
addChild(scoreHeader);
scoreText = new TextField();
scoreText.x = 75;
scoreText.y = 0;
scoreText.text = String(0);
addChild(scoreText);
timeHeader = new TextField();
timeHeader.x = 490;
timeHeader.y = 0;
timeHeader.text = String("Time: ");
addChild(timeHeader);
timeText = new TextField();
timeText.x = 550;
timeText.y = 0;
timeText.text = gameTime.toString();
addChild(timeText);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
timeHeader.setTextFormat(scoreFormat);
timeText.setTextFormat(scoreFormat);
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
stage.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
showMenu();
}
function showMenu()
{
stage.addChild(menu);
Mouse.show();
enemyShipTimer.stop();
enemyShipTimerMed.stop();
enemyShipTimerSmall.stop();
}
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
stage.addChild(enemy);
stage.addChild(crosshair);
}
function sendEnemyMed(e:Event)
{
var enemymed = new EnemyShipMed();
stage.addChild(enemymed);
stage.addChild(crosshair);
}
function sendEnemySmall(e:Event)
{
var enemysmall = new EnemyShipSmall();
stage.addChild(enemysmall);
stage.addChild(crosshair);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
}
function updateTime(e:TimerEvent):void
{
trace(gameTime);
// your class variable tracking each second,
gameTime--;
//update your user interface as needed
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
timeText.defaultTextFormat = scoreFormat;
timeText.text = String(gameTime);
}
function timeExpired(e:TimerEvent):void
{
var gameTimer:Timer = e.target as Timer;
gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
// do whatever you need to do for game over
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
I have been trying to figure it out most of the day but I don't have a lot of experience with Actionscript.
Create two containers (Sprites or MovieClips), add the one that you want to be in the back first, and the one you want to be in the front second. Now, add the menu to the front one and evrything else to the back one.

Can't access public properties of my Class

I'm still learning about object oriented programming...
I made my own simple button class to do horizontal buttons lists. I works fine, but...
package as3Classes {
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.text.TextFormat;
public class simpleButton extends MovieClip {
//var nome:String = new String();
var sub:Sprite = new Sprite();
public var realce:Sprite = new Sprite();
public var titulo:String;
public function simpleButton(nomeId:String, texto:String) {
this.name = nomeId;
this.titulo = texto;
this.useHandCursor = true;
this.buttonMode = true;
this.mouseChildren = false;
var txtf:TextFormat = new TextFormat();
txtf.font = "Arial";
var txt:TextField = new TextField();
txt.wordWrap = false;
txt.multiline = false;
txt.text = texto;
txt.setTextFormat(txtf);
txt.width = txt.textWidth+4;
txt.height = 22;
txt.x = 4;
txt.y = 2;
txt.cacheAsBitmap = true;
var fundo:Sprite = new Sprite();
fundo.graphics.beginFill(0xCCCCCC);
fundo.graphics.drawRect(0, 0, txt.width+8, 22);
fundo.graphics.endFill();
//var realce:Sprite = new Sprite();
this.realce.graphics.beginFill(0x00CC00);
this.realce.graphics.drawRect(0, 0, txt.width+8, 22);
this.realce.graphics.endFill();
this.sub.graphics.beginFill(0xFF0000);
this.sub.graphics.drawRect(0, 0, txt.width+8, 2);
this.sub.graphics.endFill();
this.addChild(fundo);
this.addChild(this.realce);
this.addChild(this.sub);
this.addChild(txt);
this.sub.alpha = 0;
this.sub.y = 22;
this.addEventListener(MouseEvent.MOUSE_OVER, mouseover);
this.addEventListener(MouseEvent.MOUSE_OUT, mouseout);
}
private function mouseover(e:MouseEvent){
sub.alpha = 1;
}
private function mouseout(e:MouseEvent){
sub.alpha = 0;
}
}
}
... when I try to access the "titulo" or set "realce" as alpha=1 (to display it as clicked) it returns undefined. I can only set or read proprieties inherited, as the name, alpha, etc. What is my conceptual mistake?
Yes! I found a way to avoid this issue: Since (as it seams) I can't access the internal public objects of "simpleButton" by display objects list, I create a private object (btns:Object) in the root of the main class (can be seen everywhere) and add the simpleButtons simultaneously as adding at display. The name is the same, so I can change the realce.alpha by btns and it will reflect in the display.
This is bizarre, because the e.target leads to the child instead of the object itself!
If somebody knows a better way, please let me know.