[英]Button Class by instance name, Actionscript-3
I'm so use to timeline code, that this baffles me. 我很习惯时间轴代码,这让我感到困惑。 How do I get a button class to recognize a button instance on stage?
如何获取按钮类以识别舞台上的按钮实例?
Please help. 请帮忙。
...Just revised ......刚刚修改过
Buttons.fla Buttons.fla
Class: 'Buttons'
Button with instance name placed on stage
Buttons.as Buttons.as
package {
import flash.display.MovieClip;
public class Buttons extends MovieClip {
public function Buttons() {
mc.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
stage.addChild(this);
}
public function onClick(event:MouseEvent):void{
trace("Hello World");
}
}
}
Error: 错误:
1120: undefined property 1120:未定义的属性
The error indicates it's the mouse event, not my instance name mc. 错误表明它是鼠标事件,而不是我的实例名称mc。
You are missing a curly brace and a definition of mc and an import of MouseEvent (the root of your problem above): 您缺少大括号和mc的定义以及MouseEvent的导入(上面问题的根源):
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Buttons extends MovieClip {
public function Buttons() {
//it's better to use "this" here instead of adding another
//instance of movieclip named "mc"
this.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
}
public function onClick(event:MouseEvent):void{
trace("Hello World");
}
}
}
Of course, there are several other/better ways to accomplish these same results, but that should at least fix your compile problem. 当然,还有其他几种/更好的方法可以实现这些相同的结果,但至少应该解决您的编译问题。 Now, to get this on stage, you need to add it to something that exists.
现在,要将其添加到舞台上,您需要将其添加到存在的内容中。 One simple way to do that is by putting the following line right below this.addEventListener :
一种简单的方法是将以下行放在this.addEventListener下面:
stage.addChild(this);
If you have other questions on getting this to work, let me know. 如果您对此工作有其他疑问,请告诉我。 I hope that points you in the right direction,
我希望指出你正确的方向,
--gMale --gMale
EDIT: 编辑:
In response to the comments below here is a link to the Flash files . 响应下面的评论,这里是Flash文件的链接 。 I tried to follow the intent of what you were doing.
我试图遵循你正在做的事情的意图。 There's one quick clickable button coded within the IDE and one quick clickable button coded in a separate *.AS file:
在IDE中编写了一个快速可点击按钮,在一个单独的* .AS文件中编码了一个快速可点击按钮:
This may help with the 'mc' instance 这可能有助于'mc'实例
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Buttons extends MovieClip {
public function Buttons() {
//it's better to use "this" here instead of adding another
//instance of movieclip named "mc"
this.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
}
public function onClick(event:MouseEvent):void{
trace("Hello World");
//PASS IT INTO THE BRACKETS
stage.addChild(mc);//<--------------------------
}
}
}
to access instances created in the IDE you need to call them with the [] syntax, this works : 要访问在IDE中创建的实例,您需要使用[]语法调用它们,这有效:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Buttons extends MovieClip {
public function Buttons() {
this["mc"].addEventListener(MouseEvent.MOUSE_DOWN, onClick);
//stage.addChild(this); // this is really not useful
}
public function onClick(event:MouseEvent):void{
trace("Hello World");
}
}
}
also note you need to import MouseEvent. 另请注意,您需要导入MouseEvent。 :)
:)
if you really need to be able to access your button via mc, it needs more code : 如果你真的需要能够通过mc访问你的按钮,它需要更多的代码:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Buttons extends MovieClip {
protected var mc:MovieClip;
public function Buttons() {
if(this["mc"] is MovieClip){
mc = this["mc"];
}else{
//you probably want to create it if not found on the stage.
}
mc.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
}
public function onClick(event:MouseEvent):void{
trace("Hello World");
}
}
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.