从上面看,我的角色动画片段水平和垂直移动。 在起始位置,字符鼻子朝向屏幕顶部,因此当按下左键时,它应向左旋转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;
}
答案 0 :(得分:1)
按下该键可能有助于设置字符状态变量,然后使用该状态为该字符设置动画。
例如,在按下键时设置旋转和x和y速度,然后使用这些值更新帧事件中的字符x和y以及旋转。
var leftArrow:Boolean;
var rightArrow:Boolean;
var speed:Number = 4;
var vx:Number = 0;
var vy:Number = 0;
var heading:Number = 0;
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) {
vx = -speed;
vy = 0;
heading = 90;
leftArrow = true;
}
else if (event.keyCode == Keyboard.RIGHT) {
vx = speed;
vy = 0;
heading = -90;
rightArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
else if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
}
function everyFrame(event:Event):void {
var mazehit:Boolean = false;
if (leftArrow || rightArrow) {
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 += vx;
char.y += vy;
char.rotation = heading;
}