2013-01-07 110 views

回答

1

這樣做:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 


    if (touchState != kSceneStateUngrabbed) return NO; 

    // Store the starting touch point 
    CGPoint touchPoint = [touch locationInView:[touch view]]; 
    startTouchPoint = touchPoint; 

    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint]; 




    // Change touch state 
    touchState = kSceneStateGrabbed; 

    return YES; 
} 



    - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
     CGPoint touchPoint = [touch locationInView:[touch view]]; 




     float rightleft = touchPoint.x-startTouchPoint.x ; 
     float updown = touchPoint.y-startTouchPoint.y; 
     if(abs(rightleft)>abs(updown)){ 
      if(rightleft>0) 
      { 
       [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(winsize.width,  spaceShip.position.y)]]; 

       } 
       else 
       [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(0, spaceShip.position.y)]]; 
      } 
     else{ 
       if(updown<0){ 
       [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(spaceShip.position.x, winsize.height)]]; 
         } 
       else 
       [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(spaceShip.position.x, 0)]]; 

      } 

    } 

    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
     // Change touch state 
     touchState = kSceneStateUngrabbed; 


    } 
+0

什麼是touchState,kSceneStateUngrabbed?假設startTouchPoint是CGPoint。 – Doug