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

6

如何将对象移动到特定位置。

例如,我有一个小条形物体(宽度=50,高度=10)。我想手动将其移动,就像活塞一样。我只想在x坐标上移动(限制是x = 20(起始点)到x = 50(终点)),不要在y坐标上移动。但是它移动到50后停止移动。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 );
    }
}

你需要查找Cocos和UIView的不同坐标系统。因此,你需要将点从locationInView进行转换。我不太记得了。但是CCDirector是实现这些方法的类。;-)希望这可以帮到你。 - Sandro Meier
1
para3.position = cpp(x,y); 在使用cocos2d时,必须使用cpp(x, y)而不是CGPointMake(x, y)。 - 0xDE4E15B
2个回答

2

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


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接