Results 1 to 10 of 10
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default How to make a pseudo animation?

    I have an image that moves with arrow keys, and I want to make it appear to be moving. So, I made that image, and then another with the legs moved, to give the illusion of motion. I added it to an event
    Java Code:
    if (k == KeyEvent.VK_RIGHT) {
    				image1 = image6;
    				peterx = peterx + 10;
    				resetIt();
    				repaint();
    			}
    but it only serves to change it once. How would I make it revert to the original frame once it is converted to the second frame (image 6)? I tried a gif but to no avail?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: How to make a pseudo animation?

    You could use a boolean value set to say true. Each time the event is captured, change the value bool = ! bool. Then if bool is true, use one image, otherwise, use the other. Assuming I understand what you are trying to do. And that may not be the best way to do it. And this only works for two states of the image.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: How to make a pseudo animation?

    How would I make it revert to the original frame
    Does that imply that you need some code to execute after an elapsed time that sets the image back to the original and then calls repaint() to show it? A Timer could do that.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How to make a pseudo animation?

    Is
    Java Code:
     if(bool=true){
    					image1 = image6;
    					bool =! bool;
    				}
    				if(bool=false){
    					image6 = image1;
    					bool =! bool;
    				}
    along the lines of that?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: How to make a pseudo animation?

    Close.

    Java Code:
    bool = !bool
    currentImage = (bool)?image1 : image6;
    Theoretically, bool should alternate between T and F each time this is called. The one you want to draw is currentImage.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How to make a pseudo animation?

    Hey Jim, I tried implementing that and got this: How to make a pseudo animation?-screen-shot-2014-01-24-10.18.43-pm.png

    The code I am using is (if you press the right arrow key)
    Java Code:
    if (k == KeyEvent.VK_RIGHT) {
    				if(bool){
    					bool = !bool;
    							image1 = (bool)?image1 : image6;
    				}
    				peterx = peterx + 15;
    				resetIt();
    				repaint();
    			}
    So I am thinking it should theoretically be implemented each time the right arrow key is pressed the images should switch, but I am getting those run time errors that you see above ^

    Thanks

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: How to make a pseudo animation?

    I'm not certain what you are doing to cause those errors, but your logic is flawed.

    Java Code:
    if(bool) {
      bool = !bool;
      image1 = (bool)?image1 : image6;
    }
    The first thing you are doing wrong is changing bool inside the if block. Once bool becomes false, it will never enter the if block again. The second thing is that you are using image1 on the left side of =. eventually, image1 will get lost because both image1 and image6 will contain image6.

    So just get rid of the if block (but leave its contents there) and then change the left of the assignment to another variable. And that is the one you want to draw in your paint method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make a pseudo animation?

    How nice of you to show us only the last part of the stack trace so that we have to guess what caused the Exception ... this again show that you don't know what you're doing.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How to make a pseudo animation?

    Jim, that code was perfect for what I needed. I am finally done with the game! Turned out awesome (from my point of view) thank you for your help!

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: How to make a pseudo animation?

    Congratulations! Glad I could help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Make an animation smoother
    By Chri in forum New To Java
    Replies: 16
    Last Post: 12-18-2012, 10:50 AM
  2. Pseudo-priorityQueue datastructure
    By leeple in forum Advanced Java
    Replies: 2
    Last Post: 03-01-2011, 11:30 AM
  3. pseudo code
    By jamiem in forum New To Java
    Replies: 4
    Last Post: 12-20-2010, 05:25 PM
  4. Providing pseudo Webcam
    By H@CK3R in forum New To Java
    Replies: 21
    Last Post: 09-28-2010, 06:08 AM
  5. Make animation pause
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 06-08-2010, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •