Auto-movement Problem

Yo, here’s my situation. I’m trying to make a game where the character automatically moves forward, yet it freezes every time I run it. Here are the codes concerning the auto-movement:



[java]

float MoveRate;



private void initKeys() {

inputManager.addMapping(“StartMove”, new KeyTrigger(KeyInput.KEY_SPACE));



inputManager.addListener(actionListener, new String[]{“StartMove”});

}



private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“StartMove”) && !keyPressed){

MoveRate = 1.00f;

while (FallRate > 0) {

Vector3f v = player.getLocalTranslation();

player.setLocalTranslation(v.x, v.y - MoveRate, v.z);

}

}

}

};



[/java]



I would appreciate all help possible on the topic.

what freezes :?

Hmm… I don’t know… maybe the game itself? No, really, the game freezes totally

you don’t know… can you press “ESC” to get out? Maybe you just remove the input or something. If the game regularly “freezes” your task manager says so…

Yeah, it freezes on input of SPACEBAR. Can’t use ESC, has not responding tag

Maybe if you used fallRate < 0 instead? I guess fallRate reflects if the character is falling… Since it’s probably not falling it hangs because it’s caught in an infinite loop.



Edit: rather, <= , not <

It looks to me as if this code would indeed freeze when you hit the space bar, unless there is another thread changing the variable fallRate.

As you’re running a while loop in an event response, it will all happen in the same update frame (unless I’m missing something special happening elsewhere) and so it never gets anywhere as fallRate is not updated within the loop.

Perhaps you could say set a boolean autoMove = true in the onAction callback and then in your update loop move the player one unit (multiplied by tpf) each frame. Then when you want to stop the auto move, set the autoMove boolean to false.

Maybe you could use a Control to do this for better modularity.

No, here’s what i’m trying to do. When you press the spacebar, player starts falling. Here’s a run-down:



01 creates the MoveRate variable



04 binds SPACEBAR to StartMove

06 organizes StartMove into actionListener



09 to 15 says that if SPACEBAR/StartMove is activated, then MoveRate is 1. And while MoveRate is more than 0, then player starts falling according to the value of MoveRate. Does anyone get this or am I just not getting it myself?

@Tumaini said:
It looks to me as if this code would indeed freeze when you hit the space bar, unless there is another thread changing the variable fallRate.
As you're running a while loop in an event response, it will all happen in the same update frame (unless I'm missing something special happening elsewhere) and so it never gets anywhere as fallRate is not updated within the loop.
Perhaps you could say set a boolean autoMove = true in the onAction callback and then in your update loop move the player one unit (multiplied by tpf) each frame. Then when you want to stop the auto move, set the autoMove boolean to false.
Maybe you could use a Control to do this for better modularity.


THIS is what I needed. Thank you very much. I was trying to do something similar to autoMove in the first place, but in a different thread (which failed). I will try this one, thank you

Ok, thank you Tumaini, you fixed the main portion of the problem I think. I also found that since it’s going down at .1 unit per frame, and averages 249 frames per second… yeah. So Ima use Tumaini’s technique and do some frame limitation as well… Thanks for your input (atleast the knowledgeable input)

… Ok anyone know a simple way to limit the frames? I’m stumped, the technique I was trying isn’t limiting the fps

you can run with vsync on, by ticking the checkbox on the opening frame when you run the game

Just use tpf !! Read the tutorials…

  1. Vsync still makes it crash, 2) Why didnt i think about tpf…

Ok, here’s an update on the problem. I’m currently having the movement code in the update block and did all that. Yet it still freezes. When I change the ‘while’ in the movement block to an ‘if’, the player moves one unit when I press the button. Here’s my conclusion of the problem:



The game is defining FallStart as true, like it’s told to, but resets every frame. In turn, the while block cannot run properly because FallStart is reset every time and is set to null. When while block is changed to an if block, it works properly because the FallRate only needs to turn true once, instead of STAYING true.



So with this said, I need to find a way to keep the FallRate set to true until set back to false. Is there any proper way to do that?

Please, read the tutorials.

I did. All tutorials I find, on this site AND off this site, all say that values set within the statement will stay set, yet this one won’t

I think you misunderstood me before.

It might be easier if you post your code, but from what you’ve stated in your later posts, it seems you have a while loop in the simpleUpdate method? That’s not what I meant with my suggestion and if the condition for the loop doesn’t change within it or from another thread, it will freeze.

What I suggest is you create a boolean (let’s call it autoMove), a float (call it fallRate) and a Vector3f (speed) as instance variables.

Speed represents your character’s speed in x, y and z and you set it to what is appropriate.

In simpleInit you set up the action listener and register it with the input manager.

Within the onAction method you do the following check:

[java]if(!isPressed) {

autoMove = !autoMove;

}[/java]

This will toggle the auto movement.

Now, in your simpleUpdate method you use:

[java]if(autoMove) {

player.move(speed.mult(tpf));

}

if(fallRate > 0) {

player.move(0, -fallRate*tpf, 0);

}[/java]

Note that this is just example code that I threw together right now, so it might need some changes to work and you have to decide yourself when the fallRate should change (when your character is falling) and as this example is just a simple structure it can be made much more flexible, but it should work the way I think you might want it to.