Jmonkey CookBook

Hello monkeys,
I just bought the book jmonkey CookBook, there is also the code but it does not work. Who can I contact? inappropriate copy of the code that does not work here?

however, someone can give me some help?

You might wanna post the problem. We can help here.

thanks a lot.

Grave: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ClassCastException: de.lessvoid.nifty.screen.DefaultScreenController cannot be cast to chapter06.controller.GameScreenController
	at chapter06.TestMinimap.simpleUpdate(TestMinimap.java:64)
	at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
	at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
	at java.lang.Thread.run(Thread.java:744)

GameScreenController:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter06.controller;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetNotFoundException;
import com.jme3.scene.Spatial;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.ImageRenderer;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.SizeValue;
import chapter06.util.MinimapUtil;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author reden
 */
public class GameScreenController extends NiftyController {

    private Element playerIcon;
    
    @Override
    public void bind(Nifty nifty, Screen screen) {
        super.bind(nifty, screen);

        playerIcon = screen.findElementByName("playerIcon");
    }
    
    public void createMinimap(final Spatial scene) {
        try {
            MinimapUtil.createMiniMap((SimpleApplication) app, scene, 128, 128);

//            app.enqueue(new Callable<Object>() {
//                public Object call() throws Exception {
                    try {
                        NiftyImage image = nifty.createImage(scene.getName() + "_mini.png", true);
                        screen.findElementByName("minimap").getRenderer(ImageRenderer.class).setImage(image);
                    } catch (AssetNotFoundException ex) {
                        ex.printStackTrace();
                    }
//                    return null;
//                }
//            });
        } catch (Exception ex) {
            
            Logger.getLogger(GameScreenController.class.getName()).log(Level.SEVERE, null, ex);
            
        }
       
    }
    
    public void updatePlayerPosition(int x, int y){
        if(playerIcon != null){
            playerIcon.setConstraintX(new SizeValue(x+"px"));
            playerIcon.setConstraintY(new SizeValue(y+"px"));
            screen.findElementByName("minimap").layoutElements();
        }
        
    }
}

NiftyController:

public abstract class NiftyController implements ScreenController{

    protected Nifty nifty;
    protected Screen screen;
    protected static Application app;
    private boolean optionsMenuVisible;
    
    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        this.screen = screen;
        
        /**
         * Console
         */
        initConsole();
        /**
         * /Console
         */
    }

    public void onStartScreen() {
    }

    public void onEndScreen() {
    }
    
    public static void setApplication(Application app){
        NiftyController.app = app;
    }
    
    public void toggleOptionsMenu(){
        if(optionsMenuVisible){
            nifty.getCurrentScreen().findElementByName("options").hide();
            nifty.getCurrentScreen().findElementByName("layer0").setFocus();
            optionsMenuVisible = false;
        } else {
            nifty.getCurrentScreen().findElementByName("options").show();
            nifty.getCurrentScreen().findElementByName("optionsLayer").setFocus();
            optionsMenuVisible = true;
        }
    }
    
    public void startGame(){
        
    }
    
    public void showSettings(){
        
    }
    
    public void quit(){
        app.stop();
    }
    
    /**
     * Console
     */
    private Console console;
    
    private void initConsole(){
        console = getConsole();
        ConsoleCommands consoleCommands = new ConsoleCommands(nifty, console);
        HideCommand c = new HideCommand();
        c.setController(this);
        consoleCommands.registerCommand("/hide", c);
        consoleCommands.registerCommand("/h", c);
        consoleCommands.enableCommandCompletion(true);
    }
    
    @NiftyEventSubscriber(id="console")
    public void onConsoleCommand(final String id, final ConsoleExecuteCommandEvent command) {
        String message = command.getCommandLine();
        if(!message.isEmpty()){
            if(!message.startsWith("/")){
                // send chat message
            }
        }
    }
    
    public void toggleConsole(){
        Console console = getConsole();
        if(console.getElement().isVisible()){
            console.getElement().hide();
        } else {
            console.getElement().show();
        }
    }
    
    public void outputToConsole(String message){
        console.output(message);
        
    }
    
    private Console getConsole(){
        if(console == null){
            console = nifty.getScreen("main").findNiftyControl("console", Console.class);
        }
        return console;
    }
   
    /**
     * Console
     */
}

Can you please also post the TestMinimap?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter06;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.FastMath;
import com.jme3.scene.Node;
import de.lessvoid.nifty.tools.Color;
import chapter06.controller.DialogScreenController;
import chapter06.controller.GameScreenController;
import chapter06.controller.NiftyController;
import chapter06.state.NiftyAppState;

