AS3 按住鼠标可提高速度/功率
这就是我想要实现的目标;
单击影片剪辑 (cannon_mc) 正在射击 (ball_mc)
鼠标按下的时间越长,发射球的速度就会增加。 我向你提出的问题是;
实现这一目标最有效的方法是什么? 用计时器或类似的东西;
var isMouseDown:Boolean = false;
var speed= 10;
myCannon.addEventListener(MouseEvent.MOUSE_DOWN,buttonPressed);
function buttonPressed(event:MouseEvent){
//trace("down");
isMouseDown == true;
if(isMouseDown == false)
{
speed == +1
}
}
this is what i'm trying to accomplish;
With a click on a movieclip (cannon_mc)
a shot is being fired (ball_mc)
The longer mouse is down, the speed of wich the ball is fired with should increase.
My question to you is;
What is the most efficient way to accomplish this?
With a timer or something like this;
var isMouseDown:Boolean = false;
var speed= 10;
myCannon.addEventListener(MouseEvent.MOUSE_DOWN,buttonPressed);
function buttonPressed(event:MouseEvent){
//trace("down");
isMouseDown == true;
if(isMouseDown == false)
{
speed == +1
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MOUSE_DOWN 事件仅触发一次。要获得您想要的效果,您需要 MOUSE_DOWN 和 MOUSE_UP 事件处理程序的组合。
您可以在 MOUSE_DOWN 事件中将变量设置为 true 以及来自
flash.utils.getTimer()
的当前时间戳然后在 MOUSE_UP 上,如果您在 MOUSE_DOWN 中设置的变量为 true,则计算经过的时间并相应地设置功率。
示例:
您还可以添加 ENTER_FRAME 事件并为电量计或其他视觉效果添加动画
更新
正如 The_asMan 所指出的,如果在舞台外拖动并释放鼠标,
MOUSE_UP
事件将不会触发。要处理这种情况,请为MOUSE_LEAVE
事件添加事件侦听器,并将回调作为 buttonReleased 函数的副本,但它采用 Event 对象:The MOUSE_DOWN event is only fired once. To get the effect you want you need the combo of MOUSE_DOWN and MOUSE_UP Event Handlers.
You can set a variable to true in the MOUSE_DOWN event alongwith the current timestamp from
flash.utils.getTimer()
Then on MOUSE_UP, if the variable you set in MOUSE_DOWN is true, you compute the time elapsed and set the power accordingly.
Example:
You can also add an ENTER_FRAME event and animate a power gauge or something for visual effect
Update
As pointed to by The_asMan,
MOUSE_UP
event will not fire if the mouse is dragged and released Outside the stage. To handle this case add and event listener forMOUSE_LEAVE
event with the callback as the copy of buttonReleased function but which takes an Event object:(非常短的伪代码)
编写一些事件处理程序:
_mouseDown
,将power
设置为零if (_mouseDown) power++;
_mouseDown
并以累积的能量
发射球帧速率独立版本:
_loadStart = getTimer(); _mouseDown = true; _power = 0;
if (_mouseDown) delta = getTimer() - _loadStart; _power += delta;
_power
射球,_mouseDown = false;
(in very short pseudocode)
Write some event handlers:
_mouseDown
, setpower
to zeroif (_mouseDown) power++;
_mouseDown
and fires ball with accumulatedpower
Framerate independent version:
_loadStart = getTimer(); _mouseDown = true; _power = 0;
if (_mouseDown) delta = getTimer() - _loadStart; _power += delta;
_power
,_mouseDown = false;