2012-07-27 3 views
0

나는 다음과 같은 프로그램을 작성했다 : 사용자가 움직이는 스프라이트를 터치하면 장면에서 제거되어야한다.Cocos2d의 터치, 약간의 버그

그러나 코드를 실행하면 다음과 같은 일이 발생합니다. uppest 스프라이트를 터치하면 이웃이 사라집니다. 어떻게 해결할 수 있습니까?

다음은 코드입니다.

UPD :이 코드를 더 작은 * .png 파일에서 테스트했는데 모든 것이 잘 작동합니다. 하지만 더 큰 * .png 파일 (200x200 픽셀, 아이폰 시뮬레이터와 같은 smth)에 버그가 있습니다. 객체가 가장 가까운 이웃과 touchEvent에서 제거됩니다.

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 


@interface GameplayLayer : CCLayer { 

    NSMutableArray *arrayOfSprites; 

} 
@end 
    #import "GameplayLayer.h" 
@implementation GameplayLayer 
    -(id)init 
{ 
    self = [super init]; 
    self.isTouchEnabled=YES; 
    arrayOfSprites=[[NSMutableArray alloc] init]; 

    if (self != nil) 
    { 
     int j=100; 

     for (int i=0; i<=2; i++) { 
      [arrayOfSprites addObject:[CCSprite spriteWithFile:@"sv_anim_1-hd.png"]]; 
      [[arrayOfSprites objectAtIndex:i] setPosition:CGPointMake(j,j)]; 
      [self addChild:[arrayOfSprites objectAtIndex:i] z:0 tag:i]; 
      j+=100; 
     } 
     [self startRunning]; 
    } 
    return self;         
} 
    -(void)startRunning 
{ 
    CGSize screenSize=[[CCDirector sharedDirector] winSize]; 
    for (CCSprite * currentSprite in arrayOfSprites) 
    { 
     [currentSprite runAction:[CCMoveTo actionWithDuration:10 position:CGPointMake(screenSize.height,screenSize.width/2)]]; 
    } 
} 
     -(void) registerWithTouchDispatcher 
    { 
     [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 
                swallowsTouches:YES]; 
    } 

     -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
     return YES; 
    } 

     -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 
     CGPoint locationOfTouch=[self convertTouchToNodeSpace: touch]; 

     for (CCSprite *currentSprite in arrayOfSprites) 
     { 
      if (CGRectContainsPoint(currentSprite.boundingBox, locationOfTouch)) 
      { 
       NSLog(@"Sprite was touched"); 
       [self removeChild:currentSprite cleanup:YES]; 
      } 
     } 

    } 
    @end 

답변

0

정의는이 기능을 사용해보십시오 : 귀하의 회신에 대한

-(CGRect)getSpriteRect:(CCNode *)inSprite 
{ 
    CGRect sprRect = CGRectMake(
           inSprite.position.x - inSprite.contentSize.width*inSprite.anchorPoint.x, 
           inSprite.position.y - inSprite.contentSize.height*inSprite.anchorPoint.y, 
           inSprite.contentSize.width, 
           inSprite.contentSize.height 
           ); 

    return sprRect; 
} 


- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 


    for (CCSprite *currentSprite in arrayOfSprites) 
    { 
     CGRect rect = [self getSpriteRect:currentSprite]; 

     if (CGRectContainsPoint(rect, location)) 
     { 
      NSLog(@"Sprite was touched"); 
      [self removeChild:currentSprite cleanup:YES]; 
     } 
    } 
} 
+0

들으하지만 제대로 작동하지 않습니다 여전히 같은 버그가 발생합니다. –

+0

@Taras Murzenkov, 이봐, UR 질문을 편집하고 GameplayLayer.h 인터페이스 선언을 추가하고 u가이 클래스를 초기화 한 코드를 보여줍니다. – Guru

+0

@Raj, CCSprites에는 boundingBox 속성이 있습니다. 'yourSprite.boundingBox'라고 생각합니다. – tallen11