[英]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. 我正在创建一个ios游戏,其中有一个Monster类,该类扩展CCSprite大约每秒钟生成一个新的怪物,这些怪物从屏幕的一侧移动到另一侧,然后应将其删除。
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()? 是否存在像getSpriteByID()这样的现有方法? If not, how do I go about implementing something like that?
如果没有,我该如何实施类似的方法?
Monster.h: Monster.h:
#import "CCSprite.h"
@interface Monster : CCSprite
@property (nonatomic, readonly) int instance_id;
@end
Monster.m: 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. 我猜您对cocos2d来说还比较陌生,因此就“正确处置怪物”而言,我建议您打开ARC。 I would highly recommend just google searching a tutorial for turning on ARC with Cocos2d.
我强烈建议您在Google上搜索使用Cocos2d开启ARC的教程。
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]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.