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 2
Oldest  Newest  Rating
-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
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?
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 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 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
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

}
}
  • 1
  • 2

Java / Android Development

107,070 followers
About

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

Links
Moderators
Bucky Roberts Administrator