2012-10-08 44 views
0

嗨拉吉感谢您的快速反应,实际上我的问题是我必须将精灵移动到触摸点,并且我只想沿着x轴移动该精灵,为此我用下面的代码如何将精灵移动到触摸点

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    for(UITouch *touch in touches) { 

    location = [touch locationInView: [touch view]]; 

    location = [[CCDirector sharedDirector] convertToGL: location]; 

    for(b2Body *b=world->GetBodyList();b;b=b->GetNext()){ 

    if(b->GetUserData()!=NULL) {` 

    CCSprite *myActor =(CCSprite *)b->GetUserData(); 

    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 

    b->SetTransform(b2Vec2(locationWorld.x, locationWorld.y),0); 


    NSLog(@"x position of baby sprite is %@", b->GetPosition().x); 

    id action = [CCMoveTo actionWithDuration:0.4 position:CGPointMake(b->GetPosition().x, b->GetPosition().y)]; 

    [myActor runAction:action]; 

}}}} 

与此代码我移动我的精灵在以往任何时候,我想,但它并不局限于x轴......这样你们可以帮我对此

感谢提前

+0

HMM的点的坐标,我不熟悉的Box2D – stenger96

回答

0

请尝试使用此代码:

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{ 

//当我们触摸

CGPoint tocco = [touch locationInView:[touch view]]; 
CGPoint tocco2 = [[CCDirector sharedDirector] convertToGL:tocco]; 

//行动来移动精灵

id muovi = [CCMoveTo actionWithDuration:1 position:tocco2]; 

[MySprite runAction:muovi]; 

//if you want turn the sprite left o right 
if (tocco2.x < MySprite.position.x) { 

    id sx = [CCFlipX actionWithFlipX:NO]; 

    [MySprite runAction:sx]; 

}else{ 

    id dx = [CCFlipX actionWithFlipX:YES]; 

    [MySprite runAction:dx]; 

} 

}