2012-07-16 62 views
0

當我觸摸屏幕時我想移動身體,當我釋放觸摸時停止。 我使用box2d和cocos2d,我真的不知道爲什麼我的代碼不能很好地執行。 我一個使用的touchesBegan雪碧身體運動身體在屏幕上點擊時連續移動

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     ccTime dt; 
     UITouch *touch = [touches anyObject]; 
     CGPoint location = [touch locationInView: [touch view]]; 
     location=[[CCDirector sharedDirector]convertToGL:location]; 
     [self moveRight:dt]; 

    // [self doWhateverYouWantToDo]; 
    // [self doItWithATouch:touch]; 
    } 
-(void)moveRight:(ccTime)dt 
{ 
    CCSprite *ballright=(CCSprite *)ballbody->GetUserData(); 
    NSLog(@"Ball PositionX: %f",ballbody->GetPosition().x); 
    NSLog(@"Ball PositionY: %f",ballbody->GetPosition().y); 
    [ballright runAction:[CCMoveTo actionWithDuration:1 position:ccp(ballbody->GetPosition().x,ballbody->GetPosition().y+5*dt)]]; 

} 

因此,如果其錯誤比請告訴我寫的邏輯和代碼 請幫助我。

謝謝

回答

1

應用LinearImpuls或LinearVelocity移動精靈體在ccTouchBegan和ccTouchEnd中將速度應用於零以停止精靈。

+0

它的工作原理,但當我觸摸屏幕它移動後釋放它停止,但在下次點擊屏幕後它不起作用 – 2012-08-25 06:56:43

0

對於持續移動,CCMove *操作無用。在最壞的情況下,如果每幀創建一個新的CCMove *動作,則該對象將有效地停止移動,因爲在移動開始之前存在內置的1幀延遲。

使用和修改速度矢量(CGPoint)並將其與每幀的位置進行整合以移動該對象。