Cocos2d iphone 拖动附着到实体的精灵
我有一个Cocos2d iphone 拖动附着到实体的精灵,cocos2d-iphone,box2d,Cocos2d Iphone,Box2d,我有一个CCSprite附在b2world中的body上。 当有人触摸它时,我想用触摸位置移动它。 用一个正常的雪碧可以很好的工作,但是用一个有身体的雪碧-它没有。 它得到触摸,但不移动精灵(身体跟随精灵还是相反?) 我该怎么做?相对于触摸施加力是一个问题 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoi
CCSprite
附在b2world
中的body上。
当有人触摸它时,我想用触摸位置移动它。
用一个正常的雪碧可以很好的工作,但是用一个有身体的雪碧-它没有。
它得到触摸,但不移动精灵(身体跟随精灵还是相反?)
我该怎么做?相对于触摸施加力是一个问题
- (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");
}
}
}
}
尝试使用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");
}
}
}
}
别忘了,如果你想改变身体位置,施加力或设置线速度,你必须使用运动学或动力学身体类型。鼠标关节是最好的选择。