以你为例,我假定你不打算让怪物和人类相互作出反应。
无论哪种方式,添加一个自定义联系侦听器到世界和联系侦听器检查如果一个球员和敌人的形状创建一个联系点。如果是这样,向玩家施加线性冲击()以达到您想要的效果,并禁用用户的所有键输入以防止任何运动变化。然后,只要玩家拥有一个属性,防止它在被怪物击中的时候施加冲动。
此外,当您创建的机构,你需要设置玩家和敌人的实例作为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