我有这段代码可以让球弹跳,但我正在寻找的是从地面发射子弹,一旦它们击中球,它们应该向上弹回。目标是不让球落地。我敢肯定这以前已经做过,但我想我太笨了,无法弄清楚。
编码:
package {
public class ball extends MovieClip {
var timer:Number=0;
var initialPos:Number=0;
var finalPos:Number=0;
var currentPos:Number=0;
var initialSpeed:Number=0;
function ball() {
startFallingBall();
}
function moveBallDown(e:Event) {
timer+=1;
this.y = initialPos + .5 *(timer * timer);
checkBottomBoundary();
}
function moveBallUp(e:Event) {
timer+=1;
var posA=this.y;
this.y = currentPos - initialSpeed*timer + .5*(timer * timer);
var posB=this.y;
checkTopBoundary(posA, posB);
}
function checkBottomBoundary() {
if (this.y+this.height>stage.stageHeight) {
finalPos=this.y;
stopFallingBall();
}
}
function checkTopBoundary(firstPos:Number, secondPos:Number) {
if (secondPos>firstPos) {
stopRisingBall();
startFallingBall();
}
}
function startFallingBall() {
timer=0;
initialPos=this.y;
this.addEventListener(Event.ENTER_FRAME, moveBallDown);
}
function stopFallingBall() {
this.removeEventListener(Event.ENTER_FRAME, moveBallDown);
if (finalPos-initialPos<.1*this.height) {
stopRisingBall();
} else {
startRisingBall();
}
}
function startRisingBall() {
initialSpeed=Math.sqrt(Math.abs(finalPos-initialPos));
timer=0;
currentPos=this.y;
this.addEventListener(Event.ENTER_FRAME, moveBallUp);
}
function stopRisingBall() {
this.removeEventListener(Event.ENTER_FRAME, moveBallUp);
}
function stopEverything() {
this.removeEventListener(Event.ENTER_FRAME, moveBallUp);
this.removeEventListener(Event.ENTER_FRAME, moveBallDown);
}
}
}