2011-01-13 167 views
6

如何在特定位置移动物体。例如如何在cocos2d的特定位置移动物体

。我有一个小酒吧(宽= 50,高= 10)。我必须手动移动柱塞。我只想移动x坐标(限制是x = 20(起点)到x = 50(终点))在y坐标上不移动。但是它在没有任何动作后将其移动50到10。 alt text 编码: -

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (isPlaying) { 
     UITouch *touch = [[event allTouches] anyObject]; 

     touchPosition = [touch locationInView:touch.view]; 
     if (CGRectContainsPoint(para3.boundingBox,touchPoint) 
       isDragging = YES; 

     touchOffset = para3.position.y - touchPosition.y; 

    } 
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
if (isPlaying) { 
UITouch *touch3 = [[event allTouches] anyObject]; 
     float distanceMoved = 
     ([touch3 locationInView:touch3.view].y + touchOffset) - 
     para3.position.y; 
     float newY = para3.position.y + distanceMoved; 
     if (newY > 67 && newY < 99) 
      para3.position = CGPointMake(newY , para3.position.y ); 
     //para3.contentSize/2 
     if (newY >67) 
      para3.position = CGPointMake(67, para3.position.y); 
     if (newY < 99) 
      para3.position = CGPointMake(99, para3.position.y); 
    } 
} 
+0

你必须寻找不同的坐标系的科科斯和UIView。所以你需要从locationInView转换点。我不记得确切。但CCDirector是实现这种方法的类。 ;-)我希望这有助于。 – 2011-01-13 20:36:11

回答

2

我希望我已经完全明白这个问题。在这种情况下我会做的是在我的游戏中包含Chipmunk框架,然后将我的柱塞和大炮作为物理对象。一旦完成,就可以通过框架的“ApplyImpulse”方法来控制速度和方向(即弹丸的角度)。速度和角度会受到物理自身控制,一旦我将提供初始值....