簡體   English   中英

AS3中的簡單物理系統存在的問題

[英]Problems with a simple physics system in AS3

好吧,首先,我是一名學生,所以我真的懇求您耐心等待答案。 順便說一句,我也不講英語。 一切都開始了,因為我將參加Game Jam,而且我已經開始編寫(或嘗試編寫)簡單的“物理”代碼,我本以為那很容易。 我正在使用類,並使用hitTestObject進行大腸桿菌檢測。 實際上,從一開始我就遇到了問題,但是我一直在努力解決它。 目前,代碼確實很糟糕,我已經在考慮另一種方式來做到這一點。 但是,有些事情使我感到困惑。 這是我已經編寫的代碼的一部分(可能看起來很新手)。 代碼的大部分是胡說八道,只看最后一個“ else if”。 注意:這是“ block”類,“ obj”是播放器的影片剪輯。

public function checkObj(obj:MovieClip):void
{   
    if (this.hitTestObject (obj))
    {
        if (this.y < obj.y)
        {
            if (this.x - this.width/2 <= obj.x+obj.width/2 && this.x + this.width/2 >= obj.x-obj.width/2)
            {
                downCol = true;
            }
        }

        else if (this.y + this.height/2 > obj.y-obj.height/2)
        {
            if (this.x - this.width/2+2 <= obj.x+obj.width/2 && this.x + this.width/2-2 >= obj.x-obj.width/2)
            {
                upCol = true;
                onBox = true;
            }
        }
        if (this.x - this.width/2 >= obj.x+obj.width/2-2)
        {
            if (this.y - this.height/2<= obj.y+obj.height/2&& this.y + this.height/2>= obj.y-obj.height/2)
            {
                leftCol = true;
            }
        }
        else if (this.x + this.width/2 <= obj.x-obj.width/2-2)
        {
        if (this.y + 5>= obj.y-obj.height/2 && this.y - 5<= obj.y+obj.height/2)
            {
                trace ("Collided on right");
                rightCol = true;
            }
        }
    }
}

由於某種原因,這種方式不起作用。 它從不向我返回該消息。

但是,如果我做一點改變:

public function checkObj(obj:MovieClip):void
{   
    if (this.x + this.width/2 <= obj.x-obj.width/2-2)
    {
        objOnRight = true;
    }
    if (this.hitTestObject (obj))
    {
    //...

        else if (objOnRight == true)
        {
            if (this.y + 5>= obj.y-obj.height/2 && this.y - 5<= obj.y+obj.height/2)
            {
                rightCol = true;
                trace ("Collided on right");
            }
        }
    }
}

有用。 僅僅是因為我在測試碰撞之前檢查了“ obj”的X軸。 我知道這段代碼根本不好,但是如果有人可以幫助我理解該錯誤,甚至可以指導我找到更有效的解決方案,我將不勝感激! 謝謝。

最后嘗試一下

else if (this.x + this.width / 2 <= obj.x + obj.width / 2 - 2) {
        if (this.y + 5 >= obj.y - obj.height / 2 && this.y - 5 <= obj.y + obj.height / 2) {
            trace("Collided on right");
            rightCol = true;
        }
    }  

只需將“-”更改為“ +”即可。

好,算了 我寫了一個全新的代碼,沒有hitTest,它的工作就像一個魅力。 我認為Math.abs用負數解決了這個問題。 感謝您的幫助,kare81。 這是代碼:

public function checkObj(obj:MovieClip,place:int):void
        {   
            if (obj.x + obj.width / 2 > this.x - width / 2 && obj.x < this.x - width / 2 + 7 && Math.abs (obj.y - y) < height / 2)
            {
                obj.x = this.x - width / 2 - obj.width / 2;
            }
            if (obj.x - obj.width / 2 < this.x +width / 2 && obj.x > this.x +  width / 2 - 7 && Math.abs (obj.y - y) < height / 2)
            {
                obj.x = this.x +width / 2 + obj.width / 2;
            }
            if (Math.abs (obj.x- this.x) < width / 2 + obj.width / 2 && obj.y<y-height/2 && obj.onFloor > y-height/2 && obj.onBlock != place)
            {
                obj.onFloor = y - height / 2;
                obj.onBlock = place;
            }
            if (Math.abs (obj.x - this.x) >= width / 2 + obj.width / 2 && obj.onBlock == place)
            {
                obj.onFloor = 450;
            }
            if (obj.y - obj.height / 2 < y + height /2 && obj.y > y && Math.abs (obj.x - this.x) < width / 2 + obj.width / 2)
            {
                obj.y = y + height / 2 + obj.height / 2;
            }
        }

暫無
暫無

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

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