访问 AS3 中未定义的属性问题



我在使用某些AS3时遇到了一些麻烦。第一次使用这种语言,并且在 Web 开发方面有更多的经验,然后是 OOP,所以我有点困惑。

我正在尝试使它,以便当有人单击"电源按钮"(即闪存中的"电影剪辑"符号(时,另一个符号应该变得可见。这一切都是在厨房课上完成的。

主类的代码是我从我关注的 youtube 教程视频中获得的;

package  {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.events.Event;
    import Kitchen
public class DragFood extends MovieClip
{       
    protected var originalPosition:Point;
     var myKitchen:Kitchen 

    public function DragFood() {
        myKitchen = new Kitchen;
        originalPosition = new Point (x, y);
        buttonMode = true;
        addEventListener (MouseEvent.MOUSE_DOWN, down);
    }
    protected function down (event:MouseEvent):void
    {
        parent.addChild(this);
        startDrag();
        stage.addEventListener (MouseEvent.MOUSE_UP, stageUp);
        }
    protected function stageUp (event:MouseEvent):void
    {
        stage.removeEventListener (MouseEvent.MOUSE_UP, stageUp);
        stopDrag();

        if (dropTarget)
        {
            if(dropTarget.parent.name == "bowl")
            {
                trace("The " + this.name + " is in the bowl");
                this.visible = false;
                } else {
                    returnToOriginalPosition();
                    }
        } else {
        returnToOriginalPosition();
        }
    }
    protected function returnToOriginalPosition():void
    {
            x = originalPosition.x;
            y = originalPosition.y;
    }
}
}

在其中,我称另一个类;

 import Kitchen
public class DragFood extends MovieClip
{       
    protected var originalPosition:Point;
     var myKitchen:Kitchen 

厨房类的代码是;

包 {

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

public class Kitchen extends MovieClip
{   
    // This is a function. This particular function has the same name as our class and therefore will be executed first
    public function Kitchen()
    {
        // This is a "call" to another function that is defined later in the program.
        init();
        trace("Hello world");
    }
    public function init():void
    {

        // If we want an object (on the screen or otherwise) to be notified about an event we must add a listener for that event to that object.            
        // We also need to specify what happens everytime the event we are listening for happens.
        PowerButton.addEventListener(MouseEvent.CLICK, handleButtonClicks);
    }
    //This function is called when the oven on button recieves a click.
    public function handleButtonClicks(event:MouseEvent):void
    {
        OvenOn.visible = true;
        trace("the oven is being switched on");
    }

}

}

我一直遇到的问题是 OvenOn 和 PowerButton 给了我一个未定义的访问问题,我不确定如何解决它。我发现了类似主题的帖子,例如 - 访问未定义的属性?操作脚本 3

但是我不太确定如何将其应用于我的问题,如果有人可以提供任何很棒的帮助。

在时间轴上编程时,代码引用本地命名空间,并且您在那里创建的对象(影片剪辑、文本字段等(会自动实例化在该命名空间中,以便您可以简单地调用OvenOn.visible = true。 但是,对于每个类,它们的本地命名空间是类中的任何内容,因此除非您实际在类上创建了名为 OvenOn 的属性,否则它肯定会给您Access of Undefined Property错误。

将每个类视为自己的岛屿。 为了让他们互相接触,他们需要某种联系。 一旦父级在其自己的命名空间中实例化类,就可以建立该连接。例如。。。

var foo:String = "Hello!";
var bar:MyClass = new MyClass();
// At this point, whatever code runs inside of MyClass has no concept of foo, or how to access it.
addChild(bar);
// Now that we've added it to the stage, the bar has some properties that have automatically been populated such as "root", "parent", or "stage".
foo.someProperty = "World";
// Since this namespace has a variable pointing to the instance, we can change properties on that class.

现在我们已经在舞台上实例化了 MyClass,我们可以引用类不知道的父属性。 请注意,这不一定是最佳实践。

package
    public class MyClass extends MovieClip {
        var someProperty:String = "cheese";
        public function MyClass() {
            trace(parent.foo) // this will fail
            addEventListener(Event.ADDED_TO_STAGE, test);
        }
        public function test(e:Event):void {
            trace(this["parent"].foo); // this will succeed 
        }
    }
}

如果绝对必须更改不属于 Kitchen 类的内容,请将 OvenOn 的父级或该对象作为 Kitchen 的属性传递。 您可以通过几种方式执行此操作。

与构造函数...

var something:*;
public function MyClass(someObject:*) {
    something = someObject;
}
public function test():void {
    something.visible = false;
}

。或通过分配属性...

var bar:MyClass = new MyClass();
bar.something = OvenOn;
bar.test(); // will turn off the OvenOn now that 'something' is pointing to it.

最新更新