问题
I've created a button in my stage. I've set the instance name to init
and have the MainTimeline
being called from an an external script:
./Project/MyFlash.fla
./Project/MyFlash_fla/MainTimeline.as
I'm a beginner in Flash attempting to create a button and call it inside MainTimeline.as
; the error returned:
ReferenceError: Error #1065: Variable init is not defined.
at tfm::MainTimeline()
I've also tried var init:Button = new Button();
with no luck.
More specifically, I am trying to do this:
function MainTimeline(){
//var init:Button = new Button();
init.addEventListener(MouseEvent.CLICK, begin);
}
function begin(){
addFrameScript(0, frame1);
}
These are my imports (and I've added even useless ones in frustration):
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;
import flash.text.*;
import flash.xml.*;
import flash.media.*;
import fl.controls.Button;
import flash.*;
import fl.*;
import fl.controls.*;
Any possible solution?
Update:
I'm linking the external script under the stage property, Class
as MyFlash_fla.MainTimeline
I've gotten the 'var not defined' error eliminated by simply adding this.
preceding init.add[...]
. My current error: TypeError: Error #2007: Parameter listener must be non-null
at flash.events::EventDispatcher/addEventListener()
at tfm::MainTimeline()
. Sorry for not being completely clear at first, but I have a button in my stage, and I'm trying to have it execute the function, begin()
whenever clicked. Currently, that error is returned and begin()
is executed without any interaction.
Update II.
I've eliminated the last error by adding event:Event
in begin(event:Event){...
. Everything seems to be running smoothly. I'm left with an error though; it's not affecting anything, but I would still like to rid of it: ReferenceError: Error #1065: Variable init is not defined.
at tfm::MainTimeline/__setProp_init_Scene1_Layer1_0()
at tfm::MainTimeline()
- What is this?
回答1:
You will need to access the button through the main DisplayObjectContainer
(probably this
in your context, but difficult to know without more info)
Use the method getChildByName(name:String):DisplayObject
to reference the init
button.
var init:Button = this.getChildByName("init") as Button;
init.addEventListener(MouseEvent.CLICK, begin);
Should do what you want.
By the way, don't bother adding all those unused imports, your main problem is locating the init
Button's display list scope.
It would be helpful if you showed how you were linking in your external script too.
回答2:
I'm not sure if this will solve your problem, but if I understand correctly, you basically want to be able to control a stage instance from an external script.
If your "button" in this case is a flash object that you turned into a symbol, and chose to make the symbol of type "Button", then you'll want to make sure that, in the .fla file's library, you export that symbol for Actionscript.
Control/right click on the library item that you made and go to "properties." From there, you'll want to check off the box "Export for Actionscript." You'll want to give this custom button object a class name (and the thing on your stage will be an instance of this class).
Next: If your external .as document is called "MainTimeline.as", you'll probably want to make the document class of your .fla file "MainTimeline" (assuming it's in the same directory as your .fla file, which it appears to be). You can do this by clicking on the stage in your .fla file (or just deselecting everything else) and going into the properties menu box. In the part where it says Class: [blank text area], you'll want to fill in "MainTimeline". That will then mean that MainTimeline is the overall base class for this swf, and whatever instances you add to the stage should be accessible in this class (as if they were instances created by code in the actual .as file).
In that MainTimeline.as file, if you put the following text into it and your button becomes invisible when you compile and run your swf, that should probably indicate that things are working.
MainTimeline.as code to try out:
package
{
import flash.display.MovieClip;
public class MainTimeline extends MovieClip
{
public function MainTimeline()
{
init.visible = false;
}
}
}
Hope this helps!
来源:https://stackoverflow.com/questions/4382734/how-do-i-call-a-button-instance-in-an-external-as-script