【发布时间】:2016-03-11 20:06:03
【问题描述】:
我有一个从 cocos2d 框架扩展 CCSprite 的背景。我已经将这个精灵添加到了游戏层。现在在这个背景类中,我尝试添加其他名为 Star 的 CCSprites,如下所示:
//create the stars
stars = [[CCArray alloc] init];
for (int i = 0; i < 10; i++)
{
Star* star = [[Star alloc ] initWithFile:@"star-hd.png"];
CGSize screensize = [[CCDirector sharedDirector] winSize];
//CCLOG(@"screensize: %f, %f", screensize.width, screensize.height);
CGPoint newstarlocation;
newstarlocation.x = CCRANDOM_0_1() * screensize.width;
newstarlocation.y = CCRANDOM_0_1() * screensize.height;
star.position = newstarlocation;
[self addChild:star z:i];
[stars addObject:star];
}
但星星不会显示。我尝试了几件事,唯一似乎有效的是当我在游戏层而不是背景上添加星星时。但这不是我想要的。
cocos2d 中不允许嵌套精灵吗?如果允许,我如何嵌套精灵?
【问题讨论】:
-
代码似乎正确,尝试使用不同的图像 (star.png) 和固定位置(星星的位置是 相对 (即偏移)到父位置(背景精灵)。最后,将星星添加到自己的数组有什么意义呢?毕竟,您可以只使用添加星星的子数组。如果需要,给它们所有相同的标签以区分其他节点。