2012-06-18 5 views
1

J'essaie de faire bouger mon sprite au toucher mais semble disparaître au toucher puis réapparaître au deuxième toucher. Je ne sais pas comment résoudre ce problème pour que mon sprite bouge à la direction que je touche. J'ai essayé de comprendre ceci pendant un moment mais semble que je n'ai pas de chance. J'espère que quelqu'un peut me diriger dans la bonne direction.Sprite disparaissant lorsque j'essaie de le déplacer

 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; 
} 
    } 

Répondre

0

La syntaxe de votre fonction tactile semble être différente.

essayer ce code à la place

- (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; 

} 
Questions connexes