AS3 如何停止循环旋转?
从上面看,我的角色动画剪辑水平和垂直移动。 在起始位置,角色的鼻子面向屏幕顶部,因此当按下左键时,它应该向左旋转 90 度并面向左 + 继续朝左移动。
因为我使用了移动函数 + 旋转,所以现在它会进行 90 度旋转并向左旋转。 我理解为什么会发生这种情况,但我对你的问题是;
如何让它只旋转一次角度,并继续朝着那个方向运动?
var leftArrow:Boolean;
var speed:Number = 4;
var charRadius:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
}
function everyFrame(event:Event):void {
var mazehit:Boolean = false;
if (leftArrow) {
for(var i:int = 0; i < speed; i++) {
if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x -= speed;
char.rotation -= 90;
}
Seen from above, my character movieclip moves horizontally and vertically.
In starting position the characters nose is facing the top of the screen so when left key is pressed it should rotate 90 degrees to the left and face left + continue the movement facing left.
Because I use a movement function + the rotation it now kind off loops the 90 degree rotation and swirls around in circles to the left.
I understand why this is happening, but my question to you is;
How can I make it rotate the degree angle only once, and continue the movement facing that direction?
var leftArrow:Boolean;
var speed:Number = 4;
var charRadius:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
}
function everyFrame(event:Event):void {
var mazehit:Boolean = false;
if (leftArrow) {
for(var i:int = 0; i < speed; i++) {
if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x -= speed;
char.rotation -= 90;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当按下按键时设置角色状态变量,然后使用该状态来为角色设置动画可能会有所帮助。
例如,设置按下按键时的旋转以及 x 和 y 速度,然后使用这些值更新帧事件上的字符 x 和 y 以及旋转。
It may help to set the character state variables when the key is pressed and then use that state to animate the character.
For example, set the rotation and x and y velocity when the key is pressed, then use those values to update the characters x and y and rotation on the frame event.