[Solved] Change the model

i using this example to make my game

i want change the model when i change the BaseAppState to another BaseAppState

Use the initialize method to add spatials to your scene, and the cleanup method to remove spatials. Have each appstate keep track of which models are theirs. Be sure you are using stateManager.detach() which will remove the appstate from the manager and call the cleanup method. It actually calls setEnabled(false) before it removes the appstate so if you have any pause functionality in the onDisabled method be sure to close that out in cleanup as well.

this is my code

package mygame;

import com.jme3.app.Application;
import com.jme3.app.state.BaseAppState;
import com.jme3.math.*;
import com.jme3.scene.*;

import com.simsilica.lemur.*;
import com.simsilica.lemur.component.SpringGridLayout;
import com.simsilica.lemur.style.ElementId;
import static mygame.InGameMenuState.log;

public class CharacterSelector extends BaseAppState {

    private Container loginPanel;
    private Container grupo1;
    private Container grupo2;
    private Container grupo3;
    private Container grupo4;
    private Container grupo5;
    private Container grupo6;
    private Container grupo7;
    private Container grupo8;

    private String charact0 = null;
    private String charact1 = null;
    private String charact2 = null;
    private String charact3 = null;
    private String charact4 = null;
    private String charact5 = null;
    private String charact6 = null;
    private String charact7 = null;

    public CharacterSelector() {
    }

    protected void join() {

        String name = "fsdf";
        String password = "ola";
        if (getState(ConnectionState.class).join(name, password)) {
            getStateManager().detach(this);
        }

    }

    protected void create() {
        getStateManager().attach(new CharactersClassSelector());
        getStateManager().detach(this);
        /*  Integer selection = listBox.getSelectionModel().getSelection();
        System.out.println("fdsfdfd "+""+selection); 
        // Disable ourselves
         */ setEnabled(false);

    }

    protected void cancel() {
        getState(ConnectionState.class).disconnect();
        getStateManager().detach(this);
    }

    protected void exitGame() {
        log.info("exitGame()");
        getState(OptionPanelState.class).show("Exit Game?", "Really exit the game?",
                new CallMethodAction("Yes",
                        getApplication(),
                        "stop"),
                new EmptyAction("No"),
                new EmptyAction("Cancel"));
    }

