Cocos2d:根据Touch移动CCsprite

时间:2014-02-21 06:11:18

标签: ios objective-c cocos2d-iphone

我想创建一个游戏,我想根据触摸移动CCSprite(眼球图像)。

像眼球一样应该按照我的触摸方向移动,但要移动到特定区域。我是Cocos2d的新人,所以我不知道如何实现这一目标。

所以任何人都可以帮助我在touchesMoved

上实现眼球效果

1 个答案:

答案 0 :(得分:1)

您必须实施ccTouchMoved方法

- (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);    
    CGPoint newPos = ccpAdd(mySpriteToMove.position, translation);
    mySpriteToMove.position = newPos;
}

查看本教程How To Drag and Drop Sprites