从文档类文件设置TextField的文本

时间:2014-10-27 22:43:01

标签: actionscript-3 flash

我已经搜索了一段时间,没有什么对我有用。我试图与我的朋友(他正在做图形)进行闲置游戏,而我只是在学习AS3,所以我认为这是一个容易开始的项目。我不知道如何制作一个输出剩余健康状况的健康文本框。我搜索了至少一个小时,我找不到任何工作。这是我的所有编码。我为文本框嵌入了数字。文本框的实例名称为" health"所以它不难找到。

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

    public class Main extends MovieClip {
        public var originalValue:Number = 10;
        public var remainingValue:Number = uint(originalValue);
        public var clickDamage:Number = 1;
        public var health:TextField = String(originalValue);
        public var moneyCount:Number=0;
        public var mineralValue:Number=5;
        public var destroyedCount:Number=0;

        mineral.addEventListener(MouseEvent.CLICK, takeDamage);
        public function takeDamage(e:Event):void
        {
            remainingValue = remainingValue - clickDamage;
            if (remainingValue <= 0) {
                remainingValue = originalValue;
                moneyCount += mineralValue;
                destroyedCount++;
            }
            health.text=String(remainingValue);
        }
    }
}

到目前为止我所有的符号都是我的文本框(健康)和我的按钮(矿物质)。另外,文本框符号的实例名称与文本框内的实例名称之间的区别是什么?对不起,如果这是一个糟糕的问题,我真的很困惑,它让我感到困惑,因为我一直试图解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为你的主要问题是缺乏对类文件如何工作的理解。我在下面重写了你的类文件,并添加了一些评论:

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

    public class Main extends MovieClip {
        public var originalValue:Number = 10;
        public var remainingValue:Number = 10; //no need to convert to uint
        public var clickDamage:Number = 1;
        public var health:TextField; //you can't assign a string to a text field var //health = String(originalValue);
        public var moneyCount:Number=0;
        public var mineralValue:Number=5;
        public var destroyedCount:Number=0;

         //mineral.addEventListener(MouseEvent.CLICK, takeDamage);
         //in class files, you can't have lines of code outside of methods/functions create a constructor like below:

        //the constructor function must match the name of class, and will run whenever you create an instance of this class
        public function Main(){
            mineral.addEventListener(MouseEvent.CLICK, takeDamage);
            health.text = originalValue; //now assign the text to the text field
            //this assumes you have a text field on the main timeline with the instance name of "health"
        }

        public function takeDamage(e:Event):void
        {
            remainingValue = remainingValue - clickDamage;
            if (remainingValue <= 0) {
                remainingValue = originalValue;
                moneyCount += mineralValue;
                destroyedCount++;
            }
            health.text= remainingValue.toString();
        }
    }
}

如果(您的问题中的评论可能会闪烁),您将健康文字字段封装在符号/影片剪辑中,则需要像下面那样访问它:

myHealthSymbolInstanceName.myHealthTextBoxInstance.text = value;