这是我第一次使用LibGDX制作游戏时,我试图在两件事发生碰撞时增加游戏中的分数,但它根本没有注册碰撞。
这就是我在 Player 类中所拥有的:
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.summative.MyGdxGame;
import com.mygdx.summative.Screens.PlayScreen;
public class Player extends Sprite {
public World world;
public Body b2dbody;
private TextureRegion peteStand;
public Player(World world, PlayScreen screen ){
super(screen.getAtlas().findRegion("goomba"));
this.world = world;
definePlayer();
peteStand = new TextureRegion(getTexture(), 227,1,16,16);
setBounds(227,1,16/ MyGdxGame.PPM,16/MyGdxGame.PPM);
setRegion(peteStand);
}
public void update (float dt ){
setPosition(b2dbody.getPosition().x - getWidth() /2, b2dbody.getPosition().y - getHeight()/2);
}
public void definePlayer (){
BodyDef bdef = new BodyDef();
bdef.position.set(32 / MyGdxGame.PPM,32 /MyGdxGame.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2dbody = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(6/MyGdxGame.PPM);
fdef.shape = shape;
b2dbody.createFixture(fdef);
EdgeShape body = new EdgeShape(); // used for collision dectection
body.set(new Vector2(-1/MyGdxGame.PPM,7/MyGdxGame.PPM), new Vector2(1/MyGdxGame.PPM,7/MyGdxGame.PPM)); // sets up borders for collision on player
fdef.shape = body ;
fdef.isSensor = true;
b2dbody.createFixture(fdef).setUserData("body"); // used to identify collisions to see if one if the player
}
}
当云端点击时,我想提高分数,所以我在 Object 类中写了这个:
public class Cloud extends InteractiveTileObject {
public Cloud(World world, TiledMap map, Rectangle bounds) {
super(world, map, bounds);
fixture.setUserData(this);
}
@Override
public void onBodyHit() {
Gdx.app.log("Cloud" , "Collision");
Hud.addScore(200);
}
}
我错过了什么?这是 ContactListener 类中的联系方法:
@Override
public void beginContact(Contact contact) { // when to things start to collide
// figures out with fixture is which in a collision of two objects
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();
// checks of one is players body
if (fixA.getUserData() == "body" || fixB.getUserData() == "body"){
Fixture body = fixA.getUserData() == "body" ? fixA : fixB;
Fixture object = body == fixA ? fixB : fixA;
// checks if the object the player collided with is a interactive object
if (object.getUserData() instanceof InteractiveTileObject) {
((InteractiveTileObject) object.getUserData() ).onBodyHit();
}
}
}
答案 0 :(得分:1)
通常,final int index = Integer.parseInt(intents.getStringExtra(MainActivity.KEY));
是错误的,因为您正在比较引用的相等性。要检查intent.putExtra(KEY, position); // put int value to intent
getIntent.getIntExtra(KEY, -1); // get int value where -1 is the default value
在内容方面是否与另一个相等,即按字典顺序排列,您应该fixA.getUserData() == "body"
或更安全String
。
另一方面,fixA.getUserData().equals("body")
标识符可能更合适,从技术上讲,物理世界中的所有内容都是正文。