Cocos2d iphone 为什么在将精灵添加到父级时只触发Cocos2D更新';子节点的CCSpriteBatchNode
我不明白为什么会发生下面的事情,我希望这里有人能解释一下 我有一个GameLayer(CCLayer)类和一个Food(CCNode)类 在Gamelayer类中,我创建了一组具有精灵属性的食物对象。 我想将这些精灵添加到CCSpriteBatchNodeCocos2d iphone 为什么在将精灵添加到父级时只触发Cocos2D更新';子节点的CCSpriteBatchNode,cocos2d-iphone,ccspritebatchnode,Cocos2d Iphone,Ccspritebatchnode,我不明白为什么会发生下面的事情,我希望这里有人能解释一下 我有一个GameLayer(CCLayer)类和一个Food(CCNode)类 在Gamelayer类中,我创建了一组具有精灵属性的食物对象。 我想将这些精灵添加到CCSpriteBatchNode spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"bol.png"]; [self addChild:spriteBatch]; for (int i = 0; i < 1000
spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"bol.png"];
[self addChild:spriteBatch];
for (int i = 0; i < 1000; i++) {
Food * tempfood = [Food foodWithParentNode:self];
[spriteBatch addChild:[tempfood mySprite]];
[self addChild:tempfood];
}
但是,如果我将添加精灵到批次的代码从游戏类移动到食品类,那么更改位置的更新确实起作用,并且食品在屏幕上移动。
但是为什么呢
因此,这给出:
-(id)initWithParentNode:(CCNode *)parentNode{
if((self = [super init]))
{
mySprite = [CCSprite spriteWithFile:@"bol.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
[[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[[ (GameLayer*) parentNode spriteBatch] addChild:mySprite];
[self scheduleUpdate];
}
return self;
}
我真的看不出打电话有什么区别
[[ (GameLayer*) parentNode spriteBatch] addChild:mySprite];
来自食品类或:
[spriteBatch addChild:[tempfood mySprite]];
从'parent'游戏层Ruben,mySprite是一个带有retain属性的属性? 食物类可能正在丢失此属性的内存引用 在init上,尝试使用self.mySprite设置mySprite,以保留该值 在.m或.h上,放置:
@property (nonatomic, retain) CCSprite *mySprite
在init上,使用:
self.mySprite = [CCSprite spriteWithFile:@"bol.png"];
确保mySprite属性指向mySprite实例?也许出于某种原因,在你想从游戏层添加它的时候它是零。它肯定不是零,因为它显示在屏幕上,但计划的更新没有启动,所以它没有移动……你不能说它“肯定不是零”,因为这只是一个属性。您可以创建sprite(Food类中的mySprite变量),但声明属性有点错误,因此foodInstance.mySprite可以为nil。
self.mySprite = [CCSprite spriteWithFile:@"bol.png"];