문제

CoCOS2D를 사용하여 iPhone 용 2D 게임을 개발하고 있습니다.

나는 게임에서 많은 작은 스프라이트 (이미지)를 사용합니다. 두 가지 유사한 유형의 스프라이트 (이미지)를 만지면 스프라이트 (이미지)가 숨겨집니다.

특정 스프라이트 (이미지)에서 터치를 감지하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

스프라이트가 포함 된 레이어에서는 다음과 같이 말해야합니다.

self.isTouchEnabled = YES;

그런 다음 Uiview에서 사용할 것과 동일한 이벤트를 사용할 수 있지만 약간 다르게 이름을 올릴 수 있습니다.

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   UITouch* touch = [touches anyObject];
  //in your touchesEnded event, you would want to see if you touched
  //down and then up inside the same place, and do your logic there.
}

다른 팁

이를 수행하는 더 좋은 방법은 실제로 스프라이트 자체 (cgrect)의 경계 상자를 사용하는 것입니다. 이 샘플 코드에서, 나는 모든 스프라이트를 nsmutablearray에 넣고 스프라이트 터치가 경계 상자에 있는지 간단하게 확인합니다. Init에서 터치 감지를 켜야합니다. 당신이 알면 예를 반환하여 레이어에 대한 터치를 수락/거부하는 경우 (터치를 사용하는 경우) 또는 아니오 (그렇지 않은 경우)

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{
  CGPoint location = [self convertTouchToNodeSpace: touch];

  for (CCSprite *station in _objectList)
  {
    if (CGRectContainsPoint(station.boundingBox, location))
    {
      DLog(@"Found sprite");
      return YES;
    }
  }

  return NO;
}

Jonas의 지시에 따라 조금 더 추가 ...

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   UITouch* touch = [touches anyObject];
   CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
   CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
   if(CGRectContainsPoint(particularSpriteRect, location)) {
     // particularSprite touched
     return kEventHandled;
   }
}

Cocos의 '중앙 위치'를 설명하기 위해 X/YA Little을 조정해야 할 수도 있습니다.

@David, 귀하의 코드에는 Cocos 0.7.3 및 2.2.1에 대한 오타가 있으며, 특히 CGMakerect 대신 CGRECTMAKE 및 [Touch Location]은 이제 [Touch LocationInView : Touch.View]입니다.

내가 한 일은 다음과 같습니다.

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
    CGRect myRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height);


    if(CGRectContainsPoint(myRect, location)) {
        // particularSprite touched
        return kEventHandled;
    }
}

@genericrich : cgrectcontainspoint 위의 호출 2 줄로 인해 Cocosland에서 작동합니다.

[[Director sharedDirector] convertCoordinate:]

CoCOS2D 객체는 OpenGL 좌표계를 사용하는데, 여기서 0,0은 왼쪽 하단이며 Uikit 좌표 (터치가 발생한 곳)는 0,0이 왼쪽 상단입니다. ConvertCoordinate : Uikit에서 Opengl로 플립을 만들고 있습니다.

여기가 나에게 어떻게 효과가 있었는지 ... Spritesize가 스프라이트의 크기 인 곳 ... : P

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
    CGRect myRect = CGRectMake(sprite.position.x-spriteSize/2, sprite.position.y-spriteSize/2, spriteSize, spriteSize);


    if(CGRectContainsPoint(myRect, location)) {
        // particularSprite touched
        return kEventHandled;
    }
}

이것은 기본 터치 시스템을 설명하는 좋은 튜토리얼입니다.http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/

먼저 쓰기

self.isTouchEnabled = YES;

그런 다음 CCTouchesended, cctouchesbegan 등 함수를 구현해야합니다.

내가 이해 한 바에 따르면 화면의 다른 좌표에있을 수있는 두 개의 스프라이트를 '일치'할 수 있기를 원합니다.

이 작업을 수행하는 방법 .. : (다른 많은 방법이 확실합니다)

2 개의 글로벌 변수를 고려하십시오.

따라서 터치가 스프라이트에 닿을 때마다 CgrectContainspoint 기능을 여러 번 언급하여 어떤 스프라이트가 터치 된 지 찾습니다. 그런 다음 글로벌 변수 중 하나에 해당 스프라이트의 '태그'를 저장할 수 있습니다.

두 번째 터치에 대해서도 동일한 작업을 수행 한 다음 2 개의 글로벌 변수를 비교합니다.

나머지를 알아낼 수 있지만 문제가있는 경우 의견을 제시해야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top