AS3, Flash - Function to go to label? - actionscript-3

I was thinking of making a function which can be called and will send to the called label..
Well, I currently do this for all buttons:
myButtonSomething.addEventListener(Event, GoToLabelSomething);
function GoToLabelSomething (e:Event):void{
this.gotoAndStop("Something");
}
Would it not be possible to do so that.. Like in PHP it would be this:
GoToLabel("something");
function GoToLabel($label) {
// gotoAndStop($label); or something....
}
I hope I make sense :D

Are you looking for something like this :
var labelName:String = "something";
myButtonSomething.addEventListener(Event, function(e:Event){
GoToLabel(labelName);
});
function GoToLabel (name:String):void{
this.gotoAndStop(name);
}

Related

Actionscript delay inside function

I created a button in Adobe Animate which should, if you press him be exchanged with another button in a different color, after 1 second you should be forwarded to the previous scene.
My Code:
button_answer_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
function fl_ClickToGoToPreviousScene(event:MouseEvent):void
{
button_answer_2.visible = false;
button_answer_2_red.visible = true;
setTimeout(myDelayedFunction,3000);
function myDelayedFunction(){
MovieClip(this.root).prevScene();
}
}
sadly the myDelayedFunction doesn't work because it is inside another fuction, I can't see analternative way. Could anyone help me? (I am not the best programmer so keep it simple if possible)
Thank you for your answer
Then why not simply move the function out of the other one. There was never a reason to nest them to begin with.
button_answer_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
function fl_ClickToGoToPreviousScene(event:MouseEvent):void
{
button_answer_2.visible = false;
button_answer_2_red.visible = true;
setTimeout(myDelayedFunction,3000);
}
function myDelayedFunction():void
{
MovieClip(this.root).prevScene();
}
Use inline function and test again, I can not test it now:
setTimeout(function(){ MovieClip(this.root).prevScene(); }, 3000);

AS3 Get Functions

So I've got an AS3 AS file with a function like this:
static function get instance() {
if (__instance == null) {
__instance = new com.disney.dlearning.managers.DLSManager();
}
return(__instance);
}
In another AS file, I got to call it like this:
function phoneClosing()
{
if(this.__isOpen)
{
this.__isOpen = false;
com.disney.dlearning.managers.DLSManager.__get__instance().pushOpcodeToHost(com.disney.dlearning.client.phone.DLearning.HOST,"STOP",com.disney.dlearning.client.phone.DLearning.CP_SPY_PHONE,this.dlsmCallback,"","","","");
}
}
And every time I get an error like this:
C:\Users\USername\CPSWF\Flippr AS3\Client\interface\com\disney\dlearning\client\phone\DLearning.as, Line 31 There is no method with the name '__get__instance'.
How can I successfully call this function? It seems I likely just need to know what to put instead of __get__instance. Thanks,
-Sandor
I believe all you need to call is com.disney.dlearning.managers.DLSManager.instance to access that singleton. I did AS3 programming for three years but its been a while so I could be wrong...

Can I pass a button into its own MouseEvent function?

I have multiple MovieClip Symbols published with Flash into FlashDevelop (I'll only use 2 in my example). Each have 3 frames for default, hover and click that I'm using as buttons.
private var btnPlay:PlayButton, btnQuit:QuitButton;
btnPlay = new PlayButton();
btnQuit = new QuitButton();
btnPlay.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
btnPlay.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
btnPlay.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);
btnPlay.buttonMode = true;
btnPlay.useHandCursor = true;
function onRollOverHandler(myEvent:MouseEvent):void {
btnPlay.gotoAndStop(2);
}
function onRollOutHandler(myEvent:MouseEvent):void {
btnPlay.gotoAndStop(1);
}
function onPressHandler(myEvent:MouseEvent):void {
btnPlay.gotoAndStop(3);
}
function onReleaseHandler(myEvent:MouseEvent):void {
btnPlay.gotoAndStop(2);
}
// Same code for btnQuit here, but replace btnPlay with btnQuit
Instead of adding new EventListeners to every button that do practically the same thing like what I'm doing above, is there a way I could just pass in the button itself to the MouseEvent functions something like this? (I realize this might be difficult since all buttons are their own class)
btnPlay.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler(btnPlay));
btnPlay.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler(btnPlay));
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler(btnPlay));
btnPlay.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler(btnPlay));
function onRollOverHandler(myEvent:MouseEvent, inButton:MovieClip):void {
inButton.gotoAndStop(2);
}
function onRollOutHandler(myEvent:MouseEvent, inButton:MovieClip):void {
inButton.gotoAndStop(1);
}
function onPressHandler(myEvent:MouseEvent, inButton:MovieClip):void {
inButton.gotoAndStop(3);
}
function onReleaseHandler(myEvent:MouseEvent, inButton:MovieClip):void {
inButton.gotoAndStop(2);
}
Maybe I am misunderstanding, but "event.target" provides you a reference to the button that has been clicked. So if you want to do something to the clicked button, you would write:
myEvent.target.gotoAndStop(1);
Or sometimes you might need to use "currentTarget". You'd still need to create listeners for each function but could use one set of handlers.
Simple answer: No. You could go to some trouble to override the MouseEvent class and allow it to send additional parameters, but why bother in this case? You don't seem to be saving any code.
SLIGHT UPDATE:
Here's a possibly-useful simplification of your original code. It saves a few lines-of-code and uses just a single handler function. Obviously, the 'trace' statements could be replaced by various 'gotoAndStop()' statements:
btnPlay.addEventListener(MouseEvent.ROLL_OVER, HandleAll);
btnPlay.addEventListener(MouseEvent.ROLL_OUT, HandleAll);
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, HandleAll);
btnPlay.addEventListener(MouseEvent.MOUSE_UP, HandleAll);
function HandleAll(e)
{
if (e.type == "rollOver") trace("rollover");
if (e.type == "rollOut") trace("rollout");
if (e.type == "mouseDown") trace("mousedown");
if (e.type == "mouseUp") trace("mouseup");
}

