gpt4 book ai didi

iphone - 我想在检测到触摸点时移动 Sprite 体

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:04 26 4
gpt4 key购买 nike

我想在触摸屏幕时移动 sprite body ,但它不能发生......

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

请帮帮我...谢谢

最佳答案

请尝试以下适合您的代码。

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

}
}

}
}

带有 body 的 Sprite 移动到触摸结束的位置。

关于iphone - 我想在检测到触摸点时移动 Sprite 体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11373385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com