私はcocos2dで私の最初のゲームを開発しており、私は正しい解決策を見つけることができない問題に遭遇しました。私は、画面の上部から下部に毎秒CCSpriteを追加してアニメーション化しています。プレイヤーがそれらのいずれかをタッチすると、これらのスプライトを非表示にする必要があります。だから私は、私が追加したすべてのスプライトにタグを与え、後で特定のタグでそのスプライトにアクセスすることを考えました。私はタッチメソッドでそれらにアクセスする前に毎秒増分を取得するので、私はいくつかの配列にタグ番号を置く必要がありますか?Cocos2dで複数のCCSpritesにタッチを実装する方法は?
- (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;
}
}
も私はそれがアニメーション化しながら、自分のキャラクターの実行を行うためにこれらのCCSpriteFrameCacheとCCSpriteBatchNodeオブジェクトを作成する数秒ごとに呼ばれている3つの方法があります。このように毎秒キャッシュを作成するにはあまりにも重くなるでしょうか、あるいはinitメソッドでそれらを作成し、ちょうどCCSpriteのアクションを実行する必要がありますか?