Evaluate where a function call originated from

Okay so I have a function called changeHandler - it is called by several eventListeners in other functions. I want to write several if statements that evaluate the source of function call and change the dataProvider of my ComboBox depending on the originating function. Example: one of the many functions is called displayCarbs() and has an eventListener like so:
function displayCarbs(event:MouseEvent):void {
myComboBox.addEventListener(Event.CHANGE, changeHandler);
}
(I've removed all of the unnecessary code from the function above)
The if statement inside the changeHandler will look something like this:
if (****referring function = displayCarbs****) {
myComboBox2.dataProvider = new DataProvider(carbItems);
}
I've searched high and low for something that can achieve this, but I just don't have a good enough grasp of AS3 or vocabulary to describe what describe what I mean to get the answer from Google.
The simplest way I can think of... Couldn't you simply create a text string that updates to the name of function before going to changeHandler then in turn changeHandler can check string content and act accordingly..
public var referring_function:String;
function displayCarbs(event:MouseEvent):void
{
referring_function = "displayCarbs";
myComboBox.addEventListener(Event.CHANGE, changeHandler);
}
function displayCarbs(event:Event):void
{
if (referring_function == "displayCarbs")
{ myComboBox2.dataProvider = new DataProvider(carbItems); }
if (referring_function == "displayOthers")
{ myComboBox2.dataProvider = new DataProvider(otherItems); }
// etc etc
}
I cant remember right now if you need == or just = when checking the If statement against strings.
I know there is an accepted answer already, but based on what I gleaned about the problem, here is a solution that wouldn't require adding another variable to check :
function displayCarbs(event:MouseEvent):void
{
myComboBox.addEventListener(Event.CHANGE, changeHandler);
}
function changeHandler(event:Event):void
{
var comboBox:ComboBox = event.target as ComboBox;
if (comboBox.dataProvider == uniqueProvider)
{
myComboBox2.dataProvider = new DataProvider(appropriateItems);
}
}
This should work if the second dataProvider is determined based on the first dataProvider. This of course requires that your uniqueProvider is a class member variable so it has scope within the handler.

How to keep AS3 code simple

This is my original pseudo code:
function1();
function1():void{
//do something
after mouseclick do function2
}
function2():void{
//do something
after animationfinish do function3
}
etc..
Can I get it into something like this?:
function1();
after mouseclick do function2()
after animationfinish do function3()
What is an easy way to get listeners on the top level?
If I understand your question correctly, when function1 is called you want to addEventListener(MouseEvent.CLICK, function2);
Are you using Actiosncript to animate or Keyframes?
I know with TweenMax you can add a function to call at the end of the tween. If using fl.Transtions.Tween you can listen for TweenEvent.MOTION_FINISH and then call function3. I don't mess much with the timeline in Flash, since I feel more in control with coding it, but I know you can add code at the last frame of an animation to call function3.
If I am completely missing you question feel free to clarify what exactly you are seeking.
If i good understand - You like to create functions chain .
I depends what You like to do , problem is in many possibilities.
For call few functions i use this class :
https://github.com/turbosqel/as3SupportLib/blob/master/as3SupportLib/src/turbosqel/utils/CountCall.as
You can also use deeper and more elastic way and add next callbacks to function :
var func:Function = function():void { // declare new function
... function body // your class actions
for each(var call:Function in arguments.callee){ // get functions
call(); // call function
}
}
func["someFunction"] = someFunction; // add function as dynamic value
func["otherFunction"] = anotherFunctionToCall; // add another function
func(); // call function