在Cocos2d中以正确的方式在多个CCSprites上执行触摸?



我正在使用cocos2d开发我的第一款游戏,我遇到了一个我似乎找不到正确解决方案的问题。我每秒钟从屏幕顶部到底部添加一个CCSprite并使其动画化,并且需要在玩家触碰其中任何一个时隐藏这些sprite。所以我想到给我添加的每个精灵添加标签,然后用特定的标签访问那个精灵。我需要把标签号在一些数组中,因为他们得到每秒递增,甚至在我访问触摸方法之前??

- (void)addStraightBugs 
{
currentAntTag++;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"smallAunt.plist"];        
spriteSheetmedAnt = [CCSpriteBatchNode batchNodeWithFile:@"smallAunt.png"];
[self addChild:spriteSheetmedAnt z:0 tag:kSpriteManager];
CCSprite *ant= [CCSprite spriteWithSpriteFrameName:@"small-aunt1.png"];
[spriteSheetmedAnt addChild:ant z:1 tag:currentAntTag];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i) {
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"small-ant%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.15f];
CCAction *action=[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];

ant.position = ccp(100,500);
[ant runAction:action];
CGPoint realDest = ccp(60,140);
int minDuration = 2.0;
int maxDuration = 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
[ant runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:actualDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(moveFinished:)],
                       nil]];
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event  
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CCSpriteBatchNode *spriteManager;
spriteManager = (CCSpriteBatchNode*)[self getChildByTag:kSpriteManager];
CCSprite *ant = (CCSprite*)[spriteManager getChildByTag:currentAntTag];
CGRect abc= CGRectInset([ant boundingBox],30, 85);
if(CGRectContainsPoint(abc,touchLocation))  
{
    ant.visible=NO;
}
}

我也有3个方法,每隔几秒钟调用一次,我创建这些CCSpriteFrameCache和CCSpriteBatchNode对象,使我的角色在动画时运行。像这样每秒创建缓存会不会太沉重,或者我应该在init方法中创建它们,然后在CCSprite上运行动作?

子类CCSprite。然后在init中:

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

实现ccTouch方法:

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

现在你有两个选择。你可以有一个委托变量去回调或者使用NSNotifications。这里用回调,它更快。

// in your @interface
id touchDelegate; // between the {}'s
@property (nonatomic, assign) id touchDelegate;

在你的游戏类中,当你创建坏角色时:

NewCCSprite = newSprite = [NewCCSprite init];
newSprite.touchDelegate = self;

当你碰到一个时:

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    if (self.touchDelegate == nil) return;
    [self.touchDelegate performSelector:@selector(touched:) withDelay:0.0f];
}
最后,你的touch: method:
- (void) touch:(id)sender {
    NewCCSprite* sprite = (NewCCSprite*)sender;
    // hide/kill off/etc
}

最新更新