Flash AS3按住按钮

时间:2012-05-25 19:49:18

标签: actionscript-3 flash button actionscript adobe

我不是Flash方面的专家,但我在 AS2 中找到了一种方法来制作“按住”按钮。现在我正在使用AS3,我希望将此代码转换为 AS3 。有人可以帮忙吗?

stop();

function startTimer(mc, conversionTime) {
    mc.onEnterFrame = function() {
        if ((getTimer() / 1000) - conversionTime > 1) {
            delete this.onEnterFrame;
            gotoAndStop(3);
        }
    };
}
button1.onPress = function() {
    var conversionTime:Number = getTimer() / 1000;
    startTimer(this, conversionTime);
    this.onRelease = function() {
        if (this.onEnterFrame != null) {
            gotoAndStop(2);
        }
        delete this.onEnterFrame;
    };
};

谢谢!

2 个答案:

答案 0 :(得分:3)

在AS3中它看起来像这样:

mc.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
var myTimer:Timer = new Timer(5000,1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, _buttonPressedEnoughLong);

private function _mouseDown(e:MouseEvent):void 
{
    stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
    myTimer.start();
}

private function _mouseUp(e:MouseEvent):void 
{
    stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);
    myTimer.reset();
}

private function _buttonPressedEnoughLong(e:TimerEvent) : void {
    e.currentTarget.reset();
    // Do stuff
}

在事件发生之前,您需要按住按钮5秒钟。

答案 1 :(得分:0)

mc.onEnterFrame = function() ...更改为:

mc.addEventListener(Event.ENTER_FRAME, onEvent);

function onEvent(e:Event)
{
  if ((getTimer() / 1000) - conversionTime > 1)
  {
       this.removeEventListener(Event.ENTER_FRAME, onEvent);
       gotoAndStop(3);
  }
}

button1.onPress = function() ...更改为:

button1.addEventListener(MouseEvent.MOUSE_DOWN, onBtnDown);

function onBtnDown(e:MouseEvent)
{
  var conversionTime:Number = getTimer() / 1000;
  startTimer(this, conversionTime);
  function onBtnUp(e:MouseEvent)
  {
    if (this.hasEventListener(Event.ENTER_FRAME))
    {
        gotoAndStop(2);
        this.removeEventListener(Event.ENTER_FRAME, onEvent);
    }
  }
}