How to removechild IF it's visible - actionscript-3

I'm making a game in AS3 when you can save and restore your game.
The player can load his saved game at the introscreen OR during the game.
I've got an error (not very important as the game is still working but I'd like to correct in order to be clean) as I don't know how to put it in code.
So, when the player click on "load" at the Introscreen, a window opens where he can choose wich games he wants to restore.
When he choose a game, he click on it and the IntroScreen AND the restore window disappear.
Here's the code for this :
if(allSaveData){
..
dispatchEvent(new Event("closeThis"));
this.parent.removeChild(introScreen);
}
But when he's in the game and he wants to restore. He clicks on "esc" and the restore windows appears. But the IntroScreen IS NOT HERE. So when he restore his game, I've got an error with "this.parent.removeChild(introScreen);" as IntroScreen is not here.
I've tried this :
if(allSaveData){
..
dispatchEvent(new Event("closeThis"));
}
if (introScreen.visible == true){
this.parent.removeChild(introScreen);
}
But it's not working.
Do you know how I can tell the code "if introscreen is here close it, if not don't" ?
Thank you for your help !

visible is to show/hide a display object. You can't use this to determine whether the object is added in display list or not. There are a number of options to achieve this.
Option 1: Check the parent property of introScreen.
if (introScreen.parent) {
introScreen.parent.removeChild(introScreen);
}
Option 2: Use the contains method.
if (this.parent.contains(introScreen)) {
this.parent.removeChild(introScreen);
}
Option 3: Use try-catch block and simply ignore the error.
try {
this.parent.removeChild(introScreen);
} catch(err:Error) {
// ignore the error when it's not present
}

Try
introScreen.parent.removeChild(introScreen)

Related

Adobe Animate Actionscript 3.0 "clicktogotonextscene" code not working

So I have the following code for a button called "n2" and as you can see it's supposed to take me to the next scene when I click it, but it isn't working. It is inside Scene 2 and I'm trying to get to the next one and whenever I test the scene and click it, I don't even get an error, it just doesn't work.
Btw, it's a GIF defined as a movie clip and then as a button. Could that be causing the problem?
n2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);
function fl_ClickToGoToNextScene(event:MouseEvent):void
{
MovieClip(this.root).nextScene();
}
If you put a trace in the function and comment out "MovieClip(this.root).nextScene();" you could then see if the function gets called on a button click.
I've not used animate before, but in flash you have to name your button/movieclip and then also name the instance. This allows you to create the object twice with two seperate names to be referred to by actionscript.
Not sure if this helps but thought I'd give you some things to try, good luck

Actionscript to play/pause audio on different buttons

