Cocos2D -拖动精灵没有触摸



我是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];
}

有人能帮忙吗?非常感谢!

第三个效果不太好,即使我勾选'moveDuration'并将其放入动作

中,移动也不流畅。

最新更新