类包中的Action Script 3.0 Mouse事件

时间:2013-04-13 01:18:57

标签: actionscript-3 flash action flash-cs5.5

我在课堂上使用鼠标点击事件时遇到问题,我是Action Script的绝对新手。

我想要的是,如果我点击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();
        }
}

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

1 个答案:

答案 0 :(得分:1)

您的班级gunShip1没有属性btn_MClickroot或文档类。

基本上发生的事情是你将按钮放在舞台上,这使它成为属于根容器的实例。目前,您尝试将该按钮称为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它应该做什么。