渡口

Flash AS3

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

PBE中包含了Box2D物理引擎。这一课中我们只使用Box2D的碰撞检测功能。

首先,我们创建一个RemoveOnCollideComponent组件。它的功能是,当收到发生碰撞的通知,拥有它的实体(Entity)就自动销毁(destroy)。

package
{
import com.pblabs.engine.entity.EntityComponent;
import com.pblabs.box2D.CollisionEvent;

public class RemoveOnCollideComponent extends EntityComponent
{

public function RemoveOnCollideComponent() {

}

override
protected function onAdd():void {
owner.eventDispatcher.addEventListener(CollisionEvent.COLLISION_EVENT, onCollision);
}

override
protected function onRemove():void {
owner.eventDispatcher.removeEventListener(CollisionEvent.COLLISION_EVENT, onCollision);
}

private function onCollision(e:CollisionEvent):void {
owner.destroy();
}

}

}
每个组件(Component)都有一个owner属性指向它的持有者(Entity)。每个Entity都有一个eventDispatcher,它的组件可以通过owner.eventDispatcher发送和监听事件。这是实体中的组件之间进行通信的重要方法。我们这个RemoveOnCollideComponent组件,监听了CollisionEvent.COLLISION_EVENT,这个事件是由Box2D发出的。RemoveOnCollideComponent不知道也不关心到底谁发送的事件,它只知道当接收到这个通知时,就让拥有它的实体销毁自己。

posted on 2011-06-26 12:54  BlueJack  阅读(739)  评论(2)    收藏  举报