侧面碰撞 Flash ActionScript-3?

发布于 2024-10-30 22:52:32 字数 83 浏览 1 评论 0原文

有谁有任何示例/知道任何教程可以让我在 Flash ActionScript 3 中与对象进行侧面碰撞吗?我一直在寻找但找不到任何东西..感谢您的帮助。

does anyone have any examples/know of any tutorials which would let me do side collision in flash actionscript 3 with objects? I have been searching and havent been able to find anything .. thanks for any help.

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

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

发布评论

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

评论(2

濫情▎り 2024-11-06 22:52:32

我相信您正在寻找的是可用于所有显示对象的“hitTest”函数。 hitTestObject 告诉您两个对象是否发生碰撞,hitTestPoint 告诉您一个对象是否与某个点发生碰撞。假设有一个名为“myDisplayObj”的 MovieClip 变量:

if( myDisplayObj.hitTestObject(otherDisplayObj) ){ //do object collision code }
if( myDisplayObj.hitTestPoint(100,350) ){ //do point collision code }

I believe what you're looking for are the "hitTest" functions that are available to all Display Objects. hitTestObject tells you whether two objects have collided, and hitTestPoint tells you whether an object has collided with a point. Given, say, a MovieClip var named "myDisplayObj":

if( myDisplayObj.hitTestObject(otherDisplayObj) ){ //do object collision code }
if( myDisplayObj.hitTestPoint(100,350) ){ //do point collision code }
红颜悴 2024-11-06 22:52:32

试试这个..

实现是 checkForCollision() 将返回一个字符串来表示您碰撞的一侧。您想要检查碰撞的对象需要扩展此类。另外,如果您想使用有角度的图块,请将 angl 属性设置为 tl、bl、tr、tl 来表示倾斜面的位置。希望这不会太令人困惑。

public class Impassable extends MovieClip
{
    // Vars
    public var angl:String = "";

    /**
     * Checks if the specified point collides with this
     * @param cx The x value of the point being checked
     * @param cy The y value of the point being checked
     * @param offset An offset from the edges of this that can be considered as part of the radius of this
     * @return A String representing the side that a collision was detected on
     */
    public function checkForCollision(cx:int, cy:int, offset:int=0):String
    {
        if(angl.length < 1)
        {
            // Horizontal
            if(cy > y - 1 && cy < y + height + 1)
            {
                if(cx > x - offset && cx < x + width/2) return 'WEST';
                if(cx < x + width + offset && cx > x + width/2) return 'EAST';
            }

            // Vertical
            if(cx > x - 1 && cx < x + height + 1)
            {
                if(cy > y - offset && cy < y + height/2) return 'NORTH';
                if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
            }
        }
        else
        {
            // Gradient (1)
            var xgr:Number = cx - x;
            var ygr:Number = cy - y;
            var ua:Boolean = false;

            // Angled Tiles
            if(angl == "tl")
            {
                // Top Left
                if(cx > x - 1 && cy > y - 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cy < y + height - xgr + offset || cx < x + width - ygr + offset) return 'SOUTH_EAST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx > x - offset && cx < x + width/2) return 'WEST';
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy > y - offset && cy < y + height/2) return 'NORTH';
                    }
                }
            }
            if(angl == "tr")
            {
                // Top Right
                if(cx < x + width + 1 && cy > y - 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cy < y + height - (width - xgr) + offset || cx > x + ygr - offset) return 'SOUTH_WEST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx < x + width + offset && cx > x + width/2) return 'EAST';
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy > y - offset && cy < y + height/2) return 'NORTH';
                    }
                }
            }
            if(angl == "br")
            {
                // Bottom Right
                if(cx < x + width + 1 && cy < y + height + 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cx > x + (height - ygr) - offset || cy > y + height - xgr - offset) return 'NORTH_WEST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx < x + width + offset && cx > x + width/2) return 'EAST'
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
                    }
                }
            }
            if(angl == "bl")
            {
                // Bottom Left
                if(cx > x - 1 && cy < y + height + 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cx < x + ygr + offset || cy > y + xgr - offset) return 'NORTH_EAST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx > x - offset && cx < x + width/2) return 'WEST';
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
                    }
                }
            }
        }

        return "";
    }
}

Try this..

Implementation is that checkForCollision() will return a String to represent the side you've collided against. The object you want to check for a collision against needs to extend this class. Also, if you want to use angled tiles, set the angl property to be either tl, bl, tr, tl to represent the position of the sloped side. Hope this isn't too confusing.

public class Impassable extends MovieClip
{
    // Vars
    public var angl:String = "";

    /**
     * Checks if the specified point collides with this
     * @param cx The x value of the point being checked
     * @param cy The y value of the point being checked
     * @param offset An offset from the edges of this that can be considered as part of the radius of this
     * @return A String representing the side that a collision was detected on
     */
    public function checkForCollision(cx:int, cy:int, offset:int=0):String
    {
        if(angl.length < 1)
        {
            // Horizontal
            if(cy > y - 1 && cy < y + height + 1)
            {
                if(cx > x - offset && cx < x + width/2) return 'WEST';
                if(cx < x + width + offset && cx > x + width/2) return 'EAST';
            }

            // Vertical
            if(cx > x - 1 && cx < x + height + 1)
            {
                if(cy > y - offset && cy < y + height/2) return 'NORTH';
                if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
            }
        }
        else
        {
            // Gradient (1)
            var xgr:Number = cx - x;
            var ygr:Number = cy - y;
            var ua:Boolean = false;

            // Angled Tiles
            if(angl == "tl")
            {
                // Top Left
                if(cx > x - 1 && cy > y - 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cy < y + height - xgr + offset || cx < x + width - ygr + offset) return 'SOUTH_EAST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx > x - offset && cx < x + width/2) return 'WEST';
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy > y - offset && cy < y + height/2) return 'NORTH';
                    }
                }
            }
            if(angl == "tr")
            {
                // Top Right
                if(cx < x + width + 1 && cy > y - 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cy < y + height - (width - xgr) + offset || cx > x + ygr - offset) return 'SOUTH_WEST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx < x + width + offset && cx > x + width/2) return 'EAST';
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy > y - offset && cy < y + height/2) return 'NORTH';
                    }
                }
            }
            if(angl == "br")
            {
                // Bottom Right
                if(cx < x + width + 1 && cy < y + height + 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cx > x + (height - ygr) - offset || cy > y + height - xgr - offset) return 'NORTH_WEST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx < x + width + offset && cx > x + width/2) return 'EAST'
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
                    }
                }
            }
            if(angl == "bl")
            {
                // Bottom Left
                if(cx > x - 1 && cy < y + height + 1) ua = true;
                if(ua)
                {
                    // Angle Collision
                    if(cx < x + ygr + offset || cy > y + xgr - offset) return 'NORTH_EAST';
                }
                else
                {
                    // Straight Collision
                    if(cy > y - 1 && cy < y + height + 1)
                    {
                        if(cx > x - offset && cx < x + width/2) return 'WEST';
                    }
                    if(cx > x - 1 && cx < x + height + 1)
                    {
                        if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
                    }
                }
            }
        }

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