Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: KeY Press

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Exclamation KeY Press

    I want this to happen when i press any of the w,a,s,d buttns:
    the platform (rectangle) starts going in the specified direction until it reaches the end of the bored/it reaches another platform and then it stops.
    what happens in my game .... it freezes ... and does not react to any thing. i am forced to use the task manager to stop it !
    here is what i did on the w key(i did the same for all the other key with minor changes and non of them worked):
     if(keyCode == e.VK_W){
                	 if((y1-CP != y2 && x1 != x2)||(y1-CP != y3 && x1 != x3)|(y1-CP != y4 && x1 != x4)|(y1-CP != y5 && x1 != x5)
                			 |(y1-CP != y6 && x1 != x6)|(y1-CP != y7 && x1 != x7)|(y1-CP != y8 && x1 != x8)|(y1-CP != y9 && x1 != x9)
                			 |(y1-CP != y10 && x1 != x10)|(y1-CP != y11 && x1 != x11)|(y1-CP != y12 && x1 != x12) || (y1-CP >= 205)){
                		 while((y1-CP != y2 && x1 != x2)||(y1-CP != y3 && x1 != x3)|(y1-CP != y4 && x1 != x4)|(y1-CP != y5 && x1 != x5)
                    			 |(y1-CP != y6 && x1 != x6)|(y1-CP != y7 && x1 != x7)|(y1-CP != y8 && x1 != x8)|(y1-CP != y9 && x1 != x9)
                    			 |(y1-CP != y10 && x1 != x10)|(y1-CP != y11 && x1 != x11)|(y1-CP != y12 && x1 != x12) || (y1-CP >= 205)){
                			 y1-=CP;
                		 }
                		 if(x1==655 && y1==505){
      						//winner
      					}
                	 }
                	 else{
                		 //print message!
                	 }
     
                }
    if you have any question about my code i will gladly help you help me.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    That is very ugly code. Its impossible to read with the short, non-descriptive variable names and the hardcoded magic numbers.

    It looks like there should have been a array to hold the values like x1, x2 etc and y1, y2 etc
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    crazyboy5012 (April 10th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    i know it is very ugly. i was forced to make it that way due to lack of knowledge and lack of time.
    each x and y combination belong to a platform (ex> x1,y1 belong to platform 1), and each one of them is a parameter of location,as in platform one is located at x1,y1(defult size 90X90).
    does this help?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    How are you trying to debug the code?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    crazyboy5012 (April 10th, 2013)

  7. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    I am not really keen on all of the terms yet .. can you explain your slef?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    How are you working on understanding what the code does when it executes so you can find the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    crazyboy5012 (April 10th, 2013)

  10. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    oh simple .... i look for a proble and if i think i spot it i change it ... run it again if it does not work i repeat ....
    this is the 1st time i got no results ...

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    The technique I use is adding println() statements to print out the values of variables used by the code to see how they change. If you understand how the program is supposed to work, the display of the values of the variables that control what the program does will show you where the program is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    how will this work if i am working on a JFrame ?
    i understand if i only have a console based progrem.
    (btw i am building a turn based bored game)

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    Try it and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    crazyboy5012 (April 10th, 2013)

  15. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    ok found the problem ... for some reason even with the limitations it keeps creating an infinite loop.
    i am not sure why ... it's either a whole in the limitations or a miss in the data ...
    [EDIT]
    it's a whole in the limitations cus when i simply move it step by step it lands directly on top of the locations of the other platforms.
    tho i have no idea where i went wrong with the collision ditection ...

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    Use println() statements to print out the values of the variables that keep the code looping.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    crazyboy5012 (April 10th, 2013)

  18. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    i did an it lands on the exact x and y parameters which i specified it not to land on
    if you look back at the code you'll see that the while will only run if the next jump(y-=cp;(cp=100)) does not land on another platform (let's say x1=100 y1=100 and x2=100 y2=0).
    so i type while((y1+CP != y2 && x1!=x2)|| ...)
    is that correct or am i missing something ?

  19. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeY Press

    Sorry, no way to tell what the ... code is doing.
    With multiple conditions connected by OR (||), only one needs be true.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    crazyboy5012 (April 10th, 2013)

  21. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: KeY Press

    omg thx !!! .... fixed it XD

Similar Threads

  1. [SOLVED] Get a new key in HashMap with values of another key.
    By Purple01 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2012, 04:33 AM
  2. Random key press
    By Vergil333@gmail.com in forum Loops & Control Statements
    Replies: 6
    Last Post: March 13th, 2012, 06:24 AM
  3. [SOLVED] restricting a particular key stroke or key code
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 22nd, 2011, 11:19 AM