因此,我有一个名为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();
}发布于 2012-07-16 21:19:19
您不会用新数据更新实际的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-16 20:41:00
health和day在舞台上显示了吗?他们是哪种类型的?您是否将TextField的文本设置为它们的值?
如果它们只是变量,并且没有由UI显示,您将不会看到它们发生变化。
https://stackoverflow.com/questions/11512060
复制相似问题