通过静态类型访问未定义的属性

时间:2014-04-08 16:57:05

标签: actionscript-3 flash

我收到错误:通过带有静态类型Gold的引用访问可能未定义的属性destinationY。我没有在Gold类中使用destinationY属性。只在主要班级。 我仍然是类的业余爱好者,并为对象本身定义属性。请帮忙。谢谢!

这是Main类:

package
{   

    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.Event;
    public class Main extends MovieClip
    {
        private var field:Array;
        public var gold:Gold;
        public var goldContainer:Sprite = new Sprite();
        private var goldTimer:Timer = new Timer(2000);


        public function Main():void
        {
            setupField();

            goldSet();
            addEventListener(Event.ENTER_FRAME,onEnterFrm);
        }

        private function goldSet():void
        {   addChild(goldContainer);
            goldTimer.start();
            goldTimer.addEventListener(TimerEvent.TIMER, newGold);

        }

        private function newGold(e:TimerEvent):void {
            var goldRow:int=Math.floor(Math.random()*5);
            var goldCol:int=Math.floor(Math.random()*9);
            gold = new Gold(this);
            gold.buttonMode=true;
            goldContainer.addChild(gold);
            gold.x=30+goldCol*65;
            gold.destinationY = 35+goldRow*75;
            gold.y=-2


        }       


        private function onEnterFrm(e:Event):void {
            for (var i:uint=0; i<goldContainer.numChildren; i++) {
                var fallingGold:Gold=goldContainer.getChildAt(i) as Gold;
                if (fallingGold.y<fallingGold.destinationY) {
                    fallingGold.y++;
                } else {
                    fallingGold.alpha-=0.01;
                    if (fallingGold.alpha<0) {

                        goldContainer.removeChild(fallingGold);
                    }
                }
            }
        }
    }
}

以下是金级:

package
{

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

    public class Gold extends MovieClip
    {
        private var _main:Main
        private var gold:Gold;


        public function Gold(main:Main):void 
        {

            _main=main;
            addEventListener(MouseEvent.CLICK, goldClicked);

        }

        private function goldClicked(e:MouseEvent):void
        {
            e.currentTarget.removeEventListener(MouseEvent.CLICK,goldClicked);
            _main.goldContainer.removeChild(e.currentTarget as Gold);
        }

    }
}

2 个答案:

答案 0 :(得分:0)

乍一看,您的destinationY课程中没有Gold个属性。 MovieClip也没有这个属性,所以你试图操纵一个不存在的变量。

除非这不是你向我们展示的整个班级。当然。

详细说明

  

我没有在Gold类中使用destinationY属性。只在主要   类。

是的,但您正试图在主要类的Gold Class 中操纵名为destinationY的属性。

如果你这样做:

public class Gold extends MovieClip
    {
        private var _main:Main
        private var gold:Gold;
        public var destinationY:int = 0;
        ...
    }

你不应该再犯这个错误,但我怀疑这会解决你所有的问题。

答案 1 :(得分:0)

在Main类的第xx行,你有代码:

gold.destinationY = 35+goldRow*75;

您正在尝试访问Gold类的“destinationY”属性,但Gold类没有名为destinationY的公共属性。要添加它,只需将此行添加到Gold类:

public var destinationY;