简体   繁体   中英

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

I am confused on how to access objects after I create them, as well as how the scope on these objects work.

I am creating an ios game in which I have a class Monster which extends CCSprite generates a new monster every second or so, and these monsters travel from one side of the screen to the other side, then should be deleted.

I need to be able to access all the monsters currently on the screen, and I want to make sure the monsters off screen are properly disposed of.

Is there an existing method like getSpriteByID()? If not, how do I go about implementing something like that?

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

The generating function:

- (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]]];

}

I personally would store all of your monsters in an array.

I'm guessing you're relatively new to cocos2d, so in terms of "properly disposing of your monsters" I would recommend turning on ARC. I would highly recommend just google searching a tutorial for turning on ARC with Cocos2d.

For now though, I'll give you a simple solution that should work.

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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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