I've created a few buttons in Flash. I'm trying to make it so that if you click one button, the audio starts playing for that button. If you click another button, the active audio stops and the new audio of the button you clicked last start playing.
Any help please?
What you're describing is actually quite easy to do.
First things first, I recommend importing the audio into your Flash project. Alternatively, there is a way to play it directly from an external file. This is beyond the scope of my answer, so if you need help on that, you should post a question specifically covering it.
Assuming you have imported the audio file into your Flash project's library, make an as3 instance of it. (Right click the file in the library, click Properties --> ActionScript [tab] --> [Check] Export for ActionScript & [Enter name in] Class)
Now, create a definition of the sound in your code. (Assuming your two sounds were named "mySound1" and "mySound2" in the Class field of the previous step.)
var mySound1:Sound = new mySound1();
var mySound2:Sound = new mySound2();
Now, define your sound channel.
var mySoundChannel:SoundChannel = new SoundChannel();
There are two alternate ways of stopping one sound and playing another. The first is to create one function that does both every time. The second method is to create two formulas, one for "play" and one for "stop". You will need to decide which method works best for you. I'll use the two-function method below:
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
}
//In this function, we create an argument that allows us to tell the function
//what sound to we want it to play.
function playSound(soundname:String):void
{
mySoundChannel = this[soundname].play(0, 0);
}
[Note, you can tweak the play() properties to meet your needs, doing things like starting in the middle of the song, or looping it forever. 0,0 starts at the beginning, and doesn't loop. See the documentation for this.]
Now you hook up the event listeners for the buttons. (If you need help with event listeners, read the documentation.)
myButton1.addEventListener(Mouse.CLICK, btn1Click);
myButton2.addEventListener(Mouse.CLICK, btn2Click);
function btn1Click(evt:Event):void
{
stopSound();
playSound(mySound1);
}
function btn2Click(evt:Event):void
{
stopSound();
playSound(mySound2);
}
This should be enough information to get you started. In my game core, I actually have a custom class for dealing with sound playback that gives me the ability to repeat sounds, change volume, and keep sounds from conflicting with each other. I say that to emphasize that you can do quite a bit with the sound class. Do some digging in that documentation for ideas and help.
You may also consider putting a try-catch statement in the playSound function, since it will throw an reference error if you pass a name for a sound that doesn't exist.

detect when the client focus leave the window

i saw some online flash games can do this:
when you switch your current web to other windows or other web tabs, the application program will draw a black block and tell you've leave the application, click on the flash area to resume .
i've tried some event like focus in and out or mouse leave on the stage,but the reaction isn't what i expected.maybe i use them in the wrong way .please tell me if you got the solution.
var count:int = 0;
this.stage.addEventListener(Event.MOUSE_LEAVE,function(e:Event):void
{
//only be called if your mouse cursor leave the area,but can't detect whether you're actually switch to other program.
trace('mouseleave',count++);
});
this.stage.addEventListener(FocusEvent.FOCUS_OUT,function(e:Event):void
{
//no reaction
trace('focus out',count++);
});
this.stage.addEventListener(MouseEvent.ROLL_OVER,function(e:Event):void
{
//no reaction
trace('mouseenter',count++);
});
There are two methods:
Use Event.ACTIVATE event. But a swf should be on focus before.
Use JavaScript call from ActionScript to check is a window or a tab on focus.

Having issues with event on click

I have a button instance named "instructionButton" and I'm trying to trace "Clicked." to the output when it is clicked as a test but I haven' been successful thus far. Is there something I'm missing?
I'm using code in Flash Pro 6
import flash.events.MouseEvent;
var clickedVar:String = "Clicked.";
var runVar:String = "mice running...";
trace(runVar);
function instructionOpen(event:MouseEvent):void
{
trace(clickedVar);
gotoAndPlay(255);
}
instructionsButton.addEventListener (MouseEvent.CLICK, instructionOpen);
And of course if there's a more simple way to approach this, all knowledge will be helpful.
Check instance name is provided or not in the property window for the button (click the button and go to menu 'Window->Properties' to open property window)
What name is mentioned in the property window for the button, should use the same instance name in action script coding. Ensure the spelling from both script(code) and property window instance name.
I don't really see anything wrong with your button code, but here's how i do mine in AS3, it may help :) Creating a simple function within the event listener, I use stopPropgation to prevent my button from clicking anything that may be below it in the flash file. ( say you have two buttons on top of one another, you'll click both instead of one)
instructionsButton.addEventListener(MouseEvent.CLICK, function(e){
e.stopPropagation();
trace("Clicked.");
gotoAndPlay(255);
});
This is one button, if you need say fifteen, let me know as I have a code sample I'll give you that i use to create a limitless amount of buttons and eventlistners using switch/case which has been a huge help to me :)
The only way this will not work is if you are not reaching this frame.
Try add this code on your first frame and tell me if this helping.

how do I enable double click in action script?

I tried to do this :
panel.addEventListener(MouseEvent.DOUBLE_CLICK,showEditPopup);
but it is not working.
It works fine for
panel.addEventListener(MouseEvent.CLICK,showEditPopup);
So, I guess I have to enable double click first. Need help on it.
You have to enable double click for your panel before by doing this :
panel.doubleClickEnabled=true;
And then you can do :
panel.addEventListener(MouseEvent.DOUBLE_CLICK,showEditPopup);
I had this problem this morning trying to add a double click to my game.
This is what i found out.
First of all you gotta enable doubleclick like this:
panel.addEventListener(MouseEvent.DOUBLE_CLICK,showEditPopup);
You can also add the singe CLICK on the same function to prevent click to happen twice when double clicking, like this:
panel.addEventListener(MouseEvent.CLICK,showEditPopup);
Then this would be the function, interacting with both but 1 at a time:
function showEditPopup(e:MouseEvent) {
if (e.type == "click") {
//single click
} else if (e.type == "doubleClick") {
//double click
}
}
Now there is 2 problem that come up. First you gotta enable double click like this before the listener:
panel.enableDoubleClick = true;
Then the worse, if the display object is binded to another display object, which have a mouse event, you have to disable those event for the children so the double click work. Like this:
panel.mouseChildren = false;
That was bad for me because this is what i was doing.
Created a card with skills, skills had mouseevent.move_over to show a tooltip. But then i wanted to double click the card to place it/remove it from the deck. But it was not working because the skills, attached to that movieclip, had mouseevent in them. So i had to disable them and find another way to do it. Because tooltip wasnt showing with that mousechildren set to false and i had no choice to have this to bypass those event.
And this is why the timer solution seem a better idea indeed.You can go as up as 1 second for the wait time of a double click. It won't affect people and will fit even the slowest dude ;)