问题描述
- 创建多个相同的CCSprites
-
有么有方法能快速的创建任意数量的相同CCSprites?就为了后来删除,然后只需要一个标签引用他们
i.e.在游戏里面:
- (void)displayOneLife { CGPoint positionOne = ccp(90, 450); CCSprite *life1 = [CCSprite spriteWithFile:@"life.png"]; [life1 setPosition:positionOne]; [life1 setScale:0.5f]; [self addChild:life1 z:5 tag:1]; } - (void)displayTwoLives { CGPoint positionOne = ccp(90, 450); CGPoint positionTwo = ccp(105, 450); CCSprite *life1 = [CCSprite spriteWithFile:@"life.png"]; CCSprite *life2 = [CCSprite spriteWithFile:@"life.png"]; [life1 setScale:0.5f]; [life2 setScale:0.5f]; [life1 setPosition:positionOne]; [life2 setPosition:positionTwo]; [self addChild:life1 z:5 tag:1]; [self addChild:life2 z:5 tag:2]; } - (void)displayThreeLives { CGPoint positionOne = ccp(90, 450); CGPoint positionTwo = ccp(105, 450); CGPoint positionThree = ccp(120, 450); CCSprite *life1 = [CCSprite spriteWithFile:@"life.png"]; CCSprite *life2 = [CCSprite spriteWithFile:@"life.png"]; CCSprite *life3 = [CCSprite spriteWithFile:@"life.png"]; [life1 setPosition:positionOne]; [life2 setPosition:positionTwo]; [life3 setPosition:positionThree]; [life1 setScale:0.5f]; [life2 setScale:0.5f]; [life3 setScale:0.5f]; [self addChild:life1 z:5 tag:1]; [self addChild:life2 z:5 tag:2]; [self addChild:life3 z:5 tag:3]; }
解决方案
创建CCTexture2D
,然后初始化全部sprite。
通过编程实现:
- (void)displayLifes:(int) nrOfLifes
{
CGPoint position = ccp(90, 450);
CCTexture2D *texture = [[[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"life.png"]]autorelease];
for(int i = 1 ; i <= nrOfLifes ; i++)
{
CCSprite *life = [CCSprite spriteWithTexture:texture];
[life setPosition:position];
[life setScale:0.5f];
[self addChild:life z:5 tag:i];
position.x += 15;
}
}
时间: 2025-01-26 07:01:26