AS3将var从主类传递给另一个类

时间:2015-03-30 08:02:09

标签: actionscript-3

我的代码有效,但它没有在最后一个函数中获取VAR。我错过了什么?

主类代码

package {

    import flash.display.MovieClip;
    import flash.events.*;

    import com.Bubble;


    public class numberPop extends MovieClip 
    {

        public var numberBubble:Bubble;

        public function numberPop() 
        {

            addBubble(3);

        }


        public function addBubble(num:Number)
        {
            var i:Number = num;         
            numberBubble = new Bubble(i);
            addChild(numberBubble);
        }

    }

}

第二类代码:Bubbles.as

package com {

    import flash.display.MovieClip;
    import flash.events.*;


    public class Bubble extends MovieClip 
    {

        public var num:Number;

        public function Bubble(num:Number) 
        {
            super();
            num = num;
            trace("Number is: " + num); /// WORKS !!!!!

            this.addEventListener(MouseEvent.CLICK, bubbleAction);



        }

        public function bubbleAction(e:Event)
        {
            trace(num); // DOES NOT WORK BUT SHOULD. 
        }

    }

}

1 个答案:

答案 0 :(得分:1)

构造函数中的参数覆盖了类属性。变化:

num = num;

this.num = num;