如何在不调用构造函数的情况下从另一个 class 获取变量?
How to get a variable from another class without calling a constructor?
所以,在 LibGDX 中,我正在使用 box2d,并且我有一个 class 用于接触监听。我需要在不创建 class object 的情况下获取播放器的 body,因为它将第二次调用构造函数。静态变量不是一个选项。我应该怎么做?
这是 GameContacts class:
package com.platformer.managers;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
/**
* Created by Oliverss on 24/04/2015.
*/
public class GameContacts implements ContactListener {
private int onGround;
private int onHouse;
public static int mobCanJump;
public static int mobOnGround;
public void beginContact(Contact contact) {
Fixture fa=contact.getFixtureA();
Fixture fb=contact.getFixtureB();
if(fb.getUserData().equals("player_sensor")){
onGround++;
}
if(fa.getUserData().equals("house")&&fb.getUserData().equals("player")){
onHouse++;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor")){
mobCanJump++;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor_ground")){
mobOnGround++;
}
if(fb.getUserData().equals("player")&&fa.getUserData().equals("one_way_block")){
Vector2 vel= //here i need to get the body variable
if(vel.y>0)contact.setEnabled(false);
}
}
public void endContact(Contact contact) {
Fixture fa=contact.getFixtureA();
Fixture fb=contact.getFixtureB();
if(fb.getUserData().equals("player_sensor")){
onGround--;
}
if(fa.getUserData().equals("house")&&fb.getUserData().equals("player")){
onHouse--;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor")){
mobCanJump--;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor_ground")){
mobOnGround--;
}
}
public boolean isPlayerOnGround(){return onGround>0;}
public boolean isOnHouse(){return onHouse>0;}
public void preSolve(Contact contact, Manifold oldManifold) {}
public void postSolve(Contact contact, ContactImpulse impulse) {}
}
如果您想要参与碰撞的 object(玩家)的 body,那么您可以简单地从夹具中获取它,使用:
contact.getFixtureA().getBody();
否则,如果您有一个名为 "Player" 的自定义 class,并且您想要参与碰撞的那个 class 的 object,您可以保存参考UserData 中的玩家 object 而不是字符串 "player"。喜欢:
第一次调用构造函数时,保存对object的引用而不是"player" string:
setUserData(this);
然后在 GameContacts class 中,改用这些:
Object objectA=contact.getFixtureA().getUserData();
..
...
if(objectA instanceof Player){//instead of fb.getUserData().equals("player")
((Player) objectA)//Get object of class using casting
}
所以,在 LibGDX 中,我正在使用 box2d,并且我有一个 class 用于接触监听。我需要在不创建 class object 的情况下获取播放器的 body,因为它将第二次调用构造函数。静态变量不是一个选项。我应该怎么做?
这是 GameContacts class:
package com.platformer.managers;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
/**
* Created by Oliverss on 24/04/2015.
*/
public class GameContacts implements ContactListener {
private int onGround;
private int onHouse;
public static int mobCanJump;
public static int mobOnGround;
public void beginContact(Contact contact) {
Fixture fa=contact.getFixtureA();
Fixture fb=contact.getFixtureB();
if(fb.getUserData().equals("player_sensor")){
onGround++;
}
if(fa.getUserData().equals("house")&&fb.getUserData().equals("player")){
onHouse++;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor")){
mobCanJump++;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor_ground")){
mobOnGround++;
}
if(fb.getUserData().equals("player")&&fa.getUserData().equals("one_way_block")){
Vector2 vel= //here i need to get the body variable
if(vel.y>0)contact.setEnabled(false);
}
}
public void endContact(Contact contact) {
Fixture fa=contact.getFixtureA();
Fixture fb=contact.getFixtureB();
if(fb.getUserData().equals("player_sensor")){
onGround--;
}
if(fa.getUserData().equals("house")&&fb.getUserData().equals("player")){
onHouse--;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor")){
mobCanJump--;
}
if(fa.getUserData().equals("block") &&fb.getUserData().equals("mob_sensor_ground")){
mobOnGround--;
}
}
public boolean isPlayerOnGround(){return onGround>0;}
public boolean isOnHouse(){return onHouse>0;}
public void preSolve(Contact contact, Manifold oldManifold) {}
public void postSolve(Contact contact, ContactImpulse impulse) {}
}
如果您想要参与碰撞的 object(玩家)的 body,那么您可以简单地从夹具中获取它,使用:
contact.getFixtureA().getBody();
否则,如果您有一个名为 "Player" 的自定义 class,并且您想要参与碰撞的那个 class 的 object,您可以保存参考UserData 中的玩家 object 而不是字符串 "player"。喜欢:
第一次调用构造函数时,保存对object的引用而不是"player" string:
setUserData(this);
然后在 GameContacts class 中,改用这些:
Object objectA=contact.getFixtureA().getUserData();
..
...
if(objectA instanceof Player){//instead of fb.getUserData().equals("player")
((Player) objectA)//Get object of class using casting
}