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 4 of 4

Thread: While loop Problem Please Help!

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question While loop Problem Please Help!

    Ok So heres my problem in my program i have an arrow and i made the cordinets move by +1 so it appers to be moveing on the screen so, there for it needs a while loop. i want to be able to say that when the arrows cord's == a different sprit(monster) cord's the monster will loss 10 hp. but the x and y cords of the arrow never == the monsters cords because the values of the arrows cords are stuck in the loop and by the time the arrow gets off the screen and exits the loop the x and y cords dont match. so dose anyone know how to fix this? please its an isu lol and i cant figer it out :P


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: While loop Problem Please Help!

    I think you have correctly identified the problem: the animation takes place in code (a while loop, but it could be anything) that does not complete until *after* some significant event has taken place. Consequently you never get to find out about and react to that event.

    We can think about motion in two ways. It can be thought of as a continual change of position, but equally it can thought of as a number of very small steps in sequence but, in terms of code, independent of the others. The solution to the problem is to write code that moves the arrow by just one coordinate unit. That will complete very quickly and give you time to check whether a monster has been hit etc.

    Of course this code has to be called lots of times. And you don't want to call it within your code, because that will leave you with pretty much the same problem. So what you do is create a Timer with a callback that will get called from outside your program every 50 milliseconds or so. This callback is what moves the arrow's coordinates by just one coordinate unit, checks for a monster being hit (and reacts) and schedules a repaint of the screen.

    See How to Use Swing Timers in Oracle's Tutorial.
    Last edited by pbrockway2; January 18th, 2012 at 05:56 PM.

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

    XOlapse (January 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop Problem Please Help!

    thanks i figured it out with the "swing timers" im pritty new at programming and have to learn alot but i love it lol thanks for the help!!!!!!!!!!!!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: While loop Problem Please Help!

    You're welcome.

Similar Threads

  1. Do while loop problem
    By kratos75 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 27th, 2011, 06:04 AM
  2. While loop problem
    By ak120691 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:09 PM
  3. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 12:12 PM
  4. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM