[solved]move the ball when the space key was pressed

public void moveBall()
{
if (x + xa < 0)
xa = game.speed;
else if (x + xa > game.getWidth() - DIAMETER)
xa = -game.speed;
else if (y + ya < 0)
ya = game.speed;
else if (y + ya > game.getHeight() - DIAMETER)
game.gameOver();
else if (collision())
{
ya = -1;
y = game.racquet.getTopY() - DIAMETER;
}
}

public void keyPressedSpace(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_SPACE)
    {
        x = x + xa;
        y = y + ya;
    }
}

I was stuck with this.
I am a super newbie.

first time to try to program game with the help of the tutorials.
and now I want to try to challenge myself and to start it when I pressed the SPACE BAR.

It is like PINBALL where you need to press the SPACE BAR first.

everytime I put the KEYLISTENER and I pressed the SPACE BAR

nothing happens.

Please help me thanks.

 if (e.getKeyCode() == [b]KeyEvent.VK_LEFT[/b])
        {
            x = x + xa;
            y = y + ya;
        }

First off welcome!

Is that even the right key? Looks like LEFT to me and not Space.

Is that slick you are using? Also you need to multiply your movement by the games delta time (time since last frame) or it won’t move visually across the screen, it will just appear at X and Y coords.

if (e.getKeyCode() == KeyEvent.VK_LEFT)
        {
            x += xa * deltaTime;
            y += ya * deltaTime;
        }

+= short hand is the same as var1 = var1 + var2;

sorry, I`d pasted the wrong code so I edited it already.

thanks for the info.

the original code was this.

public void moveBall()
    {  
        if (x + xa < 0)
            xa = game.speed;
        else if (x + xa > game.getWidth() - DIAMETER)
            xa = -game.speed;
        else if (y + ya < 0)
            ya = game.speed;
        else if (y + ya > game.getHeight() - DIAMETER)
            game.gameOver();
        else if (collision())
        {
            ya = -1;
            y = game.racquet.getTopY() - DIAMETER;            
        }
        x = x + xa;
        y = y + ya;
    }

   
where: 
    int x = 143;
    int y = 330;
    int xa = 1;
    int ya = 1;

this code will make the ball move around the frame(300,400)

With your current ‘keyPressedSpace()’ code, you are just moving the ball by xa and ya units, once and once only. If you wanted a very simple pinball ‘press space to start’ effect, then you need to initialise xa and ya to 0. On space down, you then set xa and ya to the desired value, not x and y (i.e. xa = ya = 1;, then let your moveBall() method handle the movement).

Also do you have your KeyListener set up properly? Where and when does keyPressedSpace(KeyEvent e) get called from? As you haven’t directly overridden KeyListener’s keyPressed(KeyEvent e) method in the code you have provided, you need to make sure your keyPressedSpace() method is being called when appropriate.

Thanks for the reply.

This is only a public class of my main class

All the keylisteners are there.

Is it possible to call the KeyEvent inside the moveBall()?

Learn the language before attempting to learn game development. Spend at least 2-3 weeks reading these tutorials and making a few really basic programs, then you might be ready to try again.

Thank you sir

I will go ahead and build on this, although I highly recommend the Oracle tutorials, once you have a better grasp of the language try here http://www.tutorialspoint.com/java/index.htm as well, I use it as a reference as well as the Oracle documentation/tutorials.

actually I do have knowledge on programming like C/C++, Visual Basic, Java

but not in gaming

purely console …with no clicks and mouse

so if you have any idea where can I study on how to use the keylistener and etc …that have connections with gaming programming

Please put your code in [.code.][./code.] tags (without the .'s). Just makes it neater :point:

Just lettin ya know.

Thanks guys.

I solved the problem already. ;D