2010-12-14 56 views
0

我需要幫助摧毀相鄰精靈周圍和周圍的精靈,例如在半徑2.5釐米內應該銷燬所有精靈。這裏的想法是我會從底部向頂部落下的物體射擊。一旦發生碰撞,圍繞該半徑的所有精靈也應該被銷燬。像炸彈一樣。我已經使用box2d作爲碰撞,即聯繫偵聽器。如何去做呢?摧毀精靈和其周圍的精靈

請推薦:-)

問候,

KARTHIK

回答

1

握住你的精靈的數組,或者如果您使用的是batchNode你能做到這一點。

當發生碰撞時,通過你的精靈。檢查他們的位置和爆炸中心的距離,並殺死他們,如果他們在範圍內。

例如

CCSprite *sprite; 
for (sprite in [batchNode descendants]) { 

    if ([sprite isInRangeOf:[explosionSprite position]]) { 
     [sprite yourRemovalMethod]; 
    } 

} 

方法 'isInRangeOf:' 是你的精靈子

喜歡的東西..內

-(BOOL) isInRangeOf:(CGPoint)explosionCenter { 

//Use pythagoras theorem to work out the distance between [sprite position] and [explosionCenter] 

    CGFloat dx = explosionCenter.x - [self position].x; 
    CGFloat dy = explosionCenter.y - [self position].y; 
    float distance = sqrt(dx*dx + dy*dy); 

// If your distance is less than or equal to your 'death radius' return YES, else No. 
    if (distance <= 25) { 
    return TRUE; 
    } else { 
    return FALSE; 
    } 


} 

希望有所幫助。

+0

我的答案是否解決了您的問題? – Bongeh 2010-12-22 17:02:27