繁体   English   中英

Objective-C:如何跟踪对象? (getByID)?

[英]Objective-C: How do I keep track of objects? (getByID)?

我对创建对象后如何访问对象以及这些对象的作用域如何工作感到困惑。

我正在创建一个ios游戏,其中有一个Monster类,该类扩展CCSprite大约每秒钟生成一个新的怪物,这些怪物从屏幕的一侧移动到另一侧,然后应将其删除。

我需要能够访问屏幕上当前存在的所有怪物,并且要确保正确处理掉屏幕外的怪物。

是否存在像getSpriteByID()这样的现有方法? 如果没有,我该如何实施类似的方法?

Monster.h:

#import "CCSprite.h"

@interface Monster : CCSprite

@property (nonatomic, readonly) int instance_id;

@end

Monster.m:

#import "Monster.h"

static int global_id = 0;

@implementation Monster:CCSprite



-(id) init{
    self = [super init];

    if(self){
        _instance_id = global_id;
        global_id++;
    }
    return self;
}
-(id) initWithTexture:(CCTexture *)texture rect:(CGRect)rect{
    self = [super initWithTexture:texture rect:rect];
    if(self)
    {
        _instance_id  = global_id;
        global_id++;

    }
    return self;
}

@end

生成函数:

- (void)generateTerrain:(CCTime)dt {

    Monster *monster =  [Monster spriteWithImageNamed:@"monster.png"];

    int i = monster.instance_id;

    NSString* myNewString = [NSString stringWithFormat:@"%i", i];
    CCLOG(@"MonsterID @ %@",myNewString);


    int randomY = (arc4random() % 4) + 1;

    if (randomY == 1)
    {
        monster.position = CGPointMake(self.contentSize.width + 20, pos1);
    }
    else if (randomY == 2)
    {
        monster.position = CGPointMake(self.contentSize.width + 20, pos2);
    }
    else if (randomY == 3)
    {
        monster.position = CGPointMake(self.contentSize.width + 20, pos3);
    }
    else if (randomY == 4)
    {
        monster.position = CGPointMake(self.contentSize.width + 20, pos4);
    }

    // 2
    [self addChild:monster];


    // 3
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int randomDuration = (arc4random() % rangeDuration) + minDuration;

    // 4
    CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(-monster.contentSize.width/2, monster.position.y)];
    CCAction *actionRemove = [CCActionRemove action];
    [monster runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];

}

我个人会将所有的怪物存储在一个数组中。

我猜您对cocos2d来说还比较陌生,因此就“正确处置怪物”而言,我建议您打开ARC。 我强烈建议您在Google上搜索使用Cocos2d开启ARC的教程。

但就目前而言,我将为您提供一个可行的简单解决方案。

NSMutableArray *monstersArray;

@implementation

- (void)generateTerrain:(CCTime)dt {

    Monster *monster =  [Monster spriteWithImageNamed:@"monster.png"];
    monstersArray = [[NSMutableArray alloc] init];
    ...
    [self addChild:monster]
    [monstersArray addObject:monster];
}

// Use this (called 60 fps) to remove a monster
-(void) update:(ccTime)delta {
    for (int i = 0; i < monstersArray.count; i++) {
        Monster *monster = monstersArray[i];
        if (monster.position.x < 0) {
        [self removeMonsterFromArray:monster]
    }
}

// Method to remove monsters, just call this, for example, if a monster is off the screen (it's position in x is less than 0 or something
- (void)removeMonsterFromArray:monster {
    [self removeChild:monster]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM