2011-09-19 82 views
0

我的ccTouchMoved方法存在問題。我有3個ccSprites,我想用手指在屏幕上移動。事情是,運動是生澀的,有時甚至在拖動圖像時,如果我將鼠標指針移動到快速,它甚至會失去ccSprites的「抓地力」。COCOS2D。使用ccTouchMoved拖動屏幕上的CCSprite會使運動干擾

我錯過了什麼?

,我使用有以下任務三個touchmethods:

ccTouchBegan:擴大正被感動

ccTouchMoved的ccSprite:移動是在屏幕上

ccTouchEnded觸及ccSprite:按比例縮小ccSprite到原來的大小和停止運動(有縮小的ccSprite到它的原始大小的更好的辦法?)

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 

CCLOG(@"ccTouchBeganRuby"); 

CGPoint location = [self convertTouchToNodeSpace: touch]; 

if(CGRectContainsPoint([animatingRuby1 boundingBox], location)){ 
    CCLOG(@"ccTouchEndedRubyScaleUp1"); 
    id ScaleAction1 = [CCScaleBy actionWithDuration:1 scale:1.2f ]; 
    [animatingRuby1 runAction:ScaleAction1]; 
} 
if(CGRectContainsPoint([animatingRuby2 boundingBox], location)){ 
    CCLOG(@"ccTouchEndedRubyScaleUp2"); 
    id ScaleAction2 = [CCScaleBy actionWithDuration:1 scale:1.2f ]; 
    [animatingRuby2 runAction:ScaleAction2]; 
} 
if(CGRectContainsPoint([animatingRuby3 boundingBox], location)){ 
    CCLOG(@"ccTouchEndedRubyScaleUp3"); 
    id ScaleAction3 = [CCScaleBy actionWithDuration:1 scale:1.2f ]; 
    [animatingRuby3 runAction:ScaleAction3]; 
} 

return YES; 

}

- (void)ccTouchMoved:(UITouch *)touches withEvent:(UIEvent *) event{ 

CCLOG(@"ccTouchMovedRuby"); 

if (CGRectContainsPoint([animatingRuby1 boundingBox], location)) { 
    CCLOG (@"moveRubies1"); 
    if((CGRectContainsPoint([animatingRuby2 boundingBox], location))|| (CGRectContainsPoint([animatingRuby1 boundingBox], location))) { 
     return; 
    } 
    animatingRuby1.position = location; 

} 
if (CGRectContainsPoint([animatingRuby2 boundingBox], location)) { 
    CCLOG (@"moveRubies2"); 
    if((CGRectContainsPoint([animatingRuby1 boundingBox], location))|| (CGRectContainsPoint([animatingRuby3 boundingBox], location))) { 
     return; 
    } 
    animatingRuby2.position = location; 
} 
if (CGRectContainsPoint([animatingRuby3 boundingBox], location)) { 
    CCLOG (@"moveRubies3"); 
    if((CGRectContainsPoint([animatingRuby1 boundingBox], location))|| (CGRectContainsPoint([animatingRuby2 boundingBox], location))) { 
     return; 
    } 
    animatingRuby3.position = location; 

} 
[self collidableWithEachOther:location]; 

}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *) event{ 
CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
CCLOG(@"ccTouchEndedRuby"); 
CGPoint location = [self convertTouchToNodeSpace: touch]; 

if(CGRectContainsPoint([animatingRuby1 boundingBox], location)){ 
    CCLOG(@"ccTouchEndedRubyScaleDown1"); 
    [animatingRuby1 setScaleX:screenSize.width/14850.0f]; 
    [animatingRuby1 setScaleY:screenSize.height/9552.0f]; 

} 
if(CGRectContainsPoint([animatingRuby2 boundingBox], location)){ 
    CCLOG(@"ccTouchEndedRubyScaleDown2"); 
    [animatingRuby2 setScaleX:screenSize.width/14850.0f]; 
    [animatingRuby2 setScaleY:screenSize.height/9552.0f]; 
} 
if(CGRectContainsPoint([animatingRuby3 boundingBox], location)){ 
    CCLOG(@"ccTouchEndedRubyScaleDown3"); 
    [animatingRuby3 setScaleX:screenSize.width/14850.0f]; 
    [animatingRuby3 setScaleY:screenSize.height/9552.0f]; 
} 

}

回答

2

從子畫面的觸摸位置以該載體,並將其設置爲子畫面的方向,同時限制其速度。這種方式的變化不會那麼快速和激烈,運動看起來更順暢。不要求觸摸位置始終位於精靈之上,只需跟蹤拖動操作是否正在進行以及拖動哪個精靈。

+0

對不起,如果我聽起來像一個完全noob(雖然我是一個noob),但我不明白你的意思。我仍然在學習,所以你可以用一個例子來展示我...... – peteriphonenoob