如何在cocos2d中的特定位置移动对象

Sri*_*vas 6 iphone objective-c sprite cocos2d-iphone

如何在特定位置移动对象.

举些例子.我有一个小条(宽= 50,高= 10).我必须手动移动这个柱塞.我想只在x cordinates中移动(限制是x = 20(起始点)到x = 50(终点))y坐标上没有移动.但是在病房没有动静的情况下它移动了50到10.替代文字 编码:-

- (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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

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