0

这可能真的很简单,我一生都无法弄清楚为什么这不起作用。我正在尝试创建一个对象(仅用于测试)并在构造函数中分配事件侦听器。在我的脑海中它应该可以工作,但我确信我必须遗漏一些东西:

package  {

import flash.display.MovieClip;
import flash.events.MouseEvent; 

public class Box extends MovieClip {

    public function Box() {
        // constructor code
        var mySound:Sound = new Bark();
        trace("Box created");
        height=800;
        width=300;
        x=100;
        y=100;
        addEventListener(MouseEvent.MOUSE_OVER, overThis);
        addEventListener(MouseEvent.CLICK, clickToPlay);
    }

    public function overThis(m:MouseEvent){
        trace("SADF");          
    }

    function clickToPlay(m:MouseEvent){
        mySound.play();
    }

}}

通过这样做,我希望“盒子”在管理自己的事件方面能够自给自足。(请忽略 play() 之类的东西,当我直接在 MAINDOC.as 中运行时,它们都可以正常工作。

这是主要文档:

package {
import flash.display.MovieClip;
import flash.media.Sound;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;

public class MainDoc extends MovieClip
{
    public function MainDoc()
    {
        // constructor code
        init();

        function init()
        {
            createBox(300,300);
        }

        function createBox(newX,newY)
        {
            var box = new Box();
            box.x = newX;
            box.y = newY;
            addChild(box);
        }

    }
}}

当我测试它创建框(我已经绘制)但不运行任何事件?

希望大家能帮忙

射线

4

1 回答 1

2

您必须在框中添加一些图形,以便您的 MouseEvents 可以工作:

public function Box() 
{
    // constructor code
    var mySound:Sound = new Bark();
    trace("Box created");

    // begin the filling of some graphics, you can change color/alpha as you want
    this.graphics.beginFill( 0x000000, 1 );
    // make a rectangle 300x800
    this.graphics.drawRect(0,0,300,800);
    // stop filling
    this.graphics.endFill();


    // you don't need it anymore
    //height=800;
    // you don't need it anymore
    //width=300;

    // place your clip where you want but you do that in the Main class so no need there
    //x=100;
    //y=100;

    // now you have graphics attached to your MovieClip the MouseEvent must work
    addEventListener(MouseEvent.MOUSE_OVER, overThis);
    addEventListener(MouseEvent.CLICK, clickToPlay);
}

希望对你有帮助:)

于 2013-11-13T11:07:44.147 回答