Actionscript-3:movieclip中的按钮

时间:2012-07-16 20:32:16

标签: actionscript-3 function variables button

所以,我有一个名为bg的movieclip,里面有一个名为sleep_btn的按钮。我正在舞台上的一个层上进行编码,它是这样的:

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

我很快意识到如果没有在编码中定义movieclip就行不通,所以我试过了:

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();
}

2 个答案:

答案 0 :(得分:0)

舞台上是否显示healthday?他们是什么类型的?您是否将TextField的文本设置为其值?

如果它们只是变量并且UI不显示它们,您将看不到它们的变化。

答案 1 :(得分: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();
}