无法在 AS3 中更改动态文本

Posted

技术标签:

【中文标题】无法在 AS3 中更改动态文本【英文标题】:Can't get dynamic text to change in AS3 【发布时间】:2017-09-17 19:10:51 【问题描述】:

这里是整个项目的链接,包括所有依赖项(如果需要)。 https://www.dropbox.com/sh/3d3towtkhb4c38r/AADdxDJq9ix7bK9hTkt_VTSAa?dl=0

基本上是在尝试清理一些我需要处理的脚本,并且一直在努力思考为什么这不起作用。我没有收到任何调试错误,一切似乎都链接到了需要链接的地方。我的字体似乎是嵌入的,我所有的实例名称似乎都是正确的......我唯一能想到的是我在代码中遗漏了一些愚蠢的东西。任何帮助将非常感激!没有什么比被弄得一团糟并且不得不修复它更喜欢的了。

基本上,文本框应该有一个基于即将到来的比赛的倒计时计时器(例如,比赛 X 在 xxx 分钟后),如果没有比赛临近,它应该显示一些默认文本。目前,它似乎根本不想更改动态文本!

编辑:我还要提到包含文本的 RaceRibbon Movieclip 也为 as3 导出到 com.RaceRibbon 类。

package  

    import flash.display.MovieClip;
    import flash.events.TimerEvent;
    import flash.ui.Mouse;
    import flash.utils.Timer;
    import com.boo.CustomDate;
    import com.boo.ScreensaverSimple;
    import com.RaceRibbon;

        // This sections is for the image slides and Hour Of Power setting.
    public class Generic extends MovieClip 

        // This is where you can set the Hour of Power time start and end time (in 24 hour format e.g. 1330 for 1:30pm)
        // If there is no hour of power, simply set both numbers to 0
        private var HourOfPowerStartTime:Number = 0;
        private var HourOfPowerEndTime:Number = 0;
        private var HourOfPower1StartTime:Number = 0;
        private var HourOfPower1EndTime:Number = 0;


        public var race_ribbon:RaceRibbon;      
        private var ss:ScreensaverSimple;
        public var time_check_timer:Timer;
        public var is_race_time:Boolean;
        public var current_state:String;
        public var next_race:Date;
        public var race_time_arr:Array;
        public var race_num:int;
        private var ss_time_arr:Array;      
        private var delay_add_timer:Timer;

        //Set default text
        private var default_ribbon_text:String = "THERACES.COM.AU";

        // Set Race Times
        private var r1:Date = new Date(2017,5,5,12,15);
        private var r2:Date = new Date(2017,5,5,12,50);
        private var r3:Date = new Date(2017,5,5,13,25);
        private var r4:Date = new Date(2017,5,5,14,00);
        private var r5:Date = new Date(2017,5,5,14,35);
        private var r6:Date = new Date(2017,5,5,15,15);
        private var r7:Date = new Date(2017,5,5,15,55);
        private var r8:Date = new Date(2017,5,5,16,35);
        private var r9:Date = new Date(2017,5,5,17,15);



        // Hide the mouse
        public function Generic() 
            Mouse.hide();

        // Set Race Ribbon
            race_ribbon = new RaceRibbon;
            race_ribbon.x = 1109;
            race_ribbon.y = 983;

            race_time_arr = [r1,r2,r3,r4,r5,r6,r7,r8,r9];

        // Display Slideshow
            ss = new ScreensaverSimple;
            ss.setScreensaver(screens);

        // Make sure Hour of Power is not visible
            HOP1.visible = false;
            HOP2.visible = false;



            time_check_timer = new Timer(1000);
            time_check_timer.addEventListener(TimerEvent.TIMER, checkTime);

            delay_add_timer = new Timer(1,1);
            delay_add_timer.addEventListener(TimerEvent.TIMER, addAllChildren);
            delay_add_timer.start();    
        


        public function addAllChildren(evt:TimerEvent=null):void 
            delay_add_timer.removeEventListener(TimerEvent.TIMER, addAllChildren);
            delay_add_timer.stop();
            delay_add_timer = null;

            addChild(race_ribbon);

            time_check_timer.start();
            checkTime();
        


        public function checkTime(evt:TimerEvent=null):void 
            setDatesToCurrent(race_time_arr);// This makes every day race day
            setDatesToCurrent(ss_time_arr);// This makes all screensaver dates current

            checkNextRace();

            if(next_race  != null && is_race_time == false)// If it isn't race time
            
                setCountdown();
            
            if(next_race  == null && is_race_time == true)// If it's race time
            
                setDefaultText();
            

            checkHOP1();
            checkHOP2();
        

        //Call to make Hour Of Power 1 visible/invisible based on set times
        private function checkHOP1():void 
            HOP1.visible = (HourOfPowerStartTime || HourOfPowerEndTime);
            if (!HOP1.visible) return;
            var CurrentTime:Number = CustomDate.return24HourNumber();
            HOP1.visible = (CurrentTime >= HourOfPowerStartTime && CurrentTime <= HourOfPowerEndTime);
        

        //Call to make Hour Of Power 2 visible/invisible based on set times
                private function checkHOP2():void 
            HOP2.visible = (HourOfPower1StartTime || HourOfPower1EndTime);
            if (!HOP2.visible) return;
            var CurrentTime:Number = CustomDate.return24HourNumber();
            HOP2.visible = (CurrentTime >= HourOfPower1StartTime && CurrentTime <= HourOfPower1EndTime);
        

        public function setDatesToCurrent(arr:Array):void // This makes every day race day
            var cd:Date = new Date();// Current Date

            for(var i:int=0;i<arr.length;i++)
                arr[i].fullYear = cd.fullYear;
                arr[i].month = cd.month;
                arr[i].date = cd.date;
            
               

        public function checkNextRace():void 
            var ct:Date = new Date();// Current Time as a Date
            next_race = null;
            is_race_time = false;

            // FOR LOOP THIS... one day
            if(ct < r1) 
                next_race = r1;
             else if(raceTimeSpan(r1) == true) 
                is_race_time = true;
                race_num = 1;
             else if(ct > raceTimeSpan(r1) && ct < r2) 
                next_race = r2;
             else if(raceTimeSpan(r2) == true) 
                is_race_time = true;
                race_num = 2;
             else if(ct > raceTimeSpan(r2) && ct < r3) 
                next_race = r3;
             else if(raceTimeSpan(r3) == true) 
                is_race_time = true;
                race_num = 3;
             else if(ct > raceTimeSpan(r3) && ct < r4) 
                next_race = r4;
             else if(raceTimeSpan(r4) == true) 
                is_race_time = true;
                race_num = 4;
             else if(ct > raceTimeSpan(r4) && ct < r5) 
                next_race = r5;
             else if(raceTimeSpan(r5) == true) 
                is_race_time = true;
                race_num = 5;
             else if(ct > raceTimeSpan(r5) && ct < r6) 
                next_race = r6;
             else if(raceTimeSpan(r6) == true) 
                is_race_time = true;
                race_num = 6;
             else if(ct > raceTimeSpan(r6) && ct < r7) 
                next_race = r7;
             else if(raceTimeSpan(r7) == true) 
                is_race_time = true;
                race_num = 7;
             else if(ct > raceTimeSpan(r7) && ct < r8) 
                next_race = r8;
             else if(raceTimeSpan(r8) == true) 
                is_race_time = true;
                race_num = 8;
             else if(ct > raceTimeSpan(r8) && ct < r9) 
                next_race = r9;
             else if(raceTimeSpan(r9) == true) 
                is_race_time = true;
                race_num = 9;
             else if(ct > raceTimeSpan(r9)) // If all races are finished
                setDefaultText();
            
        

        public function raceTimeSpan(d:Date):Boolean 
            var race_mins:int = 2;
            var race_on:Boolean = false;
            var ct:Date = new Date();
            if(ct.hours == d.hours) 
                var max_mins:int = d.minutes + race_mins;
                if(ct.minutes >= d.minutes && ct.minutes < max_mins) 
                    race_on = true;
                
            
            return race_on;
        

        public function setCountdown():void 
            var hours_left:int = int(String(CustomDate.countdownTime(next_race)).split(":")[0]);
            var mins_left:int = int(String(CustomDate.countdownTime(next_race)).split(":")[1]);
            mins_left = (60*hours_left)+mins_left;
            is_race_time = false;
            if(mins_left > 2) 
                race_ribbon.setText("NEXT RACE IN <font color='#000000' letterspacing='-1'>"+(mins_left+1)+" MINUTES</font>");
                race_ribbon.setBG(0);
             else if(mins_left < 3) 
                if((mins_left+1) <= 1) 
                    race_ribbon.setText("NEXT RACE IN <font color='#fdb913' letterspacing='-1'>"+(mins_left+1)+" MINUTE</font>");
                 else 
                    race_ribbon.setText("NEXT RACE IN <font color='#fdb913' letterspacing='-1'>"+(mins_left+1)+" MINUTES</font>");
                
                race_ribbon.setBG(1);
            
        

        public function setDefaultText():void 
            race_ribbon.setText("<font color='#fdb913' letterspacing='-1'>"+default_ribbon_text+"</font>");
            race_ribbon.setBG(1);
               

    


