【问题标题】:How can i drag a CCSprite with velocity/speed in cocos2d by using CCTouchMoved?如何使用 CCTouchMoved 在 cocos2d 中以速度/速度拖动 CCSprite?
【发布时间】:2012-06-18 22:18:39
【问题描述】:

我有一个精灵,我想用手指在屏幕上移动~拖动来移动它。 我希望我的精灵以速度移动,这意味着不要像我的手指移动那么快。

好像是这个视频:http://www.youtube.com/watch?v=Vair3CIxZEw(从 0:12 到 0:53)

这是我的 ccTouch 代码。我该如何解决以使其移动看起来更流畅?

谢谢!!! :)

直接返回 TRUE

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    return TRUE;  
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];


    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
    if (CGRectContainsPoint(_car.boundingBox, touchLocation)) {            
        CGPoint newPos = ccpAdd(_car.position, translation);
        _car.position = newPos;
    }
}

【问题讨论】:

    标签: ios cocos2d-iphone ccsprite


    【解决方案1】:

    尝试使用 CCMoveTo 动作平滑移动

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
    if (CGRectContainsPoint(_car.boundingBox, touchLocation)) {
        CGPoint newPos = ccpAdd(_car.position, translation);
        id moveAction = [CCMoveTo actionWithDuration:0.5f position:newPos];
        [_car runAction:moveAction];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多