Using Sprite Library

edited January 4 in Library Questions

Hi so I have figured out on how to use the sprite library but now I don't know how to scroll through the animations frequently basing what I want the character to do any ideas on how to do

import sprites.*;
import sprites.maths.*;
import sprites.utils.*;
Sprite player;
int i=0;
float pos_x, pos_y;
void setup()
{
  size(600, 600, P2D);
  player=new Sprite(this, "https://opengameart.org/sites/default/files/daxbotsheet.png", 4, 4, 2);
  player.setFrameSequence(0, 40, 20);
}

void draw()
{

  background(255);
  translate(width/2, height/2);
  translate(pos_x, pos_y);
  player.draw();
  if (key==CODED)
  {
    if (keyCode==LEFT)
    {
      pos_x-=3;
    } else if (keyCode==RIGHT)
    {
      pos_x+=3;
    } else if (keyCode==UP)
    {
      pos_y-=3;
    } else if (keyCode==DOWN)
    {
      pos_y+=3;
    }
  }
}

Answers

  • Ignore the link except the two commas starting at https: to .png

  • Answer ✓

    Some things you need to know about the Sprites library.

    It was designed to update and display a large number of sprites with minimal programming by the user. To do this the library remembers / registers every sprite created so that they can be updated and drawn as a block with single instructions.

    To do that we need a timing device. In Sprites this is done with the StopWatch class. See line 8.

    In the draw we need to get the elapsed time since the last frame (line 20) and use this to update the sprites animation (line 21). All the sprites are rendered with a the statement S4P.drawSprites(); (see line 27)

    The Sprites uses seconds rather than milliseconds so I have changed the sprite frame sequence statement to

    player.setFrameSequence(0, 3, 0.2);

    The interval time between sprite frames is 0.2 seconds. Also you had the frame sequence 0-40 when in fact there are only 16 frames in the image.

    So here is the corrected code

    import sprites.*;
    import sprites.maths.*;
    import sprites.utils.*;
    Sprite player;
    int i=0;
    float pos_x, pos_y;
    
    StopWatch sw = new StopWatch();
    
    void setup()
    {
      size(600, 600, P2D);
      player=new Sprite(this, "https:// opengameart.org/sites/default/files/daxbotsheet.png", 4, 4, 0);
      player.setFrameSequence(0, 3, 0.2);
    }
    
    void draw()
    {
      // Get the elapsed time and updatre the sprites accordingly
      float elapsedTime = (float) sw.getElapsedTime();
      S4P.updateSprites(elapsedTime);
      // Start of frame rendering
      background(255);
      translate(width/2, height/2);
      translate(pos_x, pos_y);
      // Draw all 'registered' sprites
      S4P.drawSprites();
      if (key==CODED)
      {
        if (keyCode==LEFT)
        {
          pos_x-=3;
        } else if (keyCode==RIGHT)
        {
          pos_x+=3;
        } else if (keyCode==UP)
        {
          pos_y-=3;
        } else if (keyCode==DOWN)
        {
          pos_y+=3;
        }
      }
    }
    

    Note the 'space' after the // in the link this would need to be removed.

  • About the 40 frames yea I forgot to change it because I was using it with a different sprite sheet :D sorry about that @quark Thank you are the master :D

Sign In or Register to comment.