1

在类中使用鼠标单击事件时遇到问题,我是动作脚本的绝对初学者。

我想要的是,如果我单击 btn_MClick 按钮,它应该运行脚本,但是每次单击它时,我都会收到错误消息,表明 btn_MClick 未定义。

btn_MClick 在舞台上,如果 btn_MClick 带有实例名称

public class gunShip1 extends MovieClip
{
    var moveCount = 0;

    public function gunShip1()
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, moveGunShip1);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, ShootGunShip1)
                    btn_MClick.addEventListener(MouseEvent.MOUSE_DOWN.KEY_DOWN,   ShootGunShip1);;

    }


function ShootGunShip1(evt: MouseEvent)
{


            var s_Bullet:survBullet = new survBullet();
            var stagePos:Point = this.localToGlobal (new    Point(this.width / 2-10, this.height));;
            s_Bullet.x = stagePos.x;
            s_Bullet.y = stagePos.y;

            parent.addChild(s_Bullet);
            //play sound
            var gun_sound:ricochetshot = new ricochetshot();
            gun_sound.play();
        }
}

拜托,我完全不知道该怎么做,不知何故,感觉整个过程都是错误的。

4

1 回答 1

1

您的类gunShip1没有 property btn_MClick, theroot或 document 类有。

基本上发生的事情是您已将按钮放在舞台上,这使其成为属于根容器的实例。目前,您正试图将按钮称为gunShip1.

您在这里真正应该做的是让按钮单击单独管理gunShip1,并让单独的代码调用gunShip1. 例如,你可以在你的文档类中有这个:

public class Game extends MovieClip
{

    private var _ship:gunShip1;


    public function Game()
    {
        _ship = new gunShip1();

        // The Document Class will have reference to objects on the stage.
        btn_MClick.addEventListener(MouseEvent.CLICK, _click);
    }


    private function _click(e:MouseEvent):void
    {
        _ship.shoot();
    }

}

然后你更新的shoot方法在gunShip1

public function shoot():void
{
    var s_Bullet:survBullet = new survBullet();
    var stagePos:Point = this.localToGlobal (new Point(this.width / 2 - 10, this.height));
    s_Bullet.x = stagePos.x;
    s_Bullet.y = stagePos.y;
    parent.addChild(s_Bullet);

    var gun_sound:ricochetshot = new ricochetshot();
    gun_sound.play();
}

这个想法是gunShip1不应该负责处理用户输入(鼠标,键盘等)。相反,它应该是一个单独的类,通知gunShip1它应该做某事。

于 2013-04-13T01:40:27.137 回答