2012-07-07 103 views
3

我想,當我觸摸屏移動精靈的身體,但它不能發生......我想移動精靈體時觸摸點檢測

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    for(b2Body *b=world->GetBodyList();b;b=b->GetNext()) 
    { 
    CGPoint location=[touch locationInView:[touch view]]; 
     location=[[CCDirector sharedDirector]convertToGL:location]; 
     b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 


     if(b->GetUserData()!=NULL) 
     { 
     CCSprite *sprite =(CCSprite *)b->GetUserData(); 

      b->SetTransform(b2Vec2(location.x, location.y), 0); 
      id action = [CCMoveTo actionWithDuration:0.4 position:CGPointMake(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO)]; 
      [sprite runAction:action]; 
     } 
    } 
} 

請幫我... 感謝

回答

1

請嘗試下面的代碼,它會爲你工作。

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    for(UITouch *touch in touches) { 
     CGPoint location = [touch locationInView: [touch view]]; 

     location = [[CCDirector sharedDirector] convertToGL: location]; 
     for(b2Body *b=world->GetBodyList();b;b=b->GetNext()) 
     { 
      if(b->GetUserData()!=NULL) 
      { 
       CCSprite *sprite =(CCSprite *)b->GetUserData(); 
       b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 
       b->SetTransform(b2Vec2(locationWorld.x, locationWorld.y), 0); 
       id action = [CCMoveTo actionWithDuration:0.4 position:CGPointMake(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO)]; 
       [sprite runAction:action]; 

      } 
     } 

    } 
} 

與身體的精靈移動到觸摸結束的位置。

0

self.isTouchEnabled = YES;你的初始化方法