Cocos2d iphone 从父CCSprite中删除随机对象
也许这是一个简单的问题,因为我是新手 我看到一个服务员端着一盘食物的场景Cocos2d iphone 从父CCSprite中删除随机对象,cocos2d-iphone,Cocos2d Iphone,也许这是一个简单的问题,因为我是新手 我看到一个服务员端着一盘食物的场景 (食物是从数组中随机选择的食物) 每次他出现在屏幕上,都会拿着一块新的食物 (用户触摸食物,服务员走开返回 又是一块新的食物 然而,我似乎无法从屏幕上删除食物的旧标签 正如上面所说,孩子已经被添加了。。。 任何帮助都会很好 -(id) init { ///other code then... waiterOnSCreen = [CCSprite spriteWithSpriteFrameNam
(食物是从数组中随机选择的食物) 每次他出现在屏幕上,都会拿着一块新的食物
(用户触摸食物,服务员走开返回
又是一块新的食物 然而,我似乎无法从屏幕上删除食物的旧标签 正如上面所说,孩子已经被添加了。。。 任何帮助都会很好
-(id) init
{
///other code then...
waiterOnSCreen
= [CCSprite spriteWithSpriteFrameName:@"CatUP.png"];
waiterOnSCreen.position = ccp(CatOffSCreenPosX, catXpos);
[self addChild:waiterOnSCreen z:0];
//moving the waiter
// the random food sprite is added later to the waiter
// [waiterOnSCreen addChild:myRandomSprite];
}
-(void)LoadRandomFood
{
///I make my array here then and add CCSprites
RandomFood = [[NSMutableArray alloc]initWithObjects:
//cake,
RandomMuffin,
RandomMeat,
RandomCake,//
nil];//
int i = 0;
int count= [RandomFood count];
if (i < count)
{
int i = arc4random() % [RandomFood count];
myRandomSprite = (CCSprite *)[RandomFood objectAtIndex:i];
//waiterOnSCreen is a CCSprite added on the init
[waiterOnSCreen addChild:myRandomSprite];
myRandomSprite.position=ccp(290,220);
myRandomSprite.tag = RandomFoodTag;
}
}
我猜它也会崩溃“孩子已经添加了,不能再添加了”
因为它试图再次添加松饼、肉、蛋糕
我不太确定如何修复它。我不得不制作另一个数组来访问精灵并删除它
NSMutableArray *_buffer = [NSMutableArray new];
[_buffer insertObject:myRandomSprite atIndex:0];
CCSprite *sprite = (CCSprite *)[_buffer objectAtIndex:0];
[sprite removeFromParentAndCleanup:YES];
你不需要创建另一个数组来删除一个精灵。如果你能提供一个更完整的项目图片,我很乐意查看。你能把整个代码文件发布到你工作的地方吗?还是多个文件?关于检查你的一些样本,请进一步关注
LoadRandomFood
中的一些问题查找NSMutableArray
,而不检查RandomFood
是否已经有值
这样做会更安全,不过请注意,我只是在这里做一个健全性检查。在init或其他不经常执行的地方只填写一次RandomFood
会更有效
-(void)LoadRandomFood
{
if (RandomFood)
{
[RandomFood release];
}
///I make my array here then and add CCSprites
RandomFood = [[NSMutableArray alloc]initWithObjects:
//cake,
RandomMuffin,
RandomMeat,
RandomCake,//
nil];//
int i = 0;
int count= [RandomFood count];
if (i < count)
{
int i = arc4random() % [RandomFood count];
myRandomSprite = (CCSprite *)[RandomFood objectAtIndex:i];
//waiterOnSCreen is a CCSprite added on the init
[waiterOnSCreen addChild:myRandomSprite];
myRandomSprite.position=ccp(290,220);
myRandomSprite.tag = RandomFoodTag;
}
}
-(无效)装载食物
{
如果(食物)
{
[食物释放];
}
///然后我在这里创建数组并添加ccsprite
RandomFood=[[NSMutableArray alloc]initWithObjects:
//蛋糕,
松饼,
肉,
蛋糕//
零]//
int i=0;
整数计数=[随机食物计数];
如果(i
在添加精灵时发生崩溃的情况下,您确定执行了
removeChildByTag
调用吗?是否有可能在删除旧精灵之前将标记重新分配给新精灵?您是否已在调试器中完成了逻辑操作?再次在此处查看更完整的代码文件将有所帮助。谢谢Rob,是的,我喜欢e这个想法(我尝试了一下),但是我正在运行ARC,所以我不能显式地释放对象…也许有另一种类似于ARC的方法?啊,好吧,对于我的大多数东西,由于各种原因,我通常不使用ARC。你能分享一些完整的代码文件来检查吗?
-(void)LoadRandomFood
{
if (RandomFood)
{
[RandomFood release];
}
///I make my array here then and add CCSprites
RandomFood = [[NSMutableArray alloc]initWithObjects:
//cake,
RandomMuffin,
RandomMeat,
RandomCake,//
nil];//
int i = 0;
int count= [RandomFood count];
if (i < count)
{
int i = arc4random() % [RandomFood count];
myRandomSprite = (CCSprite *)[RandomFood objectAtIndex:i];
//waiterOnSCreen is a CCSprite added on the init
[waiterOnSCreen addChild:myRandomSprite];
myRandomSprite.position=ccp(290,220);
myRandomSprite.tag = RandomFoodTag;
}
}