Mousechildren Issue - actionscript-3

Consider the following code.
var sParent:Sprite = new Sprite();
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
var childB:Sprite = new Sprite();
sParent.addChild(obj);
obj.addChild(childA);
obj.addChild(childB);
childB.mouseChildren = false;
childB.mouseEnabled = false;
sParent.addEventListener(MouseEvent.CLICK, itemClickHandler);
sParent.addEventListener(MouseEvent.ROLL_OVER, onHoverIn);
sParent.addEventListener(MouseEvent.ROLL_OUT, onHoverOut);
Now, I want to detect events on "ChildA" but I do not want to detect children on "ChildB"
mouseChildren = false;
obviously isn't the solution in this particular case. Any ideas?

Looks like it is a limitation or a design feature of ROLL_OVER, check the following program I have changed the ROLL_OVER event to MOUSE_OVER and target and currentTarget are giving the correct sprites :
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var sParent:Sprite = new Sprite();
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
childA.graphics.beginFill(0xff0000);
childA.graphics.drawRect(0, 0, 100, 50);
childA.graphics.endFill();
var childB:Sprite = new Sprite();
childB.x = 150;
childB.graphics.beginFill(0x00ff00);
childB.graphics.drawRect(0, 0, 100, 50);
childB.graphics.endFill();
sParent.addChild(obj);
obj.addChild(childA);
obj.addChild(childB);
childB.mouseChildren = false;
childB.mouseEnabled = false;
sParent.mouseEnabled = false;
obj.mouseEnabled = false;
sParent.addEventListener(MouseEvent.CLICK, itemClickHandler);
sParent.addEventListener(MouseEvent.MOUSE_OVER, onHoverIn);
sParent.addEventListener(MouseEvent.MOUSE_OUT, onHoverOut);
addChild(sParent);
}
private function onHoverOut(e:MouseEvent):void
{
trace(e.currentTarget.name+ " "+e.target.name);
}
private function onHoverIn(e:MouseEvent):void
{
trace(e.currentTarget.name+ " "+e.target.name);
}
private function itemClickHandler(e:MouseEvent):void
{
trace(e.currentTarget.name+ " "+e.target.name);
}
}
}

I had the same problem and solved it with something really unexpectable...
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
var childB:Sprite = new Sprite();
obj.addChild(childA);
obj.addChild(childB);
obj.mouseEnabled = false; // this.
childB.mouseEnabled = false;
childB.mouseChildren = false;

Related

(Actionscript 3) Pixel-perfect collision detection between the walls and player?

I am trying to make a flash game in which there is collision detection between the player and the walls. However, when I try using Wall11.hitTestPoint(), I cannot get the collision detection to be perfect. Then, I decided to use bitmap but it is hard to code this because the wall is irregularly shaped (it is not a square, rectangle, circle or any regular shape). Is there anyway to improve the collision detection with walls?
function checkCollision(_debug:Boolean = false):Boolean {
var bmd1:BitmapData = new BitmapData(Wall11.width, Wall11.height, true, 0);
var bmd2:BitmapData = new BitmapData(LevelOnePlayer.width, LevelOnePlayer.height, true, 0);
bmd1.draw(Wall11);
bmd2.draw(LevelOnePlayer);
if (_debug) {
var bmp:Bitmap = new Bitmap(bmd1);
bmp.x = Wall11.x;
bmp.y = Wall11.y;
addChild(bmp);
var bmp2:Bitmap = new Bitmap(bmd2);
bmp2.x = LevelOnePlayer.x;
bmp2.y = LevelOnePlayer.y;
addChild(bmp2);
}
if(bmd1.hitTest(new Point(Wall11.x, Wall11.y), 255, bmd2, new Point(LevelOnePlayer.x, LevelOnePlayer.y), 255))
return true;
if (!_debug) {
bmd1.dispose();
bmd2.dispose();
}
return false;
}
These are basics of hitTestPoint stuff.
package
{
import flash.geom.Point;
import flash.events.Event;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class HitTest extends Sprite
{
private var textArea:TextField;
private var Circle:Shape;
private var Box:Shape;
public function HitTest()
{
if (stage) onStage();
else addEventListener(Event.ADDED_TO_STAGE, onStage);
}
private function onStage(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, onStage);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.showDefaultContextMenu = false;
stage.align = StageAlign.TOP_LEFT;
stage.stageFocusRect = false;
addShapes();
addLabel();
onFrame();
// Call it every frame to keep things updated.
addEventListener(Event.ENTER_FRAME, onFrame);
}
private function onFrame(e:Event = null):void
{
// Place graphics to the center of the stage.
x = stage.stageWidth >> 1;
y = stage.stageHeight >> 1;
// Lets detect collision with the circle.
var aPoint:Point = new Point();
// Take local mouse coordinates within the target shape.
aPoint.x = Circle.mouseX;
aPoint.y = Circle.mouseY;
// Convert them into the root coordinates - it is the ONLY correct way.
// Comment the next 2 lines to see how local coordinates fail to work.
aPoint = Circle.localToGlobal(aPoint);
aPoint = root.globalToLocal(aPoint);
// Hit test the point against shape.
// Set the last parameter to false to see hitTest against the shape bounding box.
var aHit:Boolean = Circle.hitTestPoint(aPoint.x, aPoint.y, true);
textArea.text = aHit? "! HIT !": "NO HIT";
}
private function addShapes():void
{
Circle = new Shape();
Circle.graphics.lineStyle(2, 0x000000, 1);
Circle.graphics.beginFill(0xCC99FF, 1);
Circle.graphics.drawCircle(0, 0, 50);
Circle.graphics.endFill();
Box = new Shape();
Box.graphics.lineStyle(0, 0xCCCCCC, 1);
Box.graphics.drawRect(-50, -50, 100, 100);
addChild(Box);
addChild(Circle);
}
private function addLabel():void
{
textArea = new TextField();
textArea.x = 10;
textArea.y = 10;
textArea.width = 70;
textArea.height = 20;
textArea.border = true;
textArea.wordWrap = false;
textArea.multiline = true;
textArea.selectable = true;
textArea.background = true;
textArea.mouseEnabled = false;
var aFormat:TextFormat;
aFormat = textArea.getTextFormat();
aFormat.font = "_typewriter";
aFormat.size = 12;
aFormat.bold = true;
aFormat.align = TextFormatAlign.CENTER;
textArea.setTextFormat(aFormat);
textArea.defaultTextFormat = aFormat;
stage.addChild(textArea);
textArea.text = "NO HIT";
}
}
}

