沿着滑动方向移动精灵

时间:2013-01-07 12:32:20

标签: ios cocos2d-iphone swipe

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

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

1 个答案:

答案 0 :(得分: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;


    }