2011-03-12 66 views
2

所以我得到了这个工作:用ccTouchesMoved方法移动CCCamera? (cocos2d的,iphone)

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

     for(UITouch *touch in touches) { 
      CGPoint touchLocation = [touch locationInView: [touch view]]; 
      CGPoint prevLocation = [touch previousLocationInView: [touch view]];  

      touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
      prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation]; 

      CGPoint diff = ccpSub(touchLocation,prevLocation); 
      [self setPosition: ccpAdd(self.position, diff)]; 
     } 
} 

我可以用我的手指在层中移动,但我想移动的cccamera,但我没有与任何cccamera经验。

任何人都可以帮助我吗?

非常感谢您 有一个愉快的一天

:)

回答

2

下面是我使用的是什么......这个实现移动相机是我的cocos2d的论坛上找到。

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

    for(UITouch *touch in touches) { 
     CGPoint touchLocation = [touch locationInView: [touch view]]; 
     CGPoint prevLocation = [touch previousLocationInView: [touch view]];  

     touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
     prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation]; 

     CGPoint diff = ccpSub(touchLocation,prevLocation); 
     [self setPosition: ccpAdd(self.position, diff)]; 

     // Get the camera's current values. 
     float centerX, centerY, centerZ; 
     float eyeX, eyeY, eyeZ; 
     [self.camera centerX:&centerX centerY:&centerY centerZ:&centerZ]; 
     [self.camera eyeX:&eyeX eyeY:&eyeY eyeZ:&eyeZ]; 

     // Increment panning value based on current zoom factor. 
     diff.x = 2 * diff.x * (1+(eyeZ/832)); 
     diff.y = 2 * diff.y * (1+(eyeZ/832)); 

     // Round values to avoid subpixeling. 
     int newX = centerX-round(diff.x); 
     int newY = centerY-round(diff.y); 

     // Set values. 
     [self.camera setCenterX:newX centerY:newY centerZ:0]; 
     [self.camera setEyeX:newX eyeY:newY eyeZ:eyeZ]; 
    } 
} 
+0

+1我会接受它;)但我有一个问题:当相机移动时触摸不能正确检测到。 – cocos2dbeginner 2011-05-28 18:47:57

+0

如何更正触摸检测? – cocos2dbeginner 2011-05-28 19:15:05

+0

,对于迟到的回复感到抱歉:) – cocos2dbeginner 2011-05-28 19:15:44