【问题标题】:How to get a variable from another class without calling a constructor?如何在不调用构造函数的情况下从另一个类获取变量?
【发布时间】:2015-05-26 16:46:25
【问题描述】:

所以,在 LibGDX 中,我使用的是 box2d,并且我有一个用于联系人监听的课程。我需要在不创建类对象的情况下获取玩家的身体,因为它会第二次调用构造函数。静态变量不是一种选择。我该怎么做?

这是 GameContacts 类:

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) {}

}

【问题讨论】:

  • 是否有理由拥有多个此类的实例?看看Singleton设计模式...
  • 如果要访问特定对象的实例变量,则需要对该对象的引用。
  • 你能应用单例模式吗? en.wikipedia.org/wiki/Singleton_pattern 基本上你只调用一次构造函数,但你可以从任何地方多次访问实例。
  • 是的,Singleton 模式已应用并且有效!

标签: java constructor static libgdx box2d


【解决方案1】:

如果您想要参与碰撞的对象(玩家)的主体,那么您可以简单地从夹具中获取它,使用:

contact.getFixtureA().getBody();

否则,如果您有一个名为“Player”的自定义类,并且您想要该类的对象参与碰撞,您可以将玩家对象的引用保存在 UserData 中,而不是字符串“player”。喜欢:

第一次调用构造函数时,保存对象的引用而不是“播放器”字符串:

setUserData(this);

然后在 GameContacts 类中,改用这些:

Object objectA=contact.getFixtureA().getUserData();
..
...
if(objectA instanceof Player){//instead of fb.getUserData().equals("player")
    ((Player) objectA)//Get object of class using casting
}

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2011-07-13
    相关资源
    最近更新 更多