2011-09-19 56 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