在课堂上使用鼠标
我创建了一个简单的类,它不是从任何其他 flash 类扩展的。问题是,在这个简单的类中使用鼠标协调。这是我的课程代码。
package src {
public dynamic class guider{
public function move01(xxx:int, yyy:int , speed:int) {
trace (mouseX);
return true;
}
}
}
编译后给出错误:“1120: Access of undefined property mouseX”,这是显而易见的。我尝试通过导入一些与鼠标相关的类来解决这个问题。所以我像这样重写我的代码:
package src {
import flash.utils.getDefinitionByName;
public dynamic class guider{
public function move01(xxx:int, yyy:int , speed:int) {
trace (getDefinitionByName("flash.ui.Mouse").mouseX);
return true;
}
}
}
现在编译时没有任何错误,但是当我使用它时,它跟踪出“未定义”!我这里有什么问题吗?
注意:此类用于与鼠标进行实时交互,我更喜欢使其简单以获得更高的性能。该函数的返回值将是一个基于鼠标 x 坐标和其他输入的数字。这里我使用“return true;”只是为了测试。
I make a simple class which is not extended from any other flash classes. The problem is, using mouse coordination inside this simple class. This is my code for the class.
package src {
public dynamic class guider{
public function move01(xxx:int, yyy:int , speed:int) {
trace (mouseX);
return true;
}
}
}
After compile it gives the error: “1120: Access of undefined property mouseX “ which is obvious. I try to figure it out with importing some classes related to mouse. So I rewrite my code like this:
package src {
import flash.utils.getDefinitionByName;
public dynamic class guider{
public function move01(xxx:int, yyy:int , speed:int) {
trace (getDefinitionByName("flash.ui.Mouse").mouseX);
return true;
}
}
}
Now there is not any error on compile but when I use it, it trace out “undefined”! What is my problem here?
Note: This class is used for real time interaction with mouse and I prefer to make it simple to get a higher performance. The return value of this function will be a number based on mouse x coordination and other inputs. Here I use “return true;” just for testing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在类的构造函数中,您应该传递舞台(Stage 类型),然后说:
编辑 1:
如果您想知道鼠标相对位置,则不能使用 Object(不扩展)作为基类到那个物体。要找到鼠标,您需要在舞台上显示该对象以找到鼠标所在的位置。因此,您可以使用 MouseX DisplayObject 的函数。因此,请扩展该片段而不是 MovieClip。或者您可以使用舞台再次找到鼠标。再次将阶段传递给构造函数中的类并将其保存在私有或受保护的变量中。
in the constructor of the class you should pass the stage (of type Stage) then say:
EDIT 1:
You cant use Object (extend none) as your baseclass if you want to know where the mouse is relative to that object. To find the mouse you need to show that object on the stage to find the position the mouse is in. Therefor you can use the MouseX function of the DisplayObject. So extend that one instead of MovieClip. Or you can find the mouse again by using the stage. Again pass the stage to the class in the constructor and save it in a private or protected var.