    @Override
    protected void initialize(Application app) {
        loginPanel = new Container();

        Container props = loginPanel.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.None, FillMode.Last)));
        props.setBackground(null);

        Container buttons = loginPanel.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y)));
        buttons.setBackground(null);
        buttons.setLayout(new SpringGridLayout(Axis.X, Axis.Y));
        buttons.addChild(new ActionButton(new CallMethodAction("Select Server", this, "cancel")));
        buttons.addChild(new ActionButton(new CallMethodAction("Start", this, "join")));
        buttons.addChild(new ActionButton(new CallMethodAction("Create", this, "create")));
        buttons.addChild(new ActionButton(new CallMethodAction("Delete", this, "cancel")));
        buttons.addChild(new ActionButton(new CallMethodAction("Quit", this, "exitGame")));

        float scale = 1.5f * getState(MainMenuState.class).getStandardScale();
        loginPanel.setLocalScale(scale);
        loginPanel.setBackground(null);
        Vector3f prefs = loginPanel.getPreferredSize().clone();
        prefs.x = Math.max(20, prefs.x);
        loginPanel.setPreferredSize(prefs.clone());

        // Now account for scaling
        prefs.multLocal(scale);

        int width = app.getCamera().getWidth();
        int height = app.getCamera().getHeight();

        loginPanel.setLocalTranslation(width * 0.5f - prefs.x * 0.5f, height * 0.10f - prefs.y * 0.5f, 10);

        String[] ola = {"", "", "", "", "", "", "", ""};

        for (int i = 0; i <= ola.length; i++) {
            charact0 = ola[0];
            charact1 = ola[1];
            charact2 = ola[2];
            charact3 = ola[3];
            charact4 = ola[4];
            charact5 = ola[5];
            charact6 = ola[6];
            charact7 = ola[7];

        }

        grupo1 = new Container();

        if (charact0 != "") {
            Button b = grupo1.addChild(new Button(charact0));
            b.setTextHAlignment(HAlignment.Center);
            Label g = grupo1.addChild(new Label("24"));
            g.setTextHAlignment(HAlignment.Center);

            grupo1.setPreferredSize(new Vector3f(70, 45, 0));
            grupo1.setLocalScale(scale);

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo1.setLocalTranslation(width * 0.01f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        } else {
            //grupo1.setLocalScale(scale);
            grupo1.setLocalScale(scale);
            grupo1.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo1.setLocalTranslation(width * 0.01f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }
        /*
             
       Grupo 2
       
         */
        grupo2 = new Container();
        if (charact1 != "") {
            grupo2.setPreferredSize(new Vector3f(70, 45, 0));
            grupo2.setLocalScale(scale);
            Button b = grupo2.addChild(new Button(charact1));
            Label g = grupo2.addChild(new Label("24"));
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo2.setLocalTranslation(width * 0.13f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        } else {
            //grupo1.setLocalScale(scale);
            grupo2.setLocalScale(scale);
            grupo2.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo2.setLocalTranslation(width * 0.13f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }
// Grupo 3      

        grupo3 = new Container();
        if (charact2 != "") {
            grupo3.setLocalScale(scale);
            Button b = grupo3.addChild(new Button(charact2));
            Label g = grupo3.addChild(new Label("24"));
            grupo3.setPreferredSize(new Vector3f(70, 45, 0));
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo3.setLocalTranslation(width * 0.25f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        } else {
            //grupo1.setLocalScale(scale);
            grupo3.setLocalScale(scale);
            grupo3.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo3.setLocalTranslation(width * 0.25f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }
//Grupo 4
        grupo4 = new Container();
        if (charact3 != "") {
            grupo4.setLocalScale(scale);
            Button b = grupo4.addChild(new Button(charact3));
            Label g = grupo4.addChild(new Label("24"));
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);
            grupo4.setPreferredSize(new Vector3f(70, 45, 0));
            grupo4.setLocalTranslation(width * 0.37f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        } else {
            //grupo1.setLocalScale(scale);
            grupo4.setLocalScale(scale);
            grupo4.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo4.setLocalTranslation(width * 0.37f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }
//Grupo 5   
        grupo5 = new Container();
        if (charact4 != "") {
            grupo5.setLocalScale(scale);
            Button b = grupo5.addChild(new Button(charact4));
            Label g = grupo5.addChild(new Label("24"));
            grupo5.setPreferredSize(new Vector3f(70, 45, 0));
            grupo5.setLocalTranslation(width * 0.49f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);
        } else {
            //grupo1.setLocalScale(scale);
            grupo5.setLocalScale(scale);
            grupo5.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo5.setLocalTranslation(width * 0.49f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }

        grupo6 = new Container();
        if (charact5 != "") {
            grupo6.setLocalScale(scale);
            Button b = grupo6.addChild(new Button(charact5));
            Label g = grupo6.addChild(new Label("24"));
            grupo6.setPreferredSize(new Vector3f(70, 45, 0));
            grupo6.setLocalTranslation(width * 0.61f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);
        } else {
            //grupo1.setLocalScale(scale);
            grupo6.setLocalScale(scale);
            grupo6.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo6.setLocalTranslation(width * 0.61f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }

        grupo7 = new Container();
        if (charact6 != "") {
            grupo7.setLocalScale(scale);
            Button b = grupo7.addChild(new Button(charact6));
            Label g = grupo7.addChild(new Label("24"));
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);
            grupo7.setPreferredSize(new Vector3f(70, 45, 0));
            grupo7.setLocalTranslation(width * 0.73f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        } else {
            //grupo1.setLocalScale(scale);
            grupo7.setLocalScale(scale);
            grupo7.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo7.setLocalTranslation(width * 0.73f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }
        grupo8 = new Container();
        if (charact7 != "") {
            grupo8.setLocalScale(scale);
            Button b = grupo8.addChild(new Button(charact7));
            Label g = grupo8.addChild(new Label("24"));
            b.setTextHAlignment(HAlignment.Center);
            g.setTextHAlignment(HAlignment.Center);
            grupo8.setPreferredSize(new Vector3f(70, 45, 0));
            grupo8.setLocalTranslation(width * 0.85f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        } else {
            //grupo1.setLocalScale(scale);
            grupo8.setLocalScale(scale);
            grupo8.setPreferredSize(new Vector3f(70, 45, 0));

            //Vector3f prefs2 = serverInfoPanel.getPreferredSize().mult(scale);
            grupo8.setLocalTranslation(width * 0.85f - prefs.x * 0.0f, height * 0.25f - prefs.y * 0.0f, 10);
        }

    }

    @Override
    protected void cleanup(Application app) {
    }

    @Override
    protected void onEnable() {
        Node root = ((Main) getApplication()).getGuiNode();
        root.attachChild(loginPanel);
        root.attachChild(grupo1);
        root.attachChild(grupo2);
        root.attachChild(grupo3);
        root.attachChild(grupo4);
        root.attachChild(grupo5);
        root.attachChild(grupo6);
        root.attachChild(grupo7);
        root.attachChild(grupo8);
    }

    @Override
    protected void onDisable() {
        loginPanel.removeFromParent();
        grupo1.removeFromParent();
        grupo2.removeFromParent();
        grupo3.removeFromParent();
        grupo4.removeFromParent();
        grupo5.removeFromParent();
        grupo6.removeFromParent();
        grupo7.removeFromParent();
        grupo8.removeFromParent();
    }
}

i can remove this model

Are you trying to remove the geometry or the ui elements along the bottom?

I’m not 100%, but can you confirm that your buttons are working? whenever I do buttons in lemur I use the following

Button b = new Button("Button");
b.addClickCommands(new Command(){
  @Override
    public void execute(Object source) {
      //your code here
  }
});

The geometry
I calling the geometry in main class

If you want the geometry to disappear but you’re creating it in the main class I suggest either moving it to a separate app state or make a method in your main along the lines of removeGeometry() and call that from your app state.

Did you go through the beginner tutorials on the wiki?

I begin my game from this example

Don’t tell me you’re already messing with network but didn’t do the starting tutorials.

Starting with a network example is not the best way to get started with jME3. You should first learn the basics about the scenegraph and so on. Believe me it is much more fun to start with a blue cube and the feeling that you understand every single line of your code than stating with a finished multiplayer game and knowing you can guess at most how one percent of it works. Those network examples are great for people who had mastered the beginner tutorials, created some simple games and want to go the next step. I give you the advice to start with these beginner examples. I guess everyone of the people who are helping you did it this way. And I say they are perfect to get started. Work your way through the examples and join the world of the monkeys:

https://jmonkeyengine.github.io/wiki/jme3.html#tutorials-for-beginners

Yes i messing with networking
And i understed some the code
I think the problem is becouse i load the model in main and i have to do it menu.class

What do you want to achieve? Should the geometry be removed after a button gets pressed? Do you know where it gets added to the scene?

We should maybe make a list of FPS mods and Game-Makers so we can cater to people who just want to change a few lines of code and say they made a game…

2 Likes

I finde the soluction using this code
getState(SiliconDioxideState.class).cleanup();

You are doing it completely wrong. The method you are calling here is supposed to be called by the engine itself.
Go read the information about appstates on the wiki in order to find the proper way to do this.

ok
if i use this code
getState(SiliconDioxideState.class).setEnabled(true);

This is okay, but you should read up on what AppStates are and how to use them.
https://jmonkeyengine.github.io/wiki/jme3/advanced/application_states.html#toolbar

tópico can be close the problem as been solved