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

时间:2014-12-22 19:42:10

标签: actionscript-3 flash oop

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

我试图这样做,以便当有人点击闪存中的“powerclton”符号时,另一个符号应该变得可见。这一切都在厨房课程中完成。

主要类的代码是我从后面的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给我一个未定义的访问问题,我不知道如何解决它。我找到类似主题的帖子,例如 - Access of Undefined property? Actionscript 3

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

1 个答案:

答案 0 :(得分:1)

当你在时间轴上编程时,代码引用本地命名空间,你在那里创建的对象(movieclips,textfields等)会自动在该命名空间中实例化,这样你就可以简单地调用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.