Animation Overview - News - Installation - Reference - Examples - FAQ  
 

setStep()

Description

Sets a method to execute each frame, before the Animation draws itself. The Animation class comes with a number of step methods preset, or custom methods can be used.

Using predefined step methods
Using predefined step methods is easy (see example). Included methods are:

  • ELLIPSE

    This method traces an ellipse clockwise, where the four sides of the animation-rectangle define the four corners of the ellipse.


  • SCALE

    This method scales the image from its original size to the size of the animation-rectangle, always keeping the image in the center of the rectangle. (It doesn't scale nested Animations.)


  • BOX


    This method traces the animation box clockwise.


  • BOUNCE

    This method moves from the top-left to the bottom-right, then back to the top-left.

  • NOISE

    This method bounces around the animation-rectangle in a pseudorandom fashion.

  • NONE

    This method defines no motion: the animation stays in the top-left corner..

 

Creating custom step methods
A step method is responsible for determining the location of the Animation, given the percent complete that the Animation currently is. Think of a custom step method as defining a motion equation for the Animation to follow. (see example.)

Valid prototypes for a custom step method are

  • int[] myMethod(int);
  • int[] myMethod(float);
  • float[] myMethod(int);
  • float[] myMethod(float);

where the parameter and return value is:

Required parameter
A custom step method must receive the percent complete that the Animation is at that step, either as a float (25% complete is passed as 0.25) or as an int (25% complete is passed as 25).

Required return value
A custom step method must return either int[] or float[], where the 0th index is the new x-position and the 1th index is the new y-position.

Prototype

setStep(method_name);

Parameters method_name

String representation of the method. The predefined method ELLIPSE is passed as the String "ELLIPSE".

A custom method int[] myStepMethod(float) is passed in this parameter as the String "myStepMethod".

If null is passed, the default motion (linear movement diagonally across the rectangle) is used.

Return   (none)
Example

Example using a predefined step method

 

Animation test;

void setup()
{
  size(400,100);
  test = new Animation(this, loadImage("image.gif"), 100,40, 200,40);
  test.setStep("ELLIPSE"); // sets the motion to an ellipse
  test.showRectangle();
}

void loop()
{
  background(127);
  test.draw();
}

 

Example using a custom step method

 

Animation test;

void setup()
{
  size(400,100);
  test = new Animation(this, loadImage("image.gif"), 100,40, 200,40);
  test.setStep("customStepMethod"); // use customStepMethod, defined below.
  test.showRectangle();
}

void loop()
{
  background(127);
  test.draw();
}

// traces a sine wave.
float[] customStepMethod(float percent)
{
  float return_values[] = new float[2];
  return_values[0] = test.x() + percent*test.w();
  return_values[1] = test.y() + test.h() / 2 * (1 + sin(percent*TWO_PI));
  return return_values;
}