2014-06-23 1 views
0

사용자의 터치 위치를 지속적으로 얻으려면 어떻게해야합니까? 저는 게임용 콘트롤러에서 일하고 있습니다. 그리고 서있는 그대로 새로운 손가락을 올려서 개별적으로 움직여야 작동합니다. touchLocation 변수는 각 탭에서 업데이트되는 것만으로도 계속 업데이 트되는 것 같습니다. 따라서 손가락을 슬라이드해도 위치를 알고 적절하게 업데이트 할 수 있습니다. 다음은 관련 코드입니다. 고맙습니다!Cocos2d (IOS) - 사용자의 터치 위치를 지속적으로 얻으려면 어떻게해야합니까?

-(void)update:(CCTime)delta{ 

    if([playerDirection isEqual:@"left"]){ 

     _player.position = ccp(_player.position.x - 2, _player.position.y); 


    } 

    if([playerDirection isEqual:@"right"]){ 

    _player.position = ccp(_player.position.x + 2, _player.position.y); 

    } 


    if([direction isEqual:@"up"]){ 


     _enemy.position = ccp(_enemy.position.x, _enemy.position.y + 2); 


    } 


    if([direction isEqual:@"down"]){ 

    _enemy.position = ccp(_enemy.position.x, _enemy.position.y - 2); 


    } 



    if(_enemy.position.y >= self.contentSize.height){ 

    direction = @"down"; 

    } 


    if(_enemy.position.y <= 2){ 

    direction = @"up"; 

    } 

    if(isTouching){ 

     _player.position = ccp(_player.position.x + 2, _player.position.y); 

    } 






} 

// ----------------------------------------------------------------------- 
#pragma mark - Touch Handler 
// ----------------------------------------------------------------------- 




-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

    CGPoint touchLocation = [touch locationInView:[touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 

    if(touchLocation.x > 400){ 

     playerDirection = @"right"; 

    } 

    if(touchLocation.x < 200){ 

    playerDirection = @"left"; 

    } 







    CGPoint touchLoc = [touch locationInNode:self]; 

    //isTouching = true; 



    // Log touch location 
    CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc)); 

    // Move our sprite to touch location 
    //CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc]; 
//[_sprite runAction:actionMove]; 


} 

- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    playerDirection = @"none"; 
} 

답변

1

난 당신이 touchesBegan 및 touchesEnded있어 볼 ...하지만 당신이있는 touchesMoved을 놓치고있어.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for(UITouch *touch in touches) { 
    //do whatever you want 
    } 
} 

희망은 도움이됩니다. 행운을 빕니다!

0

터치를 얻으려면 -(id) init() 메서드에서 터치를 활성화해야합니다. 당신이

self.IsTouchEnable = YES

에 의해 터치를 사용할 수 있습니다이 후 터치 처리 방법을

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

희망을 다음 터치를 얻을 수 이것은 당신에게

도움이 될 것입니다