1

私はcocos2dを使って最初のゲームを開発していますが、正しい解決策を見つけることができないという問題に遭遇しました。画面の上部から下部に向かって毎秒CCSpriteを追加およびアニメーション化しており、プレーヤーがこれらのスプライトのいずれかに触れたときにこれらのスプライトを非表示にする必要があります。そこで、追加するすべてのスプライトにタグを付け、後でその特定のタグを使用してそのスプライトにアクセスすることを考えました。touchメソッドでタグ番号にアクセスする前でも、タグ番号は1秒ごとにインクリメントされるため、配列にタグ番号を入力する必要がありますか?

- (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でアクションを実行する必要がありますか?

4

1 に答える 1

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;

今、2つのオプションがあります。コールバックするか、NSNotificationsを使用するためのデリゲート変数を持つことができます。ここでコールバックを使用してください。より高速です。

// in your @interface
id touchDelegate; // between the {}'s

@property (nonatomic, assign) id touchDelegate;

ゲームクラス内で、悪者を作成する場合:

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

そして、あなたが1つに触れるとき:

- (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
}
于 2011-08-17T20:52:13.733 に答える