1

所以,我有一个名为 bg 的电影剪辑,其中有一个名为 sleep_btn 的按钮。我正在舞台上的一个图层上进行编码,它是这样的:

sleep_btn.addEventListener(MouseEvent.CLICK, sleepClick);
function sleepClick(event:MouseEvent):void{
    health = 100;
    day += 1;
}

我很快意识到如果没有在编码中定义电影剪辑,它将无法工作,所以我尝试了:

bg.sleep_btn.addEventListener(MouseEvent.CLICK, sleepClick);
function sleepClick(event:MouseEvent):void{
    health = 100;
    day = day + 1;
}

我的错误消失了,但我发现当我点击按钮时,健康和日子保持不变。

这一天在文本字段中,编码如下:

var day:int = 1;
if (day==1) date.text = "July 1";
if (day==2) date.text = "July 2";
if (day==3) date.text = "July 3";

不断地

健康是一个 101 帧的影片剪辑,其编码如下:

var health:int = 100;
   lifebar.gotoAndStop(health + 1);

编辑

顶部横幅层:

stop();
var health:int = 100;
   lifebar.gotoAndStop(health + 1);
    //Can write this also: lifebar.health += 45;
    trace("health is "+health);
    trace("day is "+day);

var day:int = 1;
updateDay();

function updateDay():void{
if (day==1) date.text = "July 1";
if (day==2) date.text = "July 2";
if (day==3) date.text = "July 3";
if (day==4) date.text = "July 3";
}

fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
function fortyfivedownClick(event:MouseEvent):void{
    if (health < 45) {
       return;
    }
    health -= 45;
    if(health < 0) health = 0;
    else if(health > 100) health = 100;
    lifebar.gotoAndStop(health + 1);
     trace("health is "+health);
}

bg.sleep_btn.addEventListener(MouseEvent.CLICK, sleepClick);
function sleepClick(event:MouseEvent):void{
    health = 100;
    day = day + 1;

    //update lifebar
    lifebar.gotoAndStop(health + 1);

    //update day
    updateDay();
}
4

2 回答 2

0

是在舞台上展示吗healthday它们是什么类型的?您是否将 TextField 的文本设置为它们的值?

如果它们只是变量并且它们没有被 UI 显示,您将不会看到它们发生变化。

于 2012-07-16T20:41:00.370 回答
0

您没有使用新数据更新实际的 UI 文本框。将您的日期确定代码(可以大量清理,但这超出了本问题的范围)移动到自己的函数中,以便可以重用:

var day:int = 1;
updateDay();

function updateDay():void{
    if (day==1) date.text = "July 1";
    if (day==2) date.text = "July 2";
    if (day==3) date.text = "July 3";
    // ...and so on...
}

然后在处理程序中调用该函数(和健康更新)sleepClick

function sleepClick(event:MouseEvent):void{
    health = 100;
    day = day + 1;

    //update lifebar
    lifebar.gotoAndStop(health + 1);

    //update day
    updateDay();
}
于 2012-07-16T21:19:19.197 回答