how to keep moving an object

+2 Josaped Moreno · October 19, 2014
hello guys, newbie here,

I would like to ask how to move an object or picture in a single click of up button on my keyboard?

this is my code:

I'm using java slick.

boxY is the Y location of the image and the deltaY is supposed to be adding increment to Y direction after a single click of the arrow key without holding pressed it. thanks


if(input.isKeyDown(Input.KEY_UP)){
    boxY -= deltaY;
    if(boxY <= 0){
    boxY = 0;
    }
}

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Alex Sweps · October 21, 2014
Or start your deltaY at 0. Then use your up arrow to call a method to set the speed. Thats probably cleaner. 


float deltaY = 0;

private void setDeltaY(float speed){
deltaY = speed;
}


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
if(input.isKeyPressed(Input.KEY_UP)){
setDeltaY(0.2f)
if(boxY <= 0){
boxY = 0;
}
}else{
boxY += deltaY;
}
}


The key press will trigger the method and the else statement will start the boxes rolling once the lift your finger. This has the benifit of being able to use that method throughout your program to adjust the boxes speed aswell.
0 Josaped Moreno · October 25, 2014
wow it worked. Thank you so much!
0 Alex Sweps · October 19, 2014
Try this. Implement your KeyListener and add the unimplemented methods. 



import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Main implements KeyListener{

public static void main(String[] args) {


}

@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
boxY -= deltaY;
break;
case KeyEvent.VK_DOWN:
boxY += deltaY;
break;
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
}
0 Josaped Moreno · October 19, 2014
Thank you for your reply but I've tried your suggestion, imported the necessary stuff but the keyboard isn't responding to the keyevent. I think slick has a different way of handling keyboard events.

here's my update method.


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();

if(input.isKeyDown(Input.KEY_UP)){
boxY -= deltaY;
if(boxY <= 0){
boxY = 0;
}
}

if(input.isKeyDown(Input.KEY_RIGHT)){
boxX += deltaX;
if(boxX + width >= gc.getWidth() - 1){
boxX = gc.getWidth() - width;
}
}
}

still when I pressed down the arrow key and holding it, it's keeps adding increment to y position, what my target is, i want a single click of the arrow and after I release it, it will keep adding increment as I've declared deltaY = 0.2f.
thank you.
0 Alex Sweps · October 19, 2014
Adding an else statement will add the movement continuously after the key has been lifted. Something like:


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();

if(input.isKeyDown(Input.KEY_UP)){
boxY -= deltaY;
if(boxY <= 0){
boxY = 0;
}
}else{
                     deltaY += 0.2f;
               }

if(input.isKeyDown(Input.KEY_RIGHT)){
boxX += deltaX;
if(boxX + width >= gc.getWidth() - 1){
boxX = gc.getWidth() - width;
}
}else{
                    deltaX += 0.2f;
               }
}

For the single click try using the method isKeyPressed instead of isKeyDown
0 Josaped Moreno · October 26, 2014
I have another question if you don't mind.

could you tell me what this code means?

code:

for(int z = dots; z > 0; z--){
  x[z] = y[(z - 1)];
  y[z] = y[(z - 1)];
}


This is the declations:


private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RANDOM_POS = 29;
private final int DELAY = 140;

private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];

private int dots;

Thanks.
0 Alex Sweps · October 26, 2014
Answering questions hungover is so much harder than I thought haha but il do my best.

Ok so your for loop is basically creating an X and a Y array with the indexes going backwards. Each iteration of the loop is adding the value of Y to x untill x is filled.

Hmmm how do I explain this...

ALL_DOTS = 900;
private final int x[] = new int[ALL_DOTS]    //This is creating an integer array that will hold 900 items as ALL_DOTS is set to 900. Exactly same thing for y[]


First iteration is doing this:
x[900] = y[899]; //At position x[900] we want to store the value that is in y[899]

The next iteration through the loop subtracts 1 from z. So now it would be doing this:
x[899] = y[898]; //At position x[899] we want to store the value that is in y[898]

Once all of the indexes in the x array have been filled stop the loop. 

Add a System.out.println(x[z])  at the bottom of the for loop and look at what it prints out. You should see all the numbers counting backwards.


Yeah maybe someone can explain it better than me.
0 Josaped Moreno · October 26, 2014
haha I'm still confused:'( but thank you, I will hardly try to understand it, probably read your answer many times haha!:D:D:D
0 Alex Sweps · October 26, 2014
Yeha just throw in some System.out.println  throughout the loop so you can see what its doing more closely. Change some values around and see what happens.
0 Josaped Moreno · October 20, 2014
Thank you again but until this time, still not working, tried your suggestion:


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();

if(input.isKeyPressed(Input.KEY_UP)){
boxY -= deltaY;
if(boxY <= 0){
boxY = 0;
}
}else{
deltaY += 0.2f;
}

if(input.isKeyPressed(Input.KEY_RIGHT)){
boxX += deltaX;
if(boxX + width >= gc.getWidth() - 1){
boxX = gc.getWidth() - width;
}
}else{
deltaX += 0.2f;
}
}

if I pressed down the up arrow, it will move a 100 pixel up from it's starting position and stops there, it's like blinking from the starting position to 100 pixel away.

if I use these code:


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();

boxX += deltaX;
}

the image will keep moving right when I called the class. But I'm having trouble of how can I put this when I hit the arrow key button, I don't want it to keep moving right automatically
so, any suggestions? Thanks again for your patience.
  • 1
  • 2

Java / Android Development

107,207 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator