以你爲例,我假定你不打算讓怪物和人類相互作出反應。
無論哪種方式,添加一個自定義聯繫偵聽器到世界和聯繫偵聽器檢查如果一個球員和敵人的形狀創建一個聯繫點。如果是這樣,向玩家施加線性衝擊()以達到您想要的效果,並禁用用戶的所有鍵輸入以防止任何運動變化。然後,只要玩家擁有一個屬性,防止它在被怪物擊中的時候施加衝動。
此外,當您創建的機構,你需要設置玩家和敵人的實例作爲body.UserData()
public class Player extends MovieClip
{
public const MAX_EFFECT_TIME = 1.5 * framerate;
public var effectTime:int = 0;
public var body:b2Body;
public function step():void
{
if (effectTime > 0)
{
effectTime--;
//do awesome animation
}
else
{
//move normally
}
}
public function Hit(enemy:Enemy)
{
if (effectTime == 0)
{
//apply linear impulse to object
if (enemy.body.GetPosition().x < this.body.GetPosition().x)
{
//apply impulse left in left direction
b2Vec2 force = b2Vec2(-8, 10);
body.ApplyLinearImpulse(force, body.GetWorldCenter());
}
else
{
//apply impulse in right direction
b2Vec2 force = b2Vec2(8, 10);
body.ApplyLinearImpulse(force, body.GetWorldCenter());
}
//reset effect time
effectTime = MAX_EFFECT_TIME;
}
}
}
public class Game extends MovieClip
{
public var world:b2World;
public var player:Player;
public Game()
{
world = initWorld();
player = initPlayer();
var cl = new CustomContactListener();
world.SetContactListener(cl);
this.addEventListener(Event.ENTER_FRAME, step);
}
private void step(e:Event)
{
world.step();
player.step();
}
}
public class CustomContactListener extends b2ContactListener
{
//Called when a contact point is added.
public override function Add(point:b2ContactPoint):void
{
//checks if the first shape is a player and second is an enemy if true call Hit
if (point.shape1.GetBody().GetUserData().isPlayer && point.shape2.GetBody().GetUserData().isEnemy)
{
point.shape1.GetBody().GetUserData().Hit(point.shape2.GetBody().GetUserData());
}
else if (point.shape2.GetBody().GetUserData().isPlayer && point.shape1.GetBody().GetUserData().isEnemy)
{
point.shape2.GetBody().GetUserData().Hit(point.shape1.GetBody().GetUserData());
}
}
}
然後您可以根據需要編輯值。希望這會有所幫助,祝你好運!
我不認爲我會永遠得到這個答案接受:'( – Shannon 2011-06-21 08:18:21
大聲笑對不起,最後!!!謝謝! – Eli 2011-07-04 12:34:34
嗯,但這將適用於怪物時,當玩家擊中怪物impluse。怪物就像是背景,除非它不受它的影響,它只受重力影響,我該怎麼做?動態類型都是這樣的! – Eli 2011-07-07 12:25:31