0

我想将我的精灵(宇宙飞船)向用户滑动的方向移动。即,向左移动 - 向左滑动向上移动 - 向上滑动等。

我是刷卡编码的新手,所以请帮忙。

4

1 回答 1

1

这会做:

 - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{


    if (touchState != kSceneStateUngrabbed) return NO;

    // Store the starting touch point
    CGPoint touchPoint = [touch locationInView:[touch view]];
    startTouchPoint = touchPoint;

    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];




    // Change touch state
    touchState = kSceneStateGrabbed;

    return YES;
}



    - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
    {
        CGPoint touchPoint = [touch locationInView:[touch view]];




        float rightleft = touchPoint.x-startTouchPoint.x ;
        float updown =  touchPoint.y-startTouchPoint.y;
        if(abs(rightleft)>abs(updown)){
            if(rightleft>0)
            {
                [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(winsize.width,     spaceShip.position.y)]];

                }
                else
                [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(0, spaceShip.position.y)]];
            }
        else{
                if(updown<0){
                [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(spaceShip.position.x, winsize.height)]];
                        }
                else
                [ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(spaceShip.position.x, 0)]];

            }

    }

    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
    {
        // Change touch state
        touchState = kSceneStateUngrabbed;


    }
于 2013-01-10T10:52:57.253 回答