对我来说这似乎很简单,但我的解决方案无效。我做了一个小程序,我最大和最先进的程序,但仍然是一个小程序。它只是一个从地板上反弹并缓慢耗尽能量的球。我已经能够通过按SPACE重新开始弹跳,它可以通过按箭头键改变方向,使用非常简单的速度计算(实际上非常简单)。
这是我的问题,我对编程非常陌生,这只是我的第二天,我需要一种方法将球从舞台的侧壁反弹。
onClipEvent(load){
velocity = 0 //Vertical Velocity, is increased and decreased by the effects of gravity and bouncing with SPACE
sideVel =0 //Side Velocity, increases when the arrow keys are pressed, decreases over time
gravity = 2 //Gravity, constant force that never changes
}
onClipEvent(enterFrame){
_x += sideVel //Moves the ball the value of Side Velocity
if (Key.isDown(Key.LEFT)) {
sideVel -= 2 //Technically decreases Side Velocity, but really increases it in another direction
}
else {
if(sideVel<0) { //If the button isn't being pressed Side Velocity returns to 0 over time
sideVel += 1
}
}
if (Key.isDown(Key.RIGHT)) {
sideVel += 2 //Increases Side Velocity
}
else {
if(sideVel>0) //If the button isn't being pressed Side Velocity returns to 0 over time
sideVel -= 1
}
if (sideVel < -20){ //Side Velocity isn't allowed to go below this number
sideVel +=2 //So we add 2
}
if (sideVel > 20) { //Same as above
sideVel -=2
}
velocity += gravity //Regular Velocity increases by 2 every frame
_y += velocity //mcMain moves at the speed of its velocity
if(_y>=Stage.height){
if(Key.isDown(Key.SPACE)){
velocity=-28 //Sets Velocity to -28, pretty much the same as doing a jump
}
else {
_y = Stage.height
velocity *= -0.9 //Reverses mcMain's velocity, so it bounces back into the air at a slightly slower speed
}
/*Here's the problem
if(_x>=Stage.width - 25){
_x=Stage.width - 25
sideVel *= -1
}
if(_x<=Stage.width - 550){
_x=Stage.width - 550
sideVel *= -1
}
}
}
答案 0 :(得分:0)
当你碰到墙壁时,你必须手动改变MC的位置(MC.x++
和诸如此类的东西),这样碰撞就不会多次登记。此外,使用while
语句代替if
进行墙碰撞将确保球不会穿过墙壁。