This discussion is archived
10 Replies Latest reply: Jan 3, 2011 1:07 AM by gimbal2 RSS

Sprite Animation

812092 Newbie
Currently Being Moderated
For a computer science course I am trying to animate an alien. It needs to move across the screen, then move down one line and back across the screen the other way. I can get the alien to go all the way across the screen, but it continues to go off the screen. I asked our teaching assistant for help, and he told me that we should not need to use a loop, but I am confused on how to make my alien move down a line. Any ideas? thanks for your help in advance!
  • 1. Re: Sprite Animation
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Test the location of the sprite with an appropriate <tt>if</tt> condition to determine the next movement (right/left or down).

    db

    edit
    To get better help sooner, post a [url http://mindprod.com/jgloss/sscce.html]SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    Edited by: Darryl Burke
  • 2. Re: Sprite Animation
    812092 Newbie
    Currently Being Moderated
    So far I have
    public void move() {
              if (upperLeftX > 0 && upperLeftX <= 750) {
                   upperLeftX = upperLeftX + INITIAL_X_SPEED;
              }

    This moves the alien over, and stops it at the end of the line. What is the best way to test the Y condition and have it update after the X condition?
  • 3. Re: Sprite Animation
    812092 Newbie
    Currently Being Moderated
    I tried updating again and now it moves over, then straight down. How can I make it move back across the screen in the opposite direction?

    public void move() {
              if (upperLeftX > 0 && upperLeftX <= 750) {
                   upperLeftX = upperLeftX + INITIAL_X_SPEED;
              }
              if (upperLeftX>750){
                   upperLeftY++;
              }
              }




    And thank you for your help before!
  • 4. Re: Sprite Animation
    gimbal2 Guru
    Currently Being Moderated
    Perhaps your sprite state is too simple in its current form. You make decisions based on the X,Y location.

    In stead of using only the location on the screen, why don't you also add a variable that says what the sprite is currently doing.
    public class Sprite {
      public static final int MOVING_LEFT = 1;
      public static final int MOVING_RIGHT = 2;
      public static final int MOVING_DOWN = 3;
      public static final int MOVING_UP = 4;
    
      // when you start, the sprite moves to the right
      private int moving = MOVING_RIGHT;
    }
    Now you can have something like this:
    if(moving == MOVING_DOWN){
      upperleftY++;
    
      if(upperleftY > 500){
        moving = MOVING_LEFT;
      }
    }
    
    if(moving == MOVING_LEFT){
      ...
    }
    Does that help you?
  • 5. Re: Sprite Animation
    DarrylBurke Guru Moderator
    Currently Being Moderated
    gimbal2 wrote:
    In stead of using only the location on the screen, why don't you also add a variable that says what the sprite is currently doing.
    I like this idea, except that I think this is a very good use case for an enum, which could have a <tt>Point getNextPoint(Point currentPoint)</tt> method. Something like
    private enum Move {
      LEFT(-1, 0),
      RIGHT(1, 0),
      UP(0, -1),
      DOWN(0, 1);
    
      private final int x, y;
    
      Move(int x, int y) {
        this.x = x;
        this.y = y;
      }
    
      public Point getNextPoint(Point currentPoint) {
        return new Point(currentPoint.x + x, currentPoint.y + y);
      }
    }
    Then the Move 'state' of the sprite would be conditionally changed and the getNextPoint used for moving the sprite.

    db
  • 6. Re: Sprite Animation
    gimbal2 Guru
    Currently Being Moderated
    Darryl Burke wrote:
    gimbal2 wrote:
    In stead of using only the location on the screen, why don't you also add a variable that says what the sprite is currently doing.
    I like this idea, except that I think this is a very good use case for an enum
    Of course you are right, but don't forget that this person is in the process of learning; an enum might not have been on the menu yet.
  • 7. Re: Sprite Animation
    captfoss Pro
    Currently Being Moderated
    gimbal2 wrote:
    Of course you are right, but don't forget that this person is in the process of learning; an enum might not have been on the menu yet.
    I'd like to meet the professor that thinks you need to be able to implement sprite animations before you understand programming well enough to implement an enumeration ;-)
  • 8. Re: Sprite Animation
    gimbal2 Guru
    Currently Being Moderated
    captfoss wrote:
    gimbal2 wrote:
    Of course you are right, but don't forget that this person is in the process of learning; an enum might not have been on the menu yet.
    I'd like to meet the professor that thinks you need to be able to implement sprite animations before you understand programming well enough to implement an enumeration ;-)
    Well actually doing small games is a good way (in my opinion one of the best ways) to actually learn programming, so I'd understand that this particular situation happens ;) But you do make a valid point.
  • 9. Re: Sprite Animation
    827803 Newbie
    Currently Being Moderated
    I'm unsure whether your problem has been solved, but my solution will deal with the problem, when moving you want to have a magnitude which can be changed under conditions. You will be wanting to increment your sprite/alien's position by this magnitude every refresh to cause movement, yet you'll be able to give conditional situations. To get this to work out you'll need a returning angle and magnitude set of methods however it's a well documented technique online.

    For example your problem with it going outside the border, you can purely reverse the magnitude in the other direction (this is assuming you aren't using a keylistener since that would be extremely easy!).

    if (sprite.getY() > SCREEN_WIDTH){ magnitude = -1; }

    Sorry if I couldn't help further!
  • 10. Re: Sprite Animation
    gimbal2 Guru
    Currently Being Moderated
    BoxCat wrote:
    I'm unsure whether your problem has been solved
    The only thing you have to do to be sure is check the last date that the original poster made a comment. That is a loooong time ago in this case, so you can safely assume this thread has gone to sleep.

    As a warning: it is discouraged to unnecessarily wake up sleeping threads, creating a zombie (waking up a thread that can be considered dead) is very much frowned upon and will always trigger moderation actions (usually locking the thread, but usually you'll also get a warning and repeated offense can net you a ban).

    Just reason like this: there is little chance that when you wake up a sleeping thread that you will make any kind of a difference. Better focus on threads that are recent, because those people are guaranteed to need your help right now.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points