简体   繁体   中英

How to scale an object so that when it equals 0 it will go to the next frame? AS3

Ok, I've got some script for the Alice in Wonderland scene were she is eating cake and drinking potion to be bigger and smaller. I have been able to link two buttons so that when eat_me is clicked she is made smaller and when drink_me is clicked she is made bigger.

What I want to achieve is for Alice to be given a number like 2 and when you click eat_me it goes down by 1 and when you click drink_me it goes up by 1. I then want AS3 to recognise when Alice is at 0 to then move to the next frame. I have some code but not too sure whether I am close or not.

      var alice_size:Number = 2;

      drink_me.addEventListener(MouseEvent.MOUSE_DOWN, resizeAlice);

      function resizeAlice(event:MouseEvent):void {
      sitting_alice.width =  sitting_alice.width * 2;
      sitting_alice.height =  sitting_alice.height * 2;
      {if (drink_me.hitTestObject(sitting_alice))
      alice_size = alice_size +1;}
      }

      eat_me.addEventListener(MouseEvent.MOUSE_UP, resizeAlice2);

      function resizeAlice2(event:MouseEvent):void {
      sitting_alice.width =  sitting_alice.width / 2;
      sitting_alice.height =  sitting_alice.height / 2;
      {if (eat_me.hitTestObject(sitting_alice))
      alice_size = alice_size -1;}
      }

     if (alice_size == 0){
 gotoAndStop (405)
     }

Move alice_size 's declaration so that is a member of the current class. I would advise doing the same for resizeAlice() and resizeAlice2() , though you don't have to do it for them. Also drink_me and eat_me are listening for different types of events; make them both either listen for MouseEvent.MOUSE_UP or MouseEvent.CLICK . I'm not sure why hitTestObject() is being used, but the reason may lie in other code you have.

What you have is pretty close.

  • The event you want is MOUSE_CLICK.
  • Test for the less than zero condition when you are deciding whether or not to advance the frame -- so you don't 'pass' 0, and never get to the frame you want.
  • Try to use more descriptive function names.
  • Get rid of the hit test too, unless there is some other reason you need it there (I left it in).

My version of your code:

var alice_size:Number = 2;

drink_me.addEventListener(MouseEvent.MOUSE_CLICK, growAlice);

function growAlice(event:MouseEvent):void {
  sitting_alice.width *= 2;
  sitting_alice.height *= 2;
  if (drink_me.hitTestObject(sitting_alice)) {
    alice_size = alice_size +1;
  }
}

eat_me.addEventListener(MouseEvent.MOUSE_CLICK, shrinkAlice);

function shrinkAlice(event:MouseEvent):void {
  sitting_alice.width *= 0.5;
  sitting_alice.height *= 0.5;
  if (eat_me.hitTestObject(sitting_alice)){
    alice_size = alice_size -1;
  }
}

if (alice_size <= 0){
  gotoAndStop (405);
}

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