2012-08-08 95 views
0

我有一個問題,我似乎無法解決。 我想要根據手指移動在屏幕上移動精靈,但不是讓精靈向移動,我想讓精靈移動到相同的方式手指在屏幕上移動。Cocos2D雪碧相對運動

示例: 用戶將他/她的手指放在屏幕上(任意位置),然後將屏幕上的手指向左拖動200像素,精靈也應向左移動200像素。

我的方法是在開始時存儲玩家的位置,然後計算手指的起始位置和當前位置之間的差值,以便將其添加到精靈本身的起始位置。

這裏是我的代碼

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
if([touches count] == 1){ 
    UITouch *touch = [touches anyObject]; 
    CGPoint startPos = [touch locationInView:[touch view]]; 
    startPosition = startPos; 
} 

}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if([touches count] == 1){ 
     UITouch *touch = [touches anyObject]; 
     CGPoint touchPos = [touch locationInView:[touch view]]; 
     CGPoint playerPos = player.position; 

     float yDifference = startPosition.y - touchPos.y; 
     playerPos.y = playerPos.y - yDifference; 

     // just to change to GL coordinates instead of the ones used by Cocos2D 
     // still doesn't work even if I remove this... 
     CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:playerPos]; 
     [player setPosition:convertedPoint]; 
    } 

}

我希望我的問題是清楚的,但如果它不是不要猶豫可能會問的問題或進一步解釋。我一直停留在這個相當長的一段:「(

回答

1

另外,如果你想讓它動「相對「到那裏開始,而不是正好在觸摸是:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if([touches count] == 1){ 
     UITouch *touch = [touches anyObject]; 
     CGPoint startPos = [touch locationInView:[touch view]]; 
     startPosition = startPos; 
     playerStartPos = playerPos; 
    } 
} 

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if([touches count] == 1){ 
     UITouch *touch = [touches anyObject]; 
     CGPoint touchPos = [touch locationInView:[touch view]]; 

     CGPoint newPlayerPos; 
     newPlayerPos.x = playerStartPos.x + (touchPos.x - startPosition.x); 
     newPlayerPos.y = playerStartPos.y + (touchPos.y - startPosition.y); 

     // just to change to GL coordinates instead of the ones used by Cocos2D 
     // still doesn't work even if I remove this... 
     CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:newPlayerPos]; 
     [player setPosition:convertedPoint]; 
} 

您的代碼看起來像它的‘通過不斷將差於playerPos.y反饋’到自身

+0

你的代碼似乎工作,與一些變化的變化。 完全按照我想要的方式,除了它在Y軸上翻轉的事實,似乎「convertToGL」部分是問題的一部分,因爲它使得字符從位置跳到另一個位置 – 2012-08-08 14:42:58

+0

與之相反,整個「convertToGL」的東西,你有沒有嘗試過「touchPos = [player.parent convertTouchToNodeSpace:touch]」?順便說一句,「startPos」需要使用相同的方法。這樣你就可以在同一個座標空間中進行處理。 – ICanChange 2012-08-08 20:34:29

+0

我已經對所有東西進行了排序,而不是添加我應該減去的差異,完美地工作。 再次感謝您的幫助! – 2012-08-09 01:19:53

0

所以根本就沒有使用的不同,使用當前觸摸位置:

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if([touches count] == 1){ 
     UITouch *touch = [touches anyObject]; 
     CGPoint playerPos = [touch locationInView:[touch view]]; 

     // just to change to GL coordinates instead of the ones used by Cocos2D 
     // still doesn't work even if I remove this... 
     CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:playerPos]; 
     [player setPosition:convertedPoint]; 
    }