Webcam shows mirror image using action script

Using Flash CS5.5 and action script 3 I have made a small application for image capturing through web cam and integrated it in php page working perfect also save image. But problem is it shows MIRROR image on screen means if I move my right hand on screen it shows left hand. My query is how can I correct this setting. Here is the code -
package take_picture_fla
{
import com.adobe.images.*;
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.*;
import flash.ui.*;
import flash.utils.*;
dynamic public class MainTimeline extends MovieClip
{
public var capture_mc:MovieClip;
public var bitmap:Bitmap;
public var rightClickMenu:ContextMenu;
public var snd:Sound;
public var video:Video;
public var bitmapData:BitmapData;
public var warn:MovieClip;
public var save_mc:MovieClip;
public var bandwidth:int;
public var copyright:ContextMenuItem;
public var cam:Camera;
public var quality:int;
public function MainTimeline()
{
addFrameScript(0, frame1);
return;
}// end function
public function onSaveJPG(event:Event) : void
{
var myEncoder:JPGEncoder;
var byteArray:ByteArray;
var header:URLRequestHeader;
var saveJPG:URLRequest;
var urlLoader:URLLoader;
var sendComplete:Function;
var e:* = event;
sendComplete = function (event:Event) : void
{
warn.visible = true;
addChild(warn);
warn.addEventListener(MouseEvent.MOUSE_DOWN, warnDown);
warn.buttonMode = true;
return;
}// end function
;
myEncoder = new JPGEncoder(100);
byteArray = myEncoder.encode(bitmapData);
header = new URLRequestHeader("Content-type", "application/octet-stream");
saveJPG = new URLRequest("save.php");
saveJPG.requestHeaders.push(header);
saveJPG.method = URLRequestMethod.POST;
saveJPG.data = byteArray;
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, sendComplete);
urlLoader.load(saveJPG);
return;
}// end function
public function warnDown(event:MouseEvent) : void
{
navigateToURL(new URLRequest("images/"), "_blank");
warn.visible = false;
return;
}// end function
function frame1()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
rightClickMenu = new ContextMenu();
copyright = new ContextMenuItem("Developed By www.webinfopedia.com Go to Application");
copyright.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, myLink);
copyright.separatorBefore = false;
rightClickMenu.hideBuiltInItems();
rightClickMenu.customItems.push(copyright);
this.contextMenu = rightClickMenu;
snd = new camerasound();
bandwidth = 0;
quality = 100;
cam = Camera.getCamera();
cam.setQuality(bandwidth, quality);
cam.setMode(320, 240, 30, false);
video = new Video();
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);
bitmapData = new BitmapData(video.width, video.height);
bitmap = new Bitmap(bitmapData);
bitmap.x = 360;
bitmap.y = 20;
addChild(bitmap);
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK, captureImage);
save_mc.alpha = 0.5;
warn.visible = false;
return;
}// end function
public function captureImage(event:MouseEvent) : void
{
snd.play();
bitmapData.draw(video);
save_mc.buttonMode = true;
save_mc.addEventListener(MouseEvent.CLICK, onSaveJPG);
save_mc.alpha = 1;
return;
}// end function
public function myLink(event:Event)
{
navigateToURL(new URLRequest("http://www.webinfopedia.com/export-database-data-to-excel-in-php.html"), "_blank");
return;
}// end function
}
}
You can just use video.scaleX = -1; to mirror the image.
You should check your camera's settings at the system level - it is possible that the device driver is set to mirror the image.

Quiz in as3 error

I've been trying to make quiz but i get bunch of errors that say:The public attribute can only be used inside a package.
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author Ilija
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
private var ButtonI:Loader = new Loader();
I get errors for all these vars
private var ButtonI2:Loader = new Loader();
private var ButtonI3:Loader = new Loader();
private var Button1:Sprite = new Sprite();
private var Button2:Sprite = new Sprite();
private var Button3:Sprite = new Sprite();
private var QuestionText:TextField = new TextField();
private var A1Text:TextField = new TextField();
private var A2Text:TextField = new TextField();
private var A3Text:TextField = new TextField();
private var pointText:TextField = new TextField();
private var point:int = 0;
private var _questions:Questions;
public function Main():void
I have seen this error when you have { without a matching }
make sure you have an opening with closing curly brackets all over...

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.

Change format of any selected textfield in AS3?

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);
}
}
}