在更新循环中跟踪sprite在数组中的位置 [英] Keep track of sprite position in an array on update loop

查看:95
本文介绍了在更新循环中跟踪sprite在数组中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个循环添加怪物到NSMutableArray

Hi I have a loop adding monsters to an NSMutableArray

 - (void)addMonster:(CCTime)dt {


 _monster1 = [[Monster1 alloc] init];




for(int i=0;i<=num; i++)
{

    [spriteArray insertObject:_monster1  atIndex:num];

}

[_physicsWorld addChild:_monster1];


num++;
}

在我的更新循环中,这个代码是为了检查每个精灵是否通过了某些点:

in my update loop this code is meant to check if each sprite has passed a certain point:

for (int i=0; i <= num; i++) 
{
    CCSprite *tempSprite = (CCSprite *) [spriteArray objectAtIndex:i];


    if (tempSprite.position.x > 100) {

    structurelife--;


    }

}



>然而这个代码不工作(减少int结构生命取决于多少个怪物通过那一点,我很抱歉,任何帮助将不胜感激)。

However this code isn't working (to decrease the int structurelife depending on how many monsters are passed that point, I'm pretty stuck so any help would be appreciated). The position of the Sprite remains at 0,0 even though it is moving accross the screen.

推荐答案

根据您的OP是什么试图做,并根据你写的代码,你的代码是不正确的。你说你正在尝试添加怪物到数组,而是你正在做的是添加指向同一个怪物的数组一遍又一遍。

Based on what your OP is trying to do and based on the code you wrote, your code is not correct. You state you are trying to add "monsters" to an array but instead what you are doing is adding pointers to the same monster to the array over and over again.

你的循环本质上是添加相同的怪物到它多次,而不是添加不同的怪物到数组。是否是你的代码的唯一问题不能由发布的小量确定,但启动您的方法应该看起来像这是基于你似乎试图做:

Your loop is essentially adding the same monster to it multiple times, rather than adding different monsters to the array. Whether it is the only problem with your code can't be determined by the small amount that was posted, but to start your method should look like this based on what you seem to be trying to do:

- (void)addMonster:(CCTime)dt
{
    for (int i=0; i<=num; i++)
    {
        Monster1 monster = [[Monster1 alloc] init];
        [spriteArray insertObject:monster atIndex:num];
        [_physicsWorld addChild:monster];
    }

    num++;
}

但这又是基于您发布的代码。

But again this is based on the code you have posted.

我希望这有帮助。

这篇关于在更新循环中跟踪sprite在数组中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