簡體   English   中英

向滑動方向移動精靈

[英]Move the sprite on the direction of swipe

我想將精靈(太空飛船)沿用戶滑動的方向移動。 即,向左移動-向左滑動向上移動-向上滑動等。

我是滑動編碼的新手,請提供幫助。

這樣做:

 - (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;


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM