[SOLVED] Custom Cursor Problem

Im sure this problem has happen and people have asked for a solution. I am kinda new to JME yet Im some what comprehensive to JAVA. The problem with my code is that the Picture or ImageIcon is dragging along duplicates across the game window. I tired to put a wait a little while to detach and attach yet the if statement were confusing.

1st I call on Picture

Picture icon;

than I initialize a method programming the Picture.

private static void wait_time() {
	    try {
	      TimeUnit.SECONDS.sleep(3);
	    } catch (InterruptedException e) {
	      e.printStackTrace();
	    }
	  }

private Picture icon(Node attachment_node, float x_loc, float y_loc) {
                        icon = new Picture("icon");
			icon.setImage(getAssetManager(), "/Textures/ui/crosshair.png", true);
			icon.setWidth(8f); icon.setHeight(8f);
			icon.setPosition(x_loc, y_loc);

                       attachment_node.attachChild(icon);
/*** if(attachment_node.hasChild(icon)){
          attachment_node.detachChild(icon);  wait_time(); 
          attachment_node.attachChild(icon);
}
		
		return icon;
}

Than I override “simpleUpdate()”

@Override
	public void simpleUpdate(float tpf) {
		Vector2f pos = getInputManager().getCursorPosition();
		icon(guiNode, pos.x, pos.y);
	}

Like a string of continues calls adding to the NODE. I been stumbling around for hours tring to figure this out. Much help would be appreciated

Every time you call icon() you create a new picture and put it in the scene. Why are you doing that instead of just creating the icon once and moving it?

Are you getting confused about how a scene graph works versus a drawing API like Graphics2D?

Welcome to the forums!

Yah I realized that the amount of objects kept increasing… So do I initialize one Picture function than setPosition.

where would I update the position of the mouse…

Right now, every time update is called (60 FPS or whatever), you create a whole new picture, set it’s location, and attach it to the scene.

Instead, create the picture once… then set it’s location the same place you already are.

I’m not sure what’s tricky about this so I can’t explain it any better than that.

Figured it out… thanks… I been losing sleep and my brains hurts…

Picture icon; Vector2f pos;
private Picture icon(Node attachment_node, float x_loc, float y_loc){
icon = new Picture("icon");
			icon.setImage(getAssetManager(), "/Textures/ui/crosshair.png", true);
			icon.setWidth(36f); icon.setHeight(36f);
			icon.setPosition(x_loc, y_loc);

attachment_node.attachChild(icon);
return icon;
}
@Override
	public void simpleInitApp() {
icon(guiNode, -1f, -1f);
}

@Override
	public void simpleUpdate(float tpf) {
		pos = getInputManager().getCursorPosition();
		icon.setPosition(pos.x, pos.y);
		
	}

Note that JME also has custom cursor support. I didn’t want to mention it earlier because I thought it would be illuminating for you to figure out your current approach.