在动作脚本 3 中使用命中测试弹跳球
我有这段代码可以使球弹起,但我正在寻找的是从地面发射子弹,一旦它们击中球,它们应该将其向上弹回。 目标是不让球落地。 我确信这以前已经做过,但我想我太笨了,无法弄清楚。
代码:
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);
}
}
}
I have this code which makes the ball bounce, but what I am looking for is to shoot bullets from the ground and once they hit the ball they should bounce it back upwards. The goal is not to let the ball hit the ground. I am sure this has been done before, but I guess am too dumb to figure it out.
The Code:
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的方法是使用 DisplayObject 的 hitTestObject。 这会检查重叠情况。 你当然可以自己写一些更高级的东西。
对于每个游戏更新,您都会检查是否有子弹击中球,如果击中则采取行动。 您还应该考虑将游戏逻辑和显示更新分开。 考虑使用计时器。
A simple way is to use DisplayObject's hitTestObject. This checks for overlapping. You can of course write something more advanced yourself.
For each game update you check if any bullets hit the ball, and take action if they did. You should also consider separating game logic and display updating. Look into using Timer.
有一个用于执行 hittest 的函数,您可以在这里阅读:
http://livedocs.adobe.com/ flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#hitTestObject()
虽然对于圆来说,如果您知道半径,但如果中心之间的距离小于半径之和,则很容易; 他们互相触摸。
但是通过查看你的代码,你应该正确地重写整个事情。 球确实不应该在内部处理碰撞和运动逻辑。 它应该只有位置和速度向量,运动和碰撞逻辑应该在其他类中。
至于反应的代码,就看你想要的复杂程度了。 可以是简单地翻转速度矢量的 y 部分,也可以是相当复杂的数学。
这方面有很多教程和例子,我认为你最好在谷歌上找到一些并使用它。 然后自己写。
There's a function for preforming hittest you can read about it here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#hitTestObject()
Though with circles it's pretty easy if you know the radius, if the distance between the centers is less than the sum of their radii; they touch each other.
But by looking at your code you should properly rewrite the whole thing. The ball really should not handle collision and movement logic internally. It should only have a positon and speed vector, and the movement and colission logic should be in other classes.
As for the code for reaction, it depends on how complicated you want it. And can be anthing from simply flipping the y part of the speed vector, to fairly complicated math.
There's alot of toturial and examples in this area, I think you would be better of finding some on google and playing with it. Then write your own.