在动作脚本 3 中使用命中测试弹跳球

发布于 2024-07-18 06:18:39 字数 2005 浏览 9 评论 0原文

我有这段代码可以使球弹起,但我正在寻找的是从地面发射子弹,一旦它们击中球,它们应该将其向上弹回。 目标是不让球落地。 我确信这以前已经做过,但我想我太笨了,无法弄清楚。

代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-07-25 06:18:39

一个简单的方法是使用 DisplayObject 的 hitTestObject。 这会检查重叠情况。 你当然可以自己写一些更高级的东西。

对于每个游戏更新,您都会检查是否有子弹击中球,如果击中则采取行动。 您还应该考虑将游戏逻辑和显示更新分开。 考虑使用计时器。

package {
    public class BallGame extends Sprite
    {
        private var ball:Ball;
        private var bullets:Array;

        public function BallGame() {
            addEventListener(Event.ENTER_FRAME, checkCollision);
            addEventListener(MouseEvent.CLICK, fireBullet);
        }

        private function fireBullet(e:MouseEvent):void {
            // adds a fired bullet to an array so we can loop over all bullets
            bullets.push(new Bullet());
        }

        private function checkCollision(e:Event):void {
            // loops through all bullets in play
            for each(var bullet:Bullet in bullets) {
                // check if the bullet is inside the ball
                if (ball.hitTestObject(bullet)) {
                    // the bullet hit the ball
                    ball.startRisingBall();

                    // TODO: remove the bullet :)
                }
            }
        }
    }
}

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.

package {
    public class BallGame extends Sprite
    {
        private var ball:Ball;
        private var bullets:Array;

        public function BallGame() {
            addEventListener(Event.ENTER_FRAME, checkCollision);
            addEventListener(MouseEvent.CLICK, fireBullet);
        }

        private function fireBullet(e:MouseEvent):void {
            // adds a fired bullet to an array so we can loop over all bullets
            bullets.push(new Bullet());
        }

        private function checkCollision(e:Event):void {
            // loops through all bullets in play
            for each(var bullet:Bullet in bullets) {
                // check if the bullet is inside the ball
                if (ball.hitTestObject(bullet)) {
                    // the bullet hit the ball
                    ball.startRisingBall();

                    // TODO: remove the bullet :)
                }
            }
        }
    }
}
浅听莫相离 2024-07-25 06:18:39

有一个用于执行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文