【问题标题】:How to scale an object so that when it equals 0 it will go to the next frame? AS3如何缩放一个对象,以便当它等于 0 时它会转到下一帧? AS3
【发布时间】:2014-02-26 15:39:00
【问题描述】:

好的,我有一些爱丽丝梦游仙境场景的脚本,她正在吃蛋糕和喝药水,让她变大变小。我已经能够链接两个按钮,这样当点击eat_me 时,她会变小,而当点击drink_me 时,她会变大。

我想要实现的是给 Alice 一个像 2 这样的数字,当你点击 eat_me 时它会下降 1,当你点击 drink_me 时它会上升 1。然后我希望 AS3 识别 Alice 何时处于 0然后移至下一帧。我有一些代码,但不太确定我是否接近。

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

【问题讨论】:

  • ifs 前面为什么要加花括号?

标签: actionscript-3 flash function scaling next


【解决方案1】:

移动alice_size 的声明,使其成为当前类的成员。我建议对resizeAlice()resizeAlice2() 做同样的事情,尽管您不必为他们这样做。 drink_meeat_me 也在监听不同类型的事件;让他们都听MouseEvent.MOUSE_UPMouseEvent.CLICK。我不确定为什么要使用hitTestObject(),但原因可能在于您拥有的其他代码。

【讨论】:

    【解决方案2】:

    你所拥有的非常接近。

    • 您想要的事件是 MOUSE_CLICK。
    • 在决定是否推进帧时测试小于零的条件 - 这样您就不会“通过”0,也永远不会到达您想要的帧。
    • 尝试使用更具描述性的函数名称。
    • 也去掉命中测试,除非有其他原因需要它(我把它留在里面了)。

    我的代码版本:

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

    【讨论】:

      猜你喜欢
      • 2014-04-15
      • 2017-11-22
      • 2013-01-03
      • 2016-01-16
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多