Hey there everyone. I am starting to program my character to use his animations that I just finished making, and I had a few very noobish questions about how to go about coding them.
package mygame;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.CameraControl.ControlDirection;
/** Sample 7 - how to load an OgreXML model and play an animation,
- using channels, a controller, and an AnimEventListener. */
public class Main extends SimpleApplication
implements AnimEventListener {
private AnimChannel channel;
private AnimControl control;
Node player;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.LightGray);
initKeys();
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
rootNode.addLight(dl);
player = (Node) assetManager.loadModel(“Models/Link/glow_blade.mesh.xml”);
player.setLocalScale(0.5f);
rootNode.attachChild(player);
control = player.getControl(AnimControl.class);
control.addListener(this);
channel = control.createChannel();
channel.setAnim(“Run”);
channel.setAnim(“Attack 1”);
channel.setAnim(“Ocarina Raise”);
channel.setAnim(“Ocarina Play”);
channel.setAnim(“Idle”);
flyCam.setEnabled(true);
}
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
if (animName.equals("Run")) {
channel.setAnim("Idle", 0.50f);
channel.setLoopMode(LoopMode.Loop);
channel.setSpeed(1f);
}
else channel.setAnim("Idle");
}
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
// unused
}
/** Custom Keybinding: Map named actions to inputs. */
private void initKeys() {
inputManager.addMapping(“Run”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addListener(actionListener, “Run”);
inputManager.addMapping(“Ocarina Raise”, new KeyTrigger(KeyInput.KEY_O));
inputManager.addListener(actionListener, “Ocarina Raise”);
inputManager.addMapping(“Attack 1”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, “Attack 1”);
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyHeld, float tpf) {
if (name.equals("Run") && !keyHeld) {
if (!channel.getAnimationName().equals("Run")) {
channel.setAnim("Run", 0.5f);
channel.setLoopMode(LoopMode.Loop);
}
if (name.equals("Ocarina Raise") && !keyHeld){
if (!channel.getAnimationName().equals("Ocarina Raise")) {
channel.setAnim("Ocarina Raise", 0.5f);
channel.setLoopMode(LoopMode.Loop);
}
if (name.equals("Attack 1") && !keyHeld){
if (!channel.getAnimationName().equals("Attack 1")) {
channel.setAnim("Attack 1", 0.5f);
channel.setLoopMode(LoopMode.DontLoop);
}
}
}
}
}
};
}
1: For all of the code examples at which I look, the actions are all done upon releasing the key. I want some of my actions (like running) to be if the key is held. That way, you don’t have to keep spamming w to make your character run. I’m sure that there is an easy way to do this, probably something around the lines of ifKeyHeld, but I’d just like to hear it from you all first.
2: As you can see, I added in the Ocarina Raise and Attack 1 animations right into my code, and assigned them to the O key and left mouse button, respectively. I think that the code is right, but it obviously isn’t because when I press O and the left mouse button in my application, nothing happens. Any ideas? I think followed the directions exactly.
Sorry that I’m a noob, this is really my first stab at making a game.
Thanks to such a great community!
-Winner