Hi Everyone,
I am attempting to flip a sprite on a key press. After some googling many people suggested to use setLocalScale (-1f,1f,0) this would effectively mirror the sprite. However for me on JDK 3.2.1 this does not work. My sprite just disappears. If I use setLocalScale(-1f,-1f,0) the sprite appears upside down and facing the correct direction.
Below is a quick code example of my issue. a sprite named “Player” will need to be in the textures folder. If anyone has suggestions on how to mirror/flip the sprite on key press i am open to ideas/what i am doing wrong
Thanks for looking
Spatial player;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp()
{
//setup camera for 2D games
cam.setParallelProjection(true);
cam.setLocation(new Vector3f(0,0,0.5f));
getFlyByCamera().setEnabled(false);
player = getSpatial("Player");
player.setUserData("alive",true);
player.move(settings.getWidth()/2, settings.getHeight()/2,0);
//The Below Works
player.setLocalScale(-1f,-1f,0);
//The Below Does Not Work
//player.setLocalScale(-1f,1f,0);
guiNode.attachChild(player);
}
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private Spatial getSpatial(String name)
{
Node node = new Node(name);
//Load the sprites
Picture pic = new Picture(name);
Texture2D tex = (Texture2D) assetManager.loadTexture("Textures/"+name+".png");
pic.setTexture(assetManager, tex, true);
//adjust picture
float width = tex.getImage().getWidth();
float height = tex.getImage().getHeight();
pic.setWidth(width);
pic.setHeight(height);
pic.move(-width/2f, -height/2f,0);
//add a material to the picture
Material picMat = new Material(assetManager, "Common/MatDefs/Gui/Gui.j3md");
picMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.AlphaAdditive);
node.setMaterial(picMat);
//set the radius of the spatial
//(using width only as a simple approximation)
node.setUserData("radius", width/2);
//attach the picture to the node and return it
node.attachChild(pic);
return node;
}
}
`