添加和访问 CCSprite

发布于 2024-12-27 17:10:48 字数 909 浏览 0 评论 0原文

我在插入同一精灵的多个子级并访问它(或在运行时为它们设置位置)时遇到问题。请建议任何合适的方法最好指出我的错误。这是我的方法。

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2025-01-03 17:10:48

问题出在最后一段代码中。您应该在 for 循环中对每个 CCSprite 进行本地引用。

由于您将精灵添加到了 self 中,因此您将把它们作为 self 的子元素进行检索,

for (i = 1; i < 4; i++){
   CCSprite * enumHurdle = [self getChildByTag:i];
   enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}

如果您在同一场景中以这种方式创建任何其他精灵,请务必小心。给任意两个精灵赋予相同的标签是糟糕的设计。

编辑有关避免重复标签的信息。

如果您知道您将拥有多少个精灵。使用标签枚举并按名称引用精灵。

如果没有,知道有多少组并限制组的大小可以使其易于管理。

IE
假设您有 3 部分代码,用于生成这样的精灵。您可以在 .m 中(@implementation 行下)包含一个 enum 并将限制放在那里

// Choose names that describe the groups of sprites
enum { kGroupOne = 0, // limiting the size of each group to 100 
    kGroupTwo = 100, // (besides the last group, but that is not important)
    kGroupThree = 200, 
};

然后当您创建每个组

// group 1
for (i = kGroupOne; i < 4; i++){
   // set up code here
}

// group 2 
// g2_size is made up, insert whatever you want
for (i = kGroupTwo; i < g2_size; i++) {
   // set up code here
}
.
.
.

时 然后按组进行检索

for (i = kGroupOne; i < 4; i++){
   CCSprite * enumHurdle = [self getChildByTag:i];
   enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}
.
.
.

希望这能激发您的创造力。现在玩得开心吧。

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 of self

for (i = 1; i < 4; i++){
   CCSprite * enumHurdle = [self getChildByTag:i];
   enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}

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 there

// Choose names that describe the groups of sprites
enum { kGroupOne = 0, // limiting the size of each group to 100 
    kGroupTwo = 100, // (besides the last group, but that is not important)
    kGroupThree = 200, 
};

Then when you create each group

// group 1
for (i = kGroupOne; i < 4; i++){
   // set up code here
}

// group 2 
// g2_size is made up, insert whatever you want
for (i = kGroupTwo; i < g2_size; i++) {
   // set up code here
}
.
.
.

Then to retrieve in groups

for (i = kGroupOne; i < 4; i++){
   CCSprite * enumHurdle = [self getChildByTag:i];
   enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}
.
.
.

Hopefully that sparks your creativity. Now have some fun.

晒暮凉 2025-01-03 17:10:48

我经常做的事情是将类似类型的对象分组,通过将它们添加到 CCNode 并将该 CCNode 添加到层,以类似的方式对其进行操作。

我将创建一个派生自 CCNode 的类,

然后我可以将所有逻辑放入该节点中,然后通过 [selfchildren] 进行访问

for(CCSprite *hurdle in [self children]) {
    // Do what you need to do
}

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]

for(CCSprite *hurdle in [self children]) {
    // Do what you need to do
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文