0

嗨 Raj 感谢您的快速响应实际上我的问题是我必须将精灵移动到我触摸过的触摸点,而且我只想沿着 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 轴......所以你能帮我解决这个问题吗

提前致谢

4

1 回答 1

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];

}

}

于 2012-10-13T16:04:34.473 回答