/**
 *
 * @author reden
 */
public class TestMinimap extends SimpleApplication{

    public static void main(String[] args){
        TestMinimap test = new TestMinimap();
        test.start();
    }
    
    private boolean minimapRendered;
    private boolean gameScreen;
    NiftyAppState appState;
    Node scene;
    @Override
    public void simpleInitApp() {
        appState = new NiftyAppState();
        stateManager.attach(appState);
        
        scene = (Node) assetManager.loadModel("Scenes/TestScene.j3o");
        rootNode.attachChild(scene);
        
        flyCam.setEnabled(false);
    }

    private float playerLocationX;
    private float playerLocationY;
    private float time;
    @Override
    public void simpleUpdate(float tpf) {
        super.simpleUpdate(tpf); //To change body of generated methods, choose Tools | Templates.
        
        if(!gameScreen){
            appState.getNifty().gotoScreen("gameScreen");
            gameScreen = true;
        }else if(!minimapRendered ){
            ((GameScreenController)appState.getNifty().getScreen("gameScreen").getScreenController()).createMinimap(scene);
            
            minimapRendered = true;
        }
        
        playerLocationX = FastMath.sin(time);
        playerLocationY = FastMath.cos(time);
        time = (time + tpf) % FastMath.TWO_PI;
        
        ((GameScreenController)appState.getNifty().getScreen("gameScreen").getScreenController()).updatePlayerPosition((int) (128 + playerLocationX * 30f), (int)(128 + playerLocationY * 30f));
    }
}

Looks like nifty instantiated a default kontroller instead of your class. Maybe the nifty XML is missing some controller attribut.

@jmaasing said: Looks like nifty instantiated a default kontroller instead of your class. Maybe the nifty XML is missing some controller attribut.
Is a sample code. I would not know where to put the hand .. any suggestions?

Bought the book myself. The sample code is broken in so many ways that the book is basically useless.

@GluckOs said: Hello monkeys, I just bought the book jmonkey CookBook, there is also the code but it does not work. Who can I contact? inappropriate copy of the code that does not work here?

however, someone can give me some help?

Hi.

I’m sorry you’re having trouble with this example. Still, thanks for posting about it so that it can be resolved.
Unfortunately, I don’t have access to the latest code here (away over christmas). However, I have a hunch.
One of the last things that were done before release was to change the format of the package naming to chapter01 02 03 etc.
Since refactoring doesn’t work on xmls that was done by hand and there is a risk one or two files were missed.

Long story short:
Check and see if gameScreen.xml has the following line:


<screen id="gameScreen" controller="gui.controller.GameScreenController">

“gui.controller.GameScreenController” should in that case be replaced with “chapter06.controller.GameScreenController”

If that is the error, I hope it’s the only file where the problem occurs. But I’ll make sure to check it when I get home and update the example code base in that case.

I hope it helps and that you still find enjoyment in the book!

@SteveDay
If you’re also having problems, maybe you can share where the errors are so they can be fixed?

There’s always a risk with writing examples for a changing code base that the example code will eventually be obsolete. Not to say that there might not have been issues with the examples, but I know that they worked at the time of writing.

1 Like
@rickard said: Hi.

I’m sorry you’re having trouble with this example. Still, thanks for posting about it so that it can be resolved.
Unfortunately, I don’t have access to the latest code here (away over christmas). However, I have a hunch.
One of the last things that were done before release was to change the format of the package naming to chapter01 02 03 etc.
Since refactoring doesn’t work on xmls that was done by hand and there is a risk one or two files were missed.

Long story short:
Check and see if gameScreen.xml has the following line:


<screen id="gameScreen" controller="gui.controller.GameScreenController">

“gui.controller.GameScreenController” should in that case be replaced with “chapter06.controller.GameScreenController”

If that is the error, I hope it’s the only file where the problem occurs. But I’ll make sure to check it when I get home and update the example code base in that case.

I hope it helps and that you still find enjoyment in the book!

@SteveDay
If you’re also having problems, maybe you can share where the errors are so they can be fixed?

There’s always a risk with writing examples for a changing code base that the example code will eventually be obsolete. Not to say that there might not have been issues with the examples, but I know that they worked at the time of writing.

Example work!

There are also other errors, it would be nice to have all the working examples. It would be very nice if you can try the examples and update the code.

Congratulations on the book is proving crucial for people like me.

1 Like

The character control examples are all broken. Simply start one example. ChaseCamTest is funny - press “w” and see the character shoot right in the sky. And even after fixing the most obvious bugs there are still fundamental design flaws like character animation not working after a jump. I eventually simply gave up on it.