简体   繁体   English

as3画圆类错误

[英]as3 draw circle class error

I am trying to create an event listener in a custom class. 我试图在自定义类中创建事件侦听器。 I have seen this in books but I get error "Access of undefined property on MouseDown". 我在书中已经看到了这一点,但出现错误“在MouseDown上访问未定义的属性”。 I have tried to abstract the issue as much as possible. 我试图尽可能抽象这个问题。 The command I am using to call my class is provided below. 下面提供了我用来调用课程的命令。 Thanks for any insights. 感谢您的任何见解。

package  
{
import flash.display.*;;
import flash.events.*;;

public class MouseDraw extends Sprite {

    public function MouseDraw() {
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);        
        }


    public function OnMouseDown(event:MouseEvent):void {
        trace("mouse down");        
    }                   

}   

}

//code in fla //在fla中编码

var mouseDraw:MouseDraw= new MouseDraw();
addChild(mouseDraw);

There is a typo in your code: the name of your handler ( OnMouseDown() ) begins with a capital letter, but you add an event listener on onMouseDown with a lower case. 代码中有一个错字:处理程序的名称( OnMouseDown() )以大写字母开头,但是您在onMouseDown上添加了一个小写的事件侦听器。 Rename your function to onMouseDown and it should work. 将您的函数重命名为onMouseDown ,它应该可以工作。

wow--got this to work after mad googling. 哇-疯狂搜索后,让它起作用。 thanks all who commented. 感谢所有发表评论的人。 Moral of the story--dont believe the code you see in books will compile! 这个故事的寓意-不要相信您在书中看到的代码会编译!

package  
{
import flash.display.*;;
import flash.events.*;;

public class MouseDraw extends Sprite {

    public function MouseDraw() {

        graphics.lineStyle(1);

        addEventListener(Event.ADDED_TO_STAGE, init);                       
        }


    private function init(e:Event):void {
        stage.addEventListener(MouseEvent.MOUSE_DOWN,  onMouseDown);                    
    }

    private function onMouseDown(event:MouseEvent):void {
        trace("mouse down");        
    }                   

}   

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM