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
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 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.
0 Alex Sweps · October 20, 2014
Hmmm im not too sure what the problem is. If you are refreshing at around 25fps and your keypressed is adding to the axis then it should be moving smoothly untill you lift your finger.  Are you able to post any more code so we can see how its all communicating?
-1 Josaped Moreno · October 20, 2014
This is my Play class:


package javagame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState{

private Animation box, Up, Down, Left, Right;
private static final String mess = "Game Over!";
private Image background, headBox;
private boolean quit = false;
private boolean gameover = false;
private int width = 25;
private int height =25;
private int backgroundX = 0;
private int backgroundY = 0;
private float boxX = 375;
private float boxY = 375;
private float deltaX = 0.2f;
private float deltaY = 0.2f;

public Play(int state){
}

public void setBoxX(float boxX) {
this.boxX = boxX;
}

public void setBoxY(float boxY) {
this.boxY = boxY;
}

public float getBoxX() {
return boxX;
}

public float getBoxY() {
return boxY;
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
background = new Image("res/background.png");
headBox = new Image("res/head.png");
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
background.draw(backgroundX, backgroundY);
headBox.draw(boxX, boxY);

}

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

if(input.isKeyPressed(Input.KEY_UP)){
boxY -= delta * .1f;
if(boxY <= 0){
boxY += delta * .1f;
}
}

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

public int getID(){
return 1;
}
}

I've tried different codes but still no luck. Thank you.
0 Alex Sweps · October 20, 2014
Not sure if you should be creating a new input object with each update. Input input = gc.getInput();
Just a tiny mistake with boxY -= delta * .1f;      Should be deltaY. 

Im not sure why that is not working though. Maybe someone else with some more experience in games programming can help because from what im seeing that looks good unless the new input object on every update is messing up your key presses. Iv only written one game and its using similar code to what you have there so we could be overlooking something simple.

I would like to know the solution when you find the problem... 
0 Josaped Moreno · October 21, 2014
This code
boxY -= deltaY;

Works fine if I hold down the arrow up key but when I release it, the object stops. Can't find the solution yet on how can I continuously move the object after Iv pressed the arrow up key. Anyway thank you so much for your help, I gues I have to search more. Will update this thread if I get the solution.
0 Alex Sweps · October 21, 2014
Ahhhhh. I thought your code litrally wasent working.... Create a method with a boolean argument. Something like:


public void setMotion(Boolean motion){
         if(motion){
                    boxY -= deltaY
         }
}



Then you can have your box stopped and when you press the up arrow include so this:

if(input.isKeyPressed(Input.KEY_UP)){
setMotion(true);
if(boxY <= 0){
boxY = 0;
}



Something like that will work but you may need to play around with it to get it perfect. 
+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.
  • 1
  • 2

Java / Android Development

124,746 followers
About

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

Links
Moderators
Bucky Roberts Administrator