gpt4 book ai didi

actionscript-3 - As3 如何翻转影片剪辑以面对移动方向?

转载 作者:行者123 更新时间:2023-12-04 06:12:05 25 4
gpt4 key购买 nike

正在制作迷宫游戏。当按下左键时,动画片段(字符)应该向左转 90 度。

如果我错了,请纠正我,但我认为我可以使用此代码;

char.scaleX *= -1;

然而,最重要的是角色没有穿过迷宫的墙壁。
我认为这就是我实现上述代码的问题。
因为当我放在这里时它不能正常工作;
    if(!mazehit) {

char.y += speed;
char.scaleX *= -1;


}

我要问你的问题是,我必须把代码放在哪里才能翻转影片剪辑?
var leftArrow, rightArrow, upArrow, downArrow: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;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}

function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = 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;




}

} else if (rightArrow) {
for(var j:int = 0; j < speed; j++) {
if(bounds.hitTestPoint(char.x + charRadius + j, char.y, true)) {

mazehit = true;
break;
}
}
if(!mazehit) {

char.x += speed;
}
} else if (upArrow) {
for(var k:int = 0; k < speed; k++) {
if(bounds.hitTestPoint(char.x, char.y - charRadius - k, true)) {

mazehit = true;
break;
}
}
if(!mazehit) {
char.y -= speed;
}

} else if (downArrow) {

for(var m:int = 0; m < speed; m++) {
if(bounds.hitTestPoint(char.x, char.y + charRadius + m, true)) {

mazehit = true;
break;
}
}
if(!mazehit) {

char.y += speed;


}
}
}

感谢您的时间

最佳答案

我会根据速度更新方向:

char.scaleX = (speed > 0) ? 1 : -1;

或者,通过按下的键:
if(keyLeft && !keyRight)
{
char.scaleX = -1;
}
else if(keyRight && !keyLeft)
{
char.scaleX = 1;
}
else
{
// keep current direction
}

关于actionscript-3 - As3 如何翻转影片剪辑以面对移动方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7659945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com