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 に答える