嗯,首先,我是一名學生,所以我真的很懇求你耐心等待答案。順便說一句,我也不是英語的人。 一切都開始了,因爲我會參加一個Game Jam,並且我已經開始編寫(或試圖編寫)一個簡單的「物理」代碼,我真正認爲的代碼很容易。 我正在使用類並使用hitTestObject進行碰撞檢測。 其實我從開始就有問題,但我一直在努力修復它。在這個時候代碼真的很糟糕,我已經用另一種方式來思考。但是有些東西令我困惑。 這是我已經寫過的代碼的一部分(它可能看起來很新鮮)。大部分代碼都是廢話,只要看看最後的「else if」。 注意:這是「block」類,而「obj」是玩家的影片剪輯。AS3中一個簡單物理系統的問題
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軸。我知道這個代碼很糟糕,但是如果有人能幫助我理解這個錯誤,也許可以引導我更高效的解決方案,那麼我將非常感激!謝謝。
謝謝你的回答!它現在有效!但是現在他們又遇到了另一個問題:當我在左側跳躍並與其碰撞並仍然按下「D」按鈕時,對象牌手就掉下來了,我想要他們做什麼。但是當我在右側碰撞並仍然按下「A」按鈕時,palyer停留在空中,直到釋放按鈕......我只在播放器類上有這個:「if(block.leftCol){player.x - = speed} else if(block.rightCol){player.x + = speed)「 –
您應該支持更多關於按鍵功能的代碼 – kare