仅对世界上的一个精灵施加零重力
我在 cocos2d 和 box2d 中有一个世界,它有重力。 现在,为了为每个精灵添加主体,我调用一个函数并向她发送精灵。
sprite1,必须根据重力移动,但 sprite2 必须是静态的,没有重力,直到 sprite1 击中它,然后世界力量才会影响他。
我如何仅将精灵1/身体重力设置为零,直到另一个精灵击中他?
我的问题是所有精灵对身体使用相同的功能:
- (void)addBoxBodyForSprite:(CCSprite *)sprite {
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
spriteBodyDef.userData = sprite;
spriteBody = world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
}
我想在开始时仅在精灵1上应用重力,但我确实想为精灵2创建一个身体,因为稍后它将受到世界的影响。
那么,在开始时创建 2 个物体后,如何仅阻止 sprite2 掉落?
多谢。
i have a world in cocos2d with box2d, that has a gravity.
now in order to add a body for each sprite, i am calling a function and send her the sprite.
sprite1, has to move according to gravity, but sprite2 has to be static,without gravity, just until sprite1 hit it,then the world forces should affect him.
how do i set only sprite1/body gravity to be zero 'til the other sprite hit him ?
my problem is that all sprites using the same function for the body:
- (void)addBoxBodyForSprite:(CCSprite *)sprite {
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
spriteBodyDef.userData = sprite;
spriteBody = world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
}
i want to apply gravity at the start only on sprite1, BUT i do want to create a body for sprite2 also, because later it will be affected by the world.
so, after create 2 bodies at start, how do i stop only sprite2 from falling ?
thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常最常使用 SetActive(),但根据您的需求,我认为 SetAwake() 就是您想要的。
b2Body.h
这应该就是您需要的一切。
I generally use SetActive() the most, but for your needs I think SetAwake() is what you want.
b2Body.h
That should be everything you need.
您可以尝试仅将您想要受世界影响的精灵添加到空间中,然后再添加您想要受世界影响的精灵。
因此,在这种情况下,将 Sprite1 添加到您的空间,但不添加 Sprite2,稍后您可以添加 Sprite2,这样它就会受到空间的影响。
You can try adding only the sprite you want to be affected by the world to the space, and later on add the one you want to be affected by the world.
So in this case add Sprite1 to your space but not Sprite2, later on you can add Sprite2 so it is affected by the space.