如果您不想单独浏览文件,com.RaceRibbon 代码是

package com 

    import flash.display.MovieClip;
    import fl.motion.Color;
    import flash.display.Sprite; 
    import flash.text.*; 

    public class RaceRibbon extends MovieClip 

        private var c1:Color;
        private var c2:Color;

        public function RaceRibbon() 
            c1 = new Color();
            c1.setTint(0xfdb913,1);

            c2 = new Color();
            c2.setTint(0x000000,1);
        



        public function setText(str:String=""):void 
            str = str.toUpperCase();
            this.tb.htmlText = str;
        

        public function setBG(p:int=0):void 
            switch(p)
                case 0:
                    bg.transform.colorTransform = c1;
                break;
                case 1:
                    bg.transform.colorTransform = c2;
                break;
            
        

    


【问题讨论】:

只是一个疯狂的猜测,功能区的 (x,y) 是 ~ (1100,980),它在屏幕外(除非它是无比例的全屏)。 文件是 1920x1080 所以除非我弄错了(我很高兴弄错了,如果我没记错的话,应该是右下角?功能区本身正在显示(包含文本的电影剪辑),文本只是没有被修改。 好吧,我没有看到 tb 声明或创建,所以有很多可能性。现在我建议在 RaceRibbon 构造函数中使用 trace(describeType(this));,并使用他们的 name 跟踪所有孩子的列表属性和 getQualifiedClassName(...) 以了解它们所属的确切类。 当我尝试追踪时,我得到以下代码。 tb 已经在时间表上,所以除非我弄错了,否则不需要其他任何东西? TypeError: Error #1009: Cannot access a property or method of a null object reference. at Generic/setDatesToCurrent() at Generic/checkTime() at Generic/addAllChildren() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick() 文件 > 发布设置 > 允许调试以了解出现该错误的行号。关于“不应该需要其他任何东西”:您可能有重复项、错误的对象类型或其他内容。基本上,在类中故意将其声明为 public var tb:TextField; 不会造成伤害,并且是使用设计实例的一般良好实践。 【参考方案1】:

我没有机会测试您的项目文件,只是对消除的建议:

要么手动触发setDefaultText();,要么直接针对RaceRibbon 的文本字段(因此只需将// 注释掉您未从显示的3 行代码中测试的另外两个)。

如果下面的任何Update test 可以正常更新文本,那么您真正的问题在于:

IF 声明function setCountdown():(if(mins_left &gt; 2) 之前trace ("minutes left: " + mins_left); 说的金额是什么?看起来它可能会变成&gt; 2&lt; 3+1 &lt;= 1?如果不是,那么你的文本也不会更新。) 或function checkNextRace()(特别是如果你设置next_race = null;,那么在next_race = r1;之后肯定会给出一个调试器错误,如“无法将值设置为NULL(不存在)的变量” 但你说你没有错误,所以我很困惑为什么我会这样做。(使用ctrl+shift_+enter 来调试不是Test Movie 选项)。

反正代码...

public function Generic() 
    Mouse.hide();

    // Set Race Ribbon
    race_ribbon = new RaceRibbon;
    race_ribbon.x = 1109; race_ribbon.y = 983;

    // Update test #1
    setDefaultText(); //did via Function work?

    // Update test #2
    race_ribbon.tb.htmlText = "testing123"; //did via Direct Targeting work?
    race_ribbon.tb.htmlText = "<font color='#fdb913' letterspacing='-1'>" + "testing123" + "</font>"; //maybe works with HTML?

    // Temp disable other code for above elimination tests    
    /*
    race_time_arr = [r1,r2,r3,r4,r5,r6,r7,r8,r9];

    // Display Slideshow
    ss = new ScreensaverSimple; ss.setScreensaver(screens);

    // Make sure Hour of Power is not visible
    HOP1.visible = false; HOP2.visible = false;

    time_check_timer = new Timer(1000); 
    time_check_timer.addEventListener(TimerEvent.TIMER, checkTime);

    delay_add_timer = new Timer(1,1);
    delay_add_timer.addEventListener(TimerEvent.TIMER, addAllChildren);
    delay_add_timer.start();
    */  

【讨论】:

好的...所以调试控制台将我带到了我认为没有做任何事情的代码行(它不是...除了破坏所有后续代码!!!)我尝试了其他代码,它实际上使丝带消失了!当我删除这些行时,一切都恢复了并开始工作。我觉得自己真的很愚蠢,因为我应该先这样做,我想我以为我做到了,再也没有检查过,但是你让我回到了正轨,让我摆脱了很多麻烦!!!再次感谢您!

以上是关于无法在 AS3 中更改动态文本的主要内容,如果未能解决你的问题,请参考以下文章

as3 文本字段更改字符串值

不能有 2 个动态文本字段 AS3

Flex:在 AS3 中更改 Flex 组件样式

AS3.0中影片剪辑元件嵌套了个动态文本框,怎样才能在文本框上也能显示手型?

如何在Android中动态更改文本大小

如何在 iPhone 中动态更改 UITextView 中文本的字体大小