hitTestPoint() 未正确测试碰撞

发布于 2025-01-07 20:59:05 字数 1648 浏览 0 评论 0原文

我正在编写一个游戏,它有敌人和子弹。当子弹击中敌人时,我想消灭敌人和子弹。我使用 hitTestPoint() 方法来测试子弹是否击中敌人。这是我的游戏循环中的代码:

for each(var bullet:Bullet in this.bullets) {
    for each(var enemy:Enemy in this.enemies) {
        if(enemy.hitTestPoint(bullet.x, bullet.y)) {
            trace("hit");
        }
    }
    bullet.update();
}

this.bulletsthis.enemies 都是包含子弹和敌人对象的数组。这是这两个类:

package com {
    import flash.display.MovieClip;
    import flash.display.Stage;

    public class Bullet extends MovieClip {

        private var stageRef:Stage;
        public var speed:Number = 10;

        public function Bullet(stage:Stage) {
            this.stageRef = stage;
        }

        public function update() {
            this.x += Math.sin((Math.PI / 180) * (360 - this.rotation)) * this.speed;
            this.y += Math.cos((Math.PI / 180) * (360 - this.rotation)) * this.speed;
        }
    }
}

--

package com {
    import flash.display.MovieClip;
    import flash.display.Stage;

    public class Enemy extends MovieClip {

    public var speed:Number = 4;
    private var stageRef:Stage;

    public function Enemy(stage:Stage) {
        this.stageRef = stage;
        this.x = this.stageRef.stageWidth / 3;
        this.y = this.stageRef.stageHeight / 2;
    }

    public function update() {

    }
}

}

问题是,只有当bullet 和敌人的x 和y 值相同时,hitTestPoint 才返回true,而不是在两个影片剪辑重叠时返回true。这会导致子弹直接穿过敌人,但不会被记录为击中。也许我缺少一个边界框?

有没有办法让 hitTestPoint 在子弹击中敌人时返回 true,而不是仅在子弹和敌人坐标相同时返回 true?

谢谢你!

I'm writing a game and it has enemies and bullets. When a bullet hits an enemy, I want to destroy the enemy and the bullet. I'm using the hitTestPoint() method to test if a bullet has hit an enemy. Here's the code in my game loop:

for each(var bullet:Bullet in this.bullets) {
    for each(var enemy:Enemy in this.enemies) {
        if(enemy.hitTestPoint(bullet.x, bullet.y)) {
            trace("hit");
        }
    }
    bullet.update();
}

this.bullets and this.enemies are both arrays containing objects for bullets and enemies. Here's those two classes:

package com {
    import flash.display.MovieClip;
    import flash.display.Stage;

    public class Bullet extends MovieClip {

        private var stageRef:Stage;
        public var speed:Number = 10;

        public function Bullet(stage:Stage) {
            this.stageRef = stage;
        }

        public function update() {
            this.x += Math.sin((Math.PI / 180) * (360 - this.rotation)) * this.speed;
            this.y += Math.cos((Math.PI / 180) * (360 - this.rotation)) * this.speed;
        }
    }
}

--

package com {
    import flash.display.MovieClip;
    import flash.display.Stage;

    public class Enemy extends MovieClip {

    public var speed:Number = 4;
    private var stageRef:Stage;

    public function Enemy(stage:Stage) {
        this.stageRef = stage;
        this.x = this.stageRef.stageWidth / 3;
        this.y = this.stageRef.stageHeight / 2;
    }

    public function update() {

    }
}

}

The problem is, hitTestPoint only returns true if both the x and y values of bullet and enemy are the same, rather than if the two movie clips overlap. This leads to bullets going right through enemies but it not registering as a hit. Perhaps I'm missing a bounding box?

Is there a way I can make hitTestPoint return true if the bullet hits the enemy at all rather than only if the bullet and enemy co-ordinates are the same?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

玩世 2025-01-14 20:59:05

是的,如果您的影片剪辑太小,则帧 x 将正好位于对象的右侧,并且下一帧将通过它,您可能需要使“hitbox”更大,就像使用 alpha 矩形使影片剪辑更大一样。

Yes, if yours movieclips are too small the frame x will be just right to the object and the next frame it will be pass it, you may want to make a "hitbox" bigger, like doing the movieclip bigger with alpha rectangles.

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