在Cocos2d中实现多个CCSprit的接触正确的方法??

时间:2011-07-29 07:50:48

标签: iphone cocos2d-iphone ccsprite

我正在用cocos2d开发我的第一个游戏,我遇到了一个问题,我似乎找不到合适的解决方案。我每隔一秒从屏幕顶部添加动画CCSprite到底部,并且当玩家触摸其中任何一个时需要隐藏这些精灵。所以我想为我添加的每个精灵都提供标签,然后在访问该特定标签的精灵时使用。我是否需要将标签号放在某个数组中,因为它们每秒都会增加,甚至在我用触摸方法访问它们之前?

- (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上的操作?

1 个答案:

答案 0 :(得分:2)

子类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];
}

最后,你的触摸:方法:

- (void) touch:(id)sender {
    NewCCSprite* sprite = (NewCCSprite*)sender;
    // hide/kill off/etc
}