0

我想更改位于另一个类中的 var。我得到了包含以下代码的类 c_wall.as:

package  {

import flash.display.MovieClip;
import flash.events.Event;


public class c_wall extends c_gameObject {
    public var speed:Number=10;

    public function c_wall() {








    }
    override public function update(){
        x-=speed;

    }
}

}

这个类是我的墙壁孩子的父母,它是一个在屏幕上移动的对象,每帧 10 像素(我的 var speed 的值)

在主类中,我得到以下代码来更改速度变量:

        Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

        faster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);

        function fl_TapHandler(event:TouchEvent):void
        {
            trace ("the speed has increased");
            speed++;//this is the speed he doesnt know

        }

现在我得到它不知道 var 速度的错误

我认为解决方案是将 var 包含在 update(); 功能。它也像这样在主文件中使用:

                function onEnterFrame(evt:Event):void
                {

                    wall.update(;

                    player.update();


                } 

但我不会让它工作......有人知道解决方案吗?

问候,梅林

4

1 回答 1

1

如果要将TouchEvent侦听器保留在主类中,请删除它尝试访问speed变量的行。然后,在定义变量的c_wall类中speed,覆盖事件侦听器回调函数并操作变量。

在主课中:

protected function fl_TapHandler(event:TouchEvent):void {
    //Do things relevant to the main class
}

c_wall课堂上:

override protected function fl_TapHandler(event:TouchEvent):void {
    super.fl_TapHandler(event); //Pass along the event to the parent 
    speed++;
}

当然,如果您在主类中根本不使用事件侦听器,您总是可以将其沿继承链向下移动到c_wall类并跳过覆盖。

于 2012-12-04T14:08:40.947 回答