我正在使用LibGDX制作RPG风格的游戏,当我的播放器与门接触并按空格键时我想更改屏幕。我到处搜索这个字面,但无法找到它。有人可以帮忙吗?
World Contact Listener Class:
public class WorldContactListener implements ContactListener {
@Override
public void beginContact(Contact contact) {
Fixture fixA= contact.getFixtureA();
Fixture fixB= contact.getFixtureB();
if (fixA.getUserData()=="head"||fixB.getUserData()=="head"){
Fixture shape= fixA.getUserData()=="head"? fixA:fixB;
Fixture object=shape==fixA? fixB:fixA;
if(object.getUserData() instanceof InteractiveTileObject){
((InteractiveTileObject)object.getUserData()).onHeadHit();
}
}
}
@Override
public void endContact(Contact contact) {
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
}
InteractiveTileObject(在box2d中生成对象的类:
public InteractiveTileObject(World world, TiledMap map, Rectangle bounds){
this.world=world;
this.map=map;
this.bounds=bounds;
BodyDef bdef= new BodyDef();
FixtureDef fdef= new FixtureDef();
PolygonShape shape= new PolygonShape();
bdef.type=BodyDef.BodyType.StaticBody;
bdef.position.set((bounds.getX()+bounds.getWidth()/2)/ (2*MamsGame.PPM), (bounds.getY()+bounds.getHeight()/2)/(2*MamsGame.PPM));
body=world.createBody(bdef);
shape.setAsBox(bounds.getWidth()/2/ (2*MamsGame.PPM),bounds.getHeight()/2/(2*MamsGame.PPM));
fdef.shape=shape;
fixture=body.createFixture(fdef);
}
public abstract void onHeadHit();
}
DoorToSchool(我希望玩家与之碰撞的对象)
public class DoorToSchool extends InteractiveTileObject{
public DoorToSchool(World world, TiledMap map, Rectangle bounds){
super(world,map,bounds);
fixture.setUserData(this);
}
@Override
public void onHeadHit() {
Gdx.app.log("DoorToSchool","Collison");
}
}
答案 0 :(得分:0)
ContactListener以这种方式工作(我忽略了事前解决,因为你不需要它们):
然后,在联系人破坏之前没有任何事情(在调用 endContact 之后)。这是你想要听太空键的时期。
调用 beginContact 时,只需将listenForSpaceKey
这样的标记设置为true即可。然后,在游戏更新循环中,仅在listenForSpaceKey
设置为true时才侦听空格键。
调用 endContact 时,将listenForSpaceKey
设置为false。这样,只有在联系人正在进行时,您才会侦听空格键。