简体   繁体   English

动作3:movieclip中的按钮

[英]Actionscript-3: Button within movieclip

So, I have a movieclip called bg with a button named sleep_btn within it. 因此,我有一个名为bg的动画片段,其中有一个名为sleep_btn的按钮。 I am doing the coding on a layer on the stage, and it went like this: 我正在舞台上的一层上进行编码,结果如下:

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

I quickly realized that it would not work without the movieclip being defined as well in the coding, so I tried: 我很快意识到,如果在编码中未同时定义动画片段,它将无法正常工作,因此我尝试:

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

My error was gone, but I found that when I clicked on the button, the health and day stayed the same. 我的错误消失了,但是我发现当我点击按钮时,健康状况和日子保持不变。

The day is in a text field, with coding like this: 一天在一个文本字段中,编码如下:

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";

and on and on 并不断

And the health is a 101 frame movieclip with coding like: 健康状况是一个101帧的动画片段,其编码如下:

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

EDIT 编辑

Top Banner Layer: 顶部横幅层:

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

Is health and day displayed on the stage? 舞台上是否显示了healthday What type are they? 他们是什么类型的? Are you setting the text of a TextField to their values? 您是否将TextField的文本设置为其值?

If they are just variables and they are not displayed by a UI you won't see them change. 如果它们只是变量而没有在UI中显示,则不会看到它们发生变化。

You aren't updating the actual UI textboxes with the new data. 您不会使用新数据更新实际的UI文本框。 Move your day determining code (which could be massively cleaned up, but that is out of the scope of this question) into its own function so it can be reused: 将您每天确定的代码(可以进行大量清理,但这不在此问题的范围内)移至其自己的函数中,以便可以重复使用:

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...
}

Then call that function (and the health update) in the sleepClick handler: 然后在sleepClick处理程序中调用该函数(以及运行状况更新):

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

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

    //update day
    updateDay();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM