Ios 如何创建一个精灵的多个实例
我正在双击制作一个Ios 如何创建一个精灵的多个实例,ios,objective-c,cocos2d-iphone,ccsprite,Ios,Objective C,Cocos2d Iphone,Ccsprite,我正在双击制作一个CCSprite(它应该代表一颗炸弹),就像这样 - (void)handleDoubleTap:(UITapGestureRecognizer *)doubletapRecognizer { [self placeBomb]; } -(void)placeBomb { NSLog(@"Bomb placed"); _circle = [[CCSprite alloc]initWithFile:@"Circle.png"]; CGPoint c
CCSprite
(它应该代表一颗炸弹),就像这样
- (void)handleDoubleTap:(UITapGestureRecognizer *)doubletapRecognizer
{
[self placeBomb];
}
-(void)placeBomb
{
NSLog(@"Bomb placed");
_circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
CGPoint circle0position = ccp(_cat.position.x , _cat.position.y);
CGPoint c0TileCoordt = [self tileCoordForPosition:circle0position];
CGPoint c0TileCoord = [self positionForTileCoord:c0TileCoordt];//Supereffective way to get rid of decimals
_circle.position = c0TileCoord;
[self addChild:_circle];
id fade = [CCScaleTo actionWithDuration:3.5 scale:0];
[_circle runAction:fade];
[self performSelector:@selector(explosion) withObject:nil afterDelay:3];
}
然后我运行-(void)explosion
,它-惊奇-使它爆炸,然后再次调用checkForDamage方法,该方法在爆炸寿命内测试爆炸区域内的碰撞
问题是我不想限制炸弹(精灵)的数量,但当我现在放置两枚炸弹时,第一枚炸弹的爆炸发生在第二枚炸弹的现场,这显然不是我的错,因为我要求它这么做。爆炸基于\u圆。位置
爆炸块大约有三百行长,因此我不会打扰您在这里发布它,但作为一个例子,我将发布checkForDamage
-(void)checkForDamage //Gets called with CCTime on explosion
{
bool playerHit = NO;
CGPoint bombPos = [self tileCoordForPosition:_circle.position];
for (int i = 0; i <= explosionLenght; i++)
{
CGPoint playerPosF = [self tileCoordForPosition:_cat.position];
int bombX = (bombPos.x + i);
int bombY = (bombPos.y + i);
int bombNegX = (bombPos.x - i);
int bombNegY = (bombPos.y - i);
CGPoint centre = bombPos;
CGPoint top = ccp(centre.x, bombY);
CGPoint left = ccp(bombNegX, centre.y);
CGPoint bottom = ccp(centre.x, bombNegY);
CGPoint right = ccp(bombX, centre.y);
if (CGPointEqualToPoint(top, playerPosF) ||
CGPointEqualToPoint(left, playerPosF) ||
CGPointEqualToPoint(bottom, playerPosF) ||
CGPointEqualToPoint(right, playerPosF))
{
playerHit = YES;
NSLog(@"Player hit, BOOL:%i",playerHit);
[self unschedule:@selector(checkForDamage)];
break;
}
}
[self performSelector:@selector(stopCheckDamage) withObject:nil afterDelay:2];
}
但由于我最多只能放置20-30枚炸弹,这是非常无效的,我认为一定有更好的方法。有什么方法可以有效地做到这一点 你能不能在
爆炸
和检查损坏
中都添加一个圆圈
参数?你要寻找的是对象池:预创建精灵,使它们不可见,在需要时使第一个不可见,并设置其位置等。完成后,使其再次不可见,这也使其再次可重复使用。您还希望首先使用sprite批处理,因为您非常关心效率。@您可以详细说明哪些问题吗?没有CCSPrite
等方面的经验。我可能没有抓住要点。我只是想创建一个新的圆圈
,然后[自执行选择器:@selector(explosion)with object:circle afterDelay:3]代码>。然后在-(void)explosion:(圆圈*)圆圈
你做的[…checkForDamage:Circle]
@意思很重要我要试试。
if (explosion1exists) {explosion with circle1}, if (expl1 + expl2 exists) {expl with circle3}