Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Making my ball move and bounce of the borders /making my paddle move on keyevents

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello. I got a question. I am luckily now done with creating the shapes of my objects, but I do not know how to move my ball nor my paddle.
I would like to know some of your strategies or ideas of how to make my ball bounce off the borders and the bricks, such as making my paddle move to the left when I press the A key and to the right when I press the D key.

 
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You could get some ideas here: http://fxapps.blogspot.com/2017/01/pong-game-in-javafx-using-90-lines-of.html
and here: https://stackoverflow.com/questions/29962395/how-to-write-a-keylistener-for-javafx
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello. I am wondering how a KeyEvent in Canvas works. Is it the same like in the Scene class or does it work completely different. I wanna steer my paddle with A and D, but I do not know how it works in Canvas.

 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Okay thank you!
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Did you try the code for KeyEvents in the link I posted?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Yes I did, but it does not do anything.
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

it does not do anything.


Please explain what happened and post your code so we can see what the problem is with it.

Note: I was able to use the code from the links I posted to move the paddle with key presses.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I implemented that, but I am not too sure about EventHandlers aswell. I read through multiple sources (mostly stackoverflow comments) but did not get it working. Maybe it is, because I did something wrong or missed on something out, but I do not know what is wrong

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The code in the link used the Scene not the Canvas.  Have you tried doing it that way?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I used the scene class before, but that just painted it and did not let me move anything so I switched to Canvas and the movement works with it. Only the EventHandlers are confusing me.
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

I used the scene class before, but that just painted it and did not let me move anything


Can you post your code that shows what you tried?

I was able to get get things to move with the code from the links I posted.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I only have this now

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Does it work?  What problems are there?
How is this code different from the code that was posted earlier?

How are you trying to debug it to see what the computer does when the code is executed.  Add some print statements that print messages when methods are executed and that show the values of variables as their values are changed.

Why is the code using Canvas instead of Scene for the keyboard listeners as shown in the code at the links I posted?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Again. I used Canvas, because in the pong example there was Canvas used and it worked quite good with it. But they are only Keylisteners used in the pong game. In addition to that the stack overflow post is using scene instead of canvas. That is why I do not get the difference. And lastly, it just does not work and I do not know how a print statement can help with a KeyEvent.

The problem is just, that it does not work. I can press A and D all I want and the paddle will not change its position.
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I used Scene and it worked just like in the code at the link I posted.
Can you post your version that used Scene so we can see what happens and maybe get it to work?

how a print statement can help with a KeyEvent.


The print statement would show if the handle method was called and what the value of the KeyEvent was.  The posted code doesn't show that.
 
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I set a linebreak on the Eventhandler, but that handler on the canvas is simply not invoked. So a print statement would have no effect too.

@Matthias
Norm suggested to add the handler to the scene, and that you still haven't done.
If you change your code to:

then it will work. You see a slight disadvantage, namely that it takes a little time before the keyboard-repeat takes place, resulting in a slight delay in your paddle moving
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

resulting in a slight delay in your paddle moving


The code in the Runner class shows how to handle that. The key press sets a flag used by the animation method so that the ball and the paddle move in the same time frame.
Key release clears the flag.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:I set a linebreak on the Eventhandler, but that handler on the canvas is simply not invoked. So a print statement would have no effect too.

@Matthias
Norm suggested to add the handler to the scene, and that you still haven't done.
If you change your code to:

then it will work. You see a slight disadvantage, namely that it takes a little time before the keyboard-repeat takes place, resulting in a slight delay in your paddle moving




What about my canvas though? What do I do with that now?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:I set a linebreak on the Eventhandler, but that handler on the canvas is simply not invoked. So a print statement would have no effect too.

@Matthias
Norm suggested to add the handler to the scene, and that you still haven't done.
If you change your code to:

then it will work. You see a slight disadvantage, namely that it takes a little time before the keyboard-repeat takes place, resulting in a slight delay in your paddle moving




What about my canvas though? What do I do with that now?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Wait, I have it now. Thank you all!
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I made the helpful changes now and I wanna edit a limit when paddleXPos should not be allowed to gain anymore when it reaches a certain value. I want the value to not change when it reaches 0 and when it reaches 700. If you press A, you are not allowed to go beneath 0 and if you press D, you are not allowed to get over 700. However the if statement oes not change anything. They keep going out of the scene.

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Instead of requiring an exact match with ==, use <= 0 for the left hand side and >= width for the right hand side.
Note: use a variable for the width vs a hard coded 700

Why add or subtract 0 from a value?

They keep going out of the scene.


The posted code ALWAYS changes paddleXpos's value???.
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
A simple way for the left side is:

and likewise for the right side. Take the paddleWidth into account, in that case.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
So, I got this now, but my paddle gets stuck when I reach the end of one of the Positions. When I reach 0, it gets stuck and when I reach width (700) it gets stuck. Why is that so?

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

 When I reach 0, it gets stuck and when I reach width (700) it gets stuck. Why is that so?


Add some print statements that print out the values of the variables so you can see what their values are when it gets stuck.

Why does the code change the paddle's position on both the press and the release?  

Why the hardcoded value of 10 to change the paddle's position?  Should the rate of change in the paddle's position be related to the speed of the  ball?

The posted code(See Runner.java) used flags to control when  the paddle's position changed so that  the positions of the ball and the paddles were changed at the same rate instead of requiring the OS to be responsive to the key presses and releases.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What do you mean by flags?? Do you mean the Animation by that or what?
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

What do you mean by flags?


Sorry, that must be an older term.  I meant a boolean variable that is either true or false.
See the Runner.java program to see how boolean variables were used to control the moving of the image (the animation)

From Google:

In computer science, a flag is a value that acts as a signal for a function or process. The value of the flag is used to determine the next step of a program. Flags are often binary flags, which contain a boolean value (true or false). However, not all flags are binary, meaning they can store a range of values.

 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
He is using Node aswell and I have a rectangle
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I can't help you with code I can not see.

I have taken your code and the techniques from the links I posted and have the paddle so it will move when a key is pressed.

Post your code that shows the problem.  Be sure to comment on what problems you are having.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The Pong guide uses canvas, but the stackoverflow comment uses Scene. These two intervein with each other.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That is all the code I have, right now. I put some of it into comments so it would not bother the already working code. I have the problems with creating it. Some methods that are in stackoverflow just do not interact with the pong code.

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Your code sort of works.  Now it needs to be fixed to work properly.  
The Runner.java code shows how to use flags to control what direction the image is to move based on keypresses.  
The Pong.java code shows how to move a paddle.
I suggest using the method in the Runner class for controlling the paddle's movements.  Set the flag/boolean in the listener and use its value in the run() method to control paddle movements.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
But look at what they use closely. The pong code is using Canvas and the Runner is using scenes. I tried using both of them and they messed up with each other
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Your last post almost works.  All it needs is logic in the run method that uses the value in left or right to change the value in paddleXpos.  

Add some if statements around line 118 to test the booleans and change paddleXpos.
 
You didn't tell me he was so big. Unlike this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic