gpt4 book ai didi

actionscript-3 - 符号偶尔会卡在边缘

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

我有一些代码,我希望一个符号(只是一个方形 atm)在舞台内弹跳。然而,有时它会卡在边缘。有关如何解决此问题的任何建议?

其他一切正常,包括惯性运动。

package  {
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard

public class Unit extends MovieClip {

var velocityX:Number = 1;
var velocityY:Number = 1;
var accellerationX:Number = 1;
var accellerationY:Number = 1;
var bounciness:Number = 1;
var maxSpeed:Number = 10;

public function Unit(){
Key.initialize(stage);
addEventListener("enterFrame", move);

}

private function move(e:Event){

accellerate()
if ((this.x<10)||(this.x>780)){
velocityX *= -1;
if (velocityX>0){
velocityX -= bounciness;
}
if (velocityX<0){
velocityX += bounciness;
}
this.x += velocityX;
}
else{
this.x += velocityX;
}
if ((this.y<10)||(this.y>520)){
velocityY *= -1;
if (velocityY>0){
velocityY -= bounciness;
}
if (velocityY<0){
velocityY += bounciness;
}
this.y += velocityY;
}
else {
this.y += velocityY;
}
}

private function accellerate(){

if (Math.abs(velocityY) < maxSpeed){
if (Key.isDown(Keyboard.UP)){
velocityY -= accellerationY;
trace("Accellerating UP");
}
if (Key.isDown(Keyboard.DOWN)){
velocityY += accellerationY;
trace("Accellerating DOWN");
}
}
if (Math.abs(velocityX) < maxSpeed){
if (Key.isDown(Keyboard.RIGHT)){
velocityX += accellerationX;
trace("Accellerating RIGHT");
}
if (Key.isDown(Keyboard.LEFT)){
velocityX -= accellerationX;
trace("Accellerating LEFT");
}
}
if (Key.isDown(Keyboard.SPACE)){
velocityX = 0;
velocityY = 0;
}

}
}

}

最佳答案

您的问题如下:

if ((this.x<10)||(this.x>780)){
velocityX *= -1;

你切换了 volocityX,但可能会在下一帧发生,它仍然满足 if 条件(没有向另一个方向移动足够远)所以它只是在每一帧从 -1 到 1 来回移动.

改为做这样的事情:

if(this.x < 10 && velocity == -1) velocity = 1;  //If it's near the left edge, and still moving to the left, change direction
if(this.x > 780 && velocity == 1) velocity = -1;

这样,您只会在方向错误时改变方向。

重复 velocityY 的东西

关于actionscript-3 - 符号偶尔会卡在边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29570372/

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