按住按键时的 Javascript easelJS 动画
我正在尝试使用 easelJS 制作 HTML5 RPG 游戏来处理精灵动画。当我向任意方向按箭头键时,角色不会播放动画,而是似乎跳回到第 0 帧并停止,而不是循环播放动画。
我需要的是在按下按键时继续循环的动画,而不是角色卡在框架中。
这是我到目前为止的游戏,它真的很混乱,因为我正在试验并且仍在学习 HTML5。 http://cloudrealms.com/dev/
这是我正在使用的代码:
箭头键捕获:
$(window).keydown(
function(e){
keys[e.which] = true;
if(isMovementKey(e.which))
{
moveOrder.unshift(e.which);
jQuery.unique(moveOrder);
animationsToPlay.push(1);
}
e.preventDefault();
return false;
}
);
$(window).keyup(
function(e){
keys[e.which] = false;
if(isMovementKey(e.which))
{
moveOrder.splice(jQuery.inArray(e.which, moveOrder),1);
}
}
);
在我的勾选 我有一个名为 HandleInput() 的函数,这里是代码:
function HandleInput()
{
switch (moveOrder[0]) {
case 38: /* Up arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_up");
playerSequence[playerID].y -= playerSequence[playerID].vY;
break;
case 40: /* Down arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_down");
playerSequence[playerID].y += playerSequence[playerID].vY;
break;
case 37: /* Left arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_left");
playerSequence[playerID].x -= playerSequence[playerID].vX;
break;
case 39: /* Right arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_right");
playerSequence[playerID].x += playerSequence[playerID].vX;
break;
}
}
我想知道的是如何在按住箭头键的同时为玩家精灵设置动画?
I'm trying to make an HTML5 RPG game using easelJS to handle sprite animations. When I press the arrow keys in any direction the character does not play the animation through, instead it seems to jump back to frame 0 and stop instead of looping the animation.
What I need is the animation to continue looping while the key is being pressed down, instead the character get's stuck in a frame.
Here is the game I have so far, it's really a mess because I'm experimenting and still learning HTML5.
http://cloudrealms.com/dev/
Here is the code that I am using:
Arrow Key capture:
$(window).keydown(
function(e){
keys[e.which] = true;
if(isMovementKey(e.which))
{
moveOrder.unshift(e.which);
jQuery.unique(moveOrder);
animationsToPlay.push(1);
}
e.preventDefault();
return false;
}
);
$(window).keyup(
function(e){
keys[e.which] = false;
if(isMovementKey(e.which))
{
moveOrder.splice(jQuery.inArray(e.which, moveOrder),1);
}
}
);
In my tick I have a function called HandleInput() here is the code:
function HandleInput()
{
switch (moveOrder[0]) {
case 38: /* Up arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_up");
playerSequence[playerID].y -= playerSequence[playerID].vY;
break;
case 40: /* Down arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_down");
playerSequence[playerID].y += playerSequence[playerID].vY;
break;
case 37: /* Left arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_left");
playerSequence[playerID].x -= playerSequence[playerID].vX;
break;
case 39: /* Right arrow was pressed */
playerSequence[playerID].gotoAndPlay("walk_right");
playerSequence[playerID].x += playerSequence[playerID].vX;
break;
}
}
What I want to know is how can I animate the player sprite while the arrow key is being held down?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点令人困惑,很难说出你的代码在做什么,你能提供一个链接吗?
关于让你的球员在钥匙按下时移动的问题,我可以提供一些建议。 onkeydown 事件的工作方式是,只要按下键,就会触发该事件。
如果我是你,我会有一个布尔值来表示按键是否被按下。在 keydown 事件上,将其设置为 true,在 keyup 事件上将其设置为 false。:
请注意,这适用于任何键,您必须在 keydown 函数中指定所需的键,但您可以在按键功能。您可以在您的股票行情中检查它是否属实。如果这是真的,您可以在 spritesheet 上运行 gotoandplay 功能:
这并不特定于任何一个键。所以你可能想将它从布尔值更改为字符串。可能有字符串“up”、“down”、“left”、“right”等,并且 null 表示没有按下任何键。希望这有帮助。
——阿什利
It is a little confusing, and its hard to tell what your code is doing, could you provide a link?
In terms of getting your player to move while the key is down I can offer a few pieces of advice. The way the onkeydown events work is that as long as the key is down, the event will be fired.
If I were you, I would have a boolean that represents whether or not they key has been pressed. On the keydown event, you set it to true, on the keyup event you set it to false.:
Note this works for any key, you will have to specify which you want in the keydown function, but you can leave it generic in the keyup function. In your ticker you check whether it is true. In the event that it's true, you run the gotoandplay functionality on your spritesheet:
This is not specific to any one key. So you might want to change it from a bool to a string. Maybe have the strings "up", "down", "left", "right", etc. , and null for no key pressed. Hope this helps.
--Ashley