这对我来说似乎很简单,但我的解决方案不起作用。我做了一个小程序,我最大和最先进的程序,但仍然是一个小程序。它只是一个从地板弹起并慢慢耗尽能量的球。我让它能够通过按 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
}
}
}