当我试图移动时,精灵消失了

时间:2012-06-18 02:05:14

标签: iphone objective-c xcode cocos2d-iphone 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;
}
    }

1 个答案:

答案 0 :(得分: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;

}