Iphone Cocos2D-无需触摸即可拖动精灵!

Iphone Cocos2D-无需触摸即可拖动精灵!,iphone,xcode,cocos2d-iphone,touch,sprite,Iphone,Xcode,Cocos2d Iphone,Touch,Sprite,我是cocos2d新手,我正在阅读很多教程 我想做的是触摸屏幕,让精灵跟随我的触摸。 释放触摸时,精灵停止 当触摸远离精灵时,即使我继续移动手指,精灵也应该开始移动。 我怎么能做到 我以不同的方式尝试: -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { isMoving = YES; [self schedule:@selector(moveSprite:)]; return YES; }

我是cocos2d新手,我正在阅读很多教程 我想做的是触摸屏幕,让精灵跟随我的触摸。 释放触摸时,精灵停止 当触摸远离精灵时,即使我继续移动手指,精灵也应该开始移动。 我怎么能做到

我以不同的方式尝试:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    isMoving = YES;
    [self schedule:@selector(moveSprite:)];
    return YES;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    isMoving = NO;
    [self unschedule:@selector(moveSprite:)];
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    if (isMoving)
    {
        touchLocation = [touch locationInView: [touch view]];
        touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
        touchLocation = [self convertToNodeSpace:touchLocation];
    }
}

-(void)moveSprite:(ccTime) dt {

    CGPoint moveVector = ccpSub(touchLocation, mySprite.position);

    float distanceToMove = ccpLength(moveVector);
    float velo = 480.0/3.0;
    float moveDuration = distanceToMove / velo;

    self.moveAction = [CCSequence actions:
                       [CCMoveTo actionWithDuration:moveDuration position:touchLocation],
                       nil
                       ];

    [mySprite runAction:_moveAction];   

}
或者再次使用:

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {

    CGPoint touchLocation = [recognizer locationInView:recognizer.view];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];                

    moveAction = [CCSequence actions:
                       [CCMoveTo actionWithDuration:2.0f position:touchLocation],
                       nil
                       ];

    [mySprite runAction:moveAction];
}
还是简单

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{    
    touchLocation = [touch locationInView: [touch view]];       
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];

    [_mySprite stopAction:_moveAction];
    _moveAction = [CCMoveTo actionWithDuration:1 position:touchLocation];
    [_mySprite runAction:_moveAction];
}
有人能帮忙吗?
非常感谢

第三个动作效果不好,即使我检查了“移动持续时间”并将其放入动作中,动作也不流畅

第三个动作在我看来应该有效。它到底在做什么?