2011-07-29 8 views
1

나는 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에서 액션을 실행해야합니까 ??

답변

2

하위 클래스 CCSprite. 그런 다음 초기화 년대 :

[[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 
}