2012-06-18 79 views
1

我試圖讓我的精靈在觸摸時移動,但似乎在觸摸時消失,然後在第二次觸摸時重新出現。我不知道如何解決這個問題,讓我的精靈在我點擊的方向移動。我一直試圖弄清楚這一點,但似乎我運氣不好。我希望有人能指出我正確的方向。當我嘗試移動它時,Sprite消失

 CGSize winSize = [[CCDirector sharedDirector] winSize]; 
    player = [CCSprite spriteWithFile:@"Player.png" 
             rect:CGRectMake(0, 0, 27, 40)]; 
    player.position = ccp(player.contentSize.width/2, winSize.height/2); 
    [self addChild:player z:1]; 










      (void) registerWithTouchDispatcher 
     { 
      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self 
              priority:0 swallowsTouches:YES]; 
    } 

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

    -(void)setPlayerPosition:(CGPoint)position { 
     player.position = position; 
    } 

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

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

    CGPoint playerPos = player.position; 
    CGPoint diff = ccpSub(touchLocation, playerPos); 
    if (abs(diff.x) > abs(diff.y)) { 
     if (diff.x > 0) { 
    playerPos.x += contentSize_.width; 
} else { 
    playerPos.x -= contentSize_.width; 
}  
    } else { 
if (diff.y > 0) { 
     playerPos.y += contentSize_.height; 
} else { 
    playerPos.y -= contentSize_.height; 
} 
    } 

回答

0

你的觸摸功能的語法看起來不一樣。

試試這個代碼,而不是

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

    CGPoint playerPos = player.position; 

} 
相關問題