簡體   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