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

私を助けてください...ありがとう

4

2 に答える 2

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

            }
        }

    }
}

ボディを持つスプライトは、タッチが終了した場所に移動します。

于 2012-07-07T11:20:44.913 に答える
0

self.isTouchEnabled = YES;init メソッドに追加します

于 2012-07-07T08:52:52.567 に答える