添加和访问 CCSprite
我在插入同一精灵的多个子级并访问它(或在运行时为它们设置位置)时遇到问题。请建议任何合适的方法最好指出我的错误。这是我的方法。
//In the Init Method...
//int i is defined in the start.
for (i = 1; i < 4; i++)
{
hurdle = [CCSprite spriteWithFile:@"hurdle1.png"];
[self addChild:hurdle z:i tag:i];
hurdle.position = CGPointMake(150 * i, 0);
}
它将所有精灵散布在画布上。然后在一些“更新函数”中我称之为。
hurdle.position = CGPointMake(hurdle.position.x - 5, 10);
if (hurdle.position.x <= -5) {
hurdle.position = ccp(480, 10);
}
它可以工作,但正如预期的那样,只有一个实例水平移动。我希望所有实例都被移动,所以我尝试使用它......
for (i = 1; i < 4; i++){
[hurdle getChildByTag:i].position = CGPointMake(hurdle.position.x - 5, 10);
//OR
[hurdle getChildByTag:i].position = CGPointMake([hurdle getChildByTag:i].position.x - 5, 10);
}
我尝试在各个地方获取日志,并意识到 getChildByTag 无法按照我尝试使用它的方式工作。
I'm having trouble inserting multiple children of same sprite and accessing it (or setting positions for them on runtime). Kindly advise any suitable method preferably point out my mistake. Here is my approach.
//In the Init Method...
//int i is defined in the start.
for (i = 1; i < 4; i++)
{
hurdle = [CCSprite spriteWithFile:@"hurdle1.png"];
[self addChild:hurdle z:i tag:i];
hurdle.position = CGPointMake(150 * i, 0);
}
It spreads all the sprites on the canvas. then in some "UPDATE Function" I'm calling this.
hurdle.position = CGPointMake(hurdle.position.x - 5, 10);
if (hurdle.position.x <= -5) {
hurdle.position = ccp(480, 10);
}
It works but as expected only one instance moves horizontally. I want all the instances to be moved so I am trying to use this....
for (i = 1; i < 4; i++){
[hurdle getChildByTag:i].position = CGPointMake(hurdle.position.x - 5, 10);
//OR
[hurdle getChildByTag:i].position = CGPointMake([hurdle getChildByTag:i].position.x - 5, 10);
}
I've tried getting LOGs on various places and realized that getChildByTag doesn't work the way I'm trying to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在最后一段代码中。您应该在 for 循环中对每个 CCSprite 进行本地引用。
由于您将精灵添加到了
self
中,因此您将把它们作为self
的子元素进行检索,如果您在同一场景中以这种方式创建任何其他精灵,请务必小心。给任意两个精灵赋予相同的标签是糟糕的设计。
编辑有关避免重复标签的信息。
如果您知道您将拥有多少个精灵。使用标签枚举并按名称引用精灵。
如果没有,知道有多少组并限制组的大小可以使其易于管理。
IE
假设您有 3 部分代码,用于生成这样的精灵。您可以在 .m 中(@implementation 行下)包含一个
enum
并将限制放在那里然后当您创建每个组
时 然后按组进行检索
希望这能激发您的创造力。现在玩得开心吧。
The problem is in the last block of code. You should make a local reference to each CCSprite within your for loop.
Since you added the sprites to
self
, you will retrieve them as children ofself
Be careful if you create any other sprites this way in the same scene. It is bad design to give any two sprites the same tag.
EDIT about avoiding duplicate tags.
If you know how many sprites you will have. Use an enum of tags and refer to the sprites by name.
If not, knowing how many groups and putting a limit on the size of groups could make it managable.
ie
say you have 3 parts of code where you are generating sprites like this. You can include an
enum
in your .m (under @implementation line) and put the limits thereThen when you create each group
Then to retrieve in groups
Hopefully that sparks your creativity. Now have some fun.
我经常做的事情是将类似类型的对象分组,通过将它们添加到 CCNode 并将该 CCNode 添加到层,以类似的方式对其进行操作。
我将创建一个派生自 CCNode 的类,
然后我可以将所有逻辑放入该节点中,然后通过 [selfchildren] 进行访问
Something that I do often is group objects of like kind that I want to act on in a similar way by adding them to a CCNode and add that CCNode to the layer.
I would create a class that derives from CCNode
Then I can put all my logic in that node and access then via [self children]