所以,我有一个名为 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();
}