简体   繁体   English

如何在不调用构造函数的情况下从另一个类获取变量?

[英]How to get a variable from another class without calling a constructor?

So, in LibGDX I am using box2d and I have a class for contact listening. 因此,在LibGDX中,我使用box2d,并且有一个用于侦听联系人的类。 I need to get my player's body without making a class object, because it will call the constructor the second time. 我需要在不创建类对象的情况下获取玩家的身体,因为它将第二次调用构造函数。 And a static variable is not an option. 静态变量不是一种选择。 How should I do this? 我应该怎么做?

Here is the GameContacts class: 这是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) {}

}

If you want the body of an object (player) which participates in collision then you can simply get it from the fixture, using: 如果您想要对象(玩家)的身体参与碰撞,则可以使用以下方法从固定装置中轻松获取它:

contact.getFixtureA().getBody();

Else if you have a custom class named "Player" and you want the object of that class which participates in the collision, you can save the reference of the player object in UserData instead of a string "player". 否则,如果您有一个名为“ Player”的自定义类,并且希望该类的对象参与碰撞,则可以将播放器对象的引用保存在UserData中,而不是字符串“ player”。 Like: 喜欢:

When the constructor is called for first time, save a reference of object instead of "player" string: 首次调用构造函数时,请保存对象的引用,而不是“ player”字符串:

setUserData(this);

And then in GameContacts class, use these instead: 然后在GameContacts类中,改用这些:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM