0

我有一个CCSprite连接到身体的b2world. 当有人触摸它时,我想用触摸位置移动它。使用正常的精灵可以正常工作,但使用有身体的精灵 - 它没有。它得到了触摸但不移动精灵(身体跟随精灵还是相反?)我应该怎么做?相对于触摸施加力是一个问题..

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView: [touch view]];


    //detect a touch ont the button
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {

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

        CCSprite *tempSprite = (CCSprite *) b->GetUserData();
        if( tempSprite.tag==2  )
        {

            if(CGRectContainsPoint([tempSprite boundingBox], location))
            {
                tempSprite.position=location;
                NSLog(@"touched");
             }
        }

    }

}
4

1 回答 1

1

尝试使用 SetTransform 函数更改身体的位置。我认为它看起来像:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView: [touch view]];


//detect a touch ont the button
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{

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

    CCSprite *tempSprite = (CCSprite *) b->GetUserData();
    if( tempSprite.tag==2  )
    {

        if(CGRectContainsPoint([tempSprite boundingBox], location))
        {
            b->SetTransform( b2Vec2( location.x/PTM_RATIO, location.y/PTM_RATIO ), 0 ); //change position of the body
            NSLog(@"touched");
         }
    }

}

}

不要忘记,如果你想改变身体位置施加力或设置线速度,你必须使用运动学或动态身体类型。

于 2013-10-22T23:10:40.790 回答