簡體   English   中英

側面碰撞閃光動作腳本3?

[英]side collision flash actionscript-3?

沒有人有任何示例/知識的任何教程,可以讓我在Flash ActionScript 3中與對象進行側面碰撞嗎? 我一直在搜索,還沒有找到任何東西..謝謝您的幫助。

我相信您正在尋找的是可用於所有顯示對象的“ hitTest”功能。 hitTestObject告訴您兩個對象是否已碰撞,hitTestPoint告訴您對象是否與一個點碰撞。 假設有一個名為“ myDisplayObj”的MovieClip變量:

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

嘗試這個..

實現是checkForCollision()將返回一個String來表示遇到碰撞的那一側。 您要檢查沖突的對象需要擴展此類。 另外,如果要使用傾斜的圖塊,請將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 "";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM