AS3 运行 在按钮(或任何东西)事件之后持续一段时间的事件
AS3 running an event for a set duration after button (or anything) event
为了问题的目的,假设我在舞台上有一个物体。当我点击另一个按钮时,我希望颜色改变 1 秒,然后在完成后恢复原状。
下面是我的演示代码:
Button.addEventListener(MouseEvent.CLICK, Colour_Change);
function Colour_Change(evt: MouseEvent): void {
var my_color: ColorTransform = new ColorTransform();
my_color.color = 0xFF0000;
Coloured_Object.transform.colorTransform = my_color;
}
我想要的是在上述功能中加入某种定时器功能。我不知道该怎么做,因此没有实施。
你应该使用 flash.utils.Timer
class.
Adobe ActionScript 3.0 Reference for the Timer class
以下应该足以让您朝着正确的方向前进:
import flash.utils.Timer;
var myTimer:Timer = new Timer(1000, 1); // 1 second
var running:Boolean = false;
Button.addEventListener(MouseEvent.CLICK, Colour_Change);
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
function Colour_Change(evt: MouseEvent): void {
var my_color: ColorTransform = new ColorTransform();
my_color.color = 0xFF0000;
Coloured_Object.transform.colorTransform = my_color;
if(!running) {
myTimer.start();
running = true;
}
}
function runOnce(event:TimerEvent):void {
// code to revert the button's color back goes here
myTimer.reset();
running = false;
}
如果您需要更多帮助或者此示例是否有错误,请通过此答案的评论部分告诉我。
进一步解释上面使用的定时器class:
创建新计时器时
myTimer=new Timer(1000,1)
括号中的第一个数字是您希望计时器 运行 的毫秒数(例如 1000 = 1 秒)
第二个数字是您希望计时器重复多少次(或 0 表示无限重复)。
每次计时器到达您输入的时间 (1000),这将触发该事件的任何事件侦听器 Timer_Event.TIMER,例如,如果您想让它打开和关闭颜色,您可以在计时器上重复多次并更改功能。
计时器可以做的其他有用的事情:
您可以为
添加一个事件侦听器
Timer_Event.TIMER_COMPLETE
(所有重复完成后熄灭)
myTimer.currentCount
将 return 计时器到目前为止已完成的重复次数。
如其他答案所述,您可以使用 Timer
对象或 setTimeout()
函数来做到这一点,如下所示:
// the current color of our target object
var default_color:ColorTransform;
// the delay in milliseconds
var delay:int = 1000;
btn.addEventListener(MouseEvent.CLICK, Colour_Change);
function Colour_Change(evt: MouseEvent): void {
// save the current color of our target object
default_color = target.transform.colorTransform;
var new_color:ColorTransform = new ColorTransform();
new_color.color = 0xFF0000;
target.transform.colorTransform = new_color;
var timer:Timer = new Timer(delay, 1);
timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
// after the delay, we use the default color of our target
target.transform.colorTransform = default_color;
})
timer.start();
// using setTimeout(), you have to disable this if using the Timer
var timeout:int = setTimeout(
function(){
clearTimeout(timeout);
// after the delay, we use the default color of our target
target.transform.colorTransform = default_color;
},
delay
);
}
希望能帮到你。
为了问题的目的,假设我在舞台上有一个物体。当我点击另一个按钮时,我希望颜色改变 1 秒,然后在完成后恢复原状。
下面是我的演示代码:
Button.addEventListener(MouseEvent.CLICK, Colour_Change);
function Colour_Change(evt: MouseEvent): void {
var my_color: ColorTransform = new ColorTransform();
my_color.color = 0xFF0000;
Coloured_Object.transform.colorTransform = my_color;
}
我想要的是在上述功能中加入某种定时器功能。我不知道该怎么做,因此没有实施。
你应该使用 flash.utils.Timer
class.
Adobe ActionScript 3.0 Reference for the Timer class
以下应该足以让您朝着正确的方向前进:
import flash.utils.Timer;
var myTimer:Timer = new Timer(1000, 1); // 1 second
var running:Boolean = false;
Button.addEventListener(MouseEvent.CLICK, Colour_Change);
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
function Colour_Change(evt: MouseEvent): void {
var my_color: ColorTransform = new ColorTransform();
my_color.color = 0xFF0000;
Coloured_Object.transform.colorTransform = my_color;
if(!running) {
myTimer.start();
running = true;
}
}
function runOnce(event:TimerEvent):void {
// code to revert the button's color back goes here
myTimer.reset();
running = false;
}
如果您需要更多帮助或者此示例是否有错误,请通过此答案的评论部分告诉我。
进一步解释上面使用的定时器class:
创建新计时器时
myTimer=new Timer(1000,1)
括号中的第一个数字是您希望计时器 运行 的毫秒数(例如 1000 = 1 秒) 第二个数字是您希望计时器重复多少次(或 0 表示无限重复)。
每次计时器到达您输入的时间 (1000),这将触发该事件的任何事件侦听器 Timer_Event.TIMER,例如,如果您想让它打开和关闭颜色,您可以在计时器上重复多次并更改功能。
计时器可以做的其他有用的事情:
您可以为
添加一个事件侦听器Timer_Event.TIMER_COMPLETE
(所有重复完成后熄灭)
myTimer.currentCount
将 return 计时器到目前为止已完成的重复次数。
如其他答案所述,您可以使用 Timer
对象或 setTimeout()
函数来做到这一点,如下所示:
// the current color of our target object
var default_color:ColorTransform;
// the delay in milliseconds
var delay:int = 1000;
btn.addEventListener(MouseEvent.CLICK, Colour_Change);
function Colour_Change(evt: MouseEvent): void {
// save the current color of our target object
default_color = target.transform.colorTransform;
var new_color:ColorTransform = new ColorTransform();
new_color.color = 0xFF0000;
target.transform.colorTransform = new_color;
var timer:Timer = new Timer(delay, 1);
timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
// after the delay, we use the default color of our target
target.transform.colorTransform = default_color;
})
timer.start();
// using setTimeout(), you have to disable this if using the Timer
var timeout:int = setTimeout(
function(){
clearTimeout(timeout);
// after the delay, we use the default color of our target
target.transform.colorTransform = default_color;
},
delay
);
}
希望能帮到你。