简体   繁体   中英

Actionscript-3: Button within movieclip

So, I have a movieclip called bg with a button named sleep_btn within it. 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:

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? What type are they? Are you setting the text of a TextField to their values?

If they are just variables and they are not displayed by a UI you won't see them change.

You aren't updating the actual UI textboxes with the new data. 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:

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

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

    //update day
    updateDay();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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