如果你想要檢查Hero與Enemy之間是否有碰撞,只要這樣做就可以了。
var simpleCitrusSolver:SimpleCitrusSolver = new SimpleCitrusSolver ("citrus solver"); simpleCitrusSolver.overlap(Hero,Enemy); add(simpleCitrusSolver);但是如果要檢查的物件是使用了PoolObject,就會無法檢查到碰撞。
進去看了一下程式做法才知道他是用_ce.state.getObjectsByType(Class)的方法抓出場景上的各個物件來作比對,不過如果你的物件放在PoolObject裡面,getObjectsByType就會什麼都抓不到
解決方法就是傳入的PoolObject物件改用poolObject.foreachRecycled()來取得pool裏面的各個物件就可以了。
以下直接寫個繼承SimpleCitrusSolver的Class並加上這個功能
package { import citrus.core.CitrusObject; import citrus.datastructures.PoolObject; import citrus.objects.CitrusSprite; import citrus.physics.simple.SimpleCitrusSolver; /** * 使用PoolObject作碰撞檢查 * Use PoolObject to collision checking * @author cainmaila@gmail.com */ public class PoolCitrusSolver extends SimpleCitrusSolver { private var _poolChecks:Array = []; /** * Simple collision checking * @param name * @param params */ public function PoolCitrusSolver(name:String, params:Object = null) { super(name, params); } /** * 使用PoolObject作檢查 * @param typeA PoolObject * @param typeB CitrusObject Class */ public function poolChk(typeA:PoolObject, typeB:Class):void { _poolChecks.push( { a:typeA, b:typeB } ); } override public function update(timeDelta:Number):void { super.update(timeDelta); var pair:Object; var groupB:Vector.你可以直接下載我寫好的物件; var itemB:CitrusSprite; var j:int; //增加一個遍歷PoolObject各個物件的邏輯 for each (pair in _poolChecks) { PoolObject(pair.a).foreachRecycled(function (itemA:CitrusSprite):void { groupB = _ce.state.getObjectsByType(pair.b); for (j = 0; j < groupB.length; ++j) { itemB = groupB[j] as CitrusSprite; overlapOnce(itemA, itemB); } }); } } } }
新增了一個poolChk方法,使用方法只要把PoolObject物件傳入就好了
var poolCitrusSolver:PoolCitrusSolver = new PoolCitrusSolver("citrus solver"); poolCitrusSolver.poolChk(poolObject,Class); add(simpleCitrusSolver);
沒有留言:
張貼留言