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();
}
}
}

浙公网安备 33010602011771号