Sprites for Processing  V2.1
 All Classes Functions Variables
sprites.Sprite Class Reference
Inheritance diagram for sprites.Sprite:

Classes

class  Z_Order
 

Public Member Functions

 Sprite (PApplet theApplet, String imageFname, int zOrder)
 
 Sprite (PApplet theApplet, String imageFname, int zOrder, boolean register)
 
 Sprite (PApplet theApplet, String imageFname, String alphaFname, int zOrder)
 
 Sprite (PApplet theApplet, String imageFname, String alphaFname, int zOrder, boolean register)
 
 Sprite (PApplet theApplet, String imageFname, int cols, int rows, int zOrder)
 
 Sprite (PApplet theApplet, String imageFname, int cols, int rows, int zOrder, boolean register)
 
 Sprite (PApplet theApplet, String imageFname, String alphaFname, int cols, int rows, int zOrder)
 
 Sprite (PApplet theApplet, String imageFname, String alphaFname, int cols, int rows, int zOrder, boolean register)
 
void respondToMouse (boolean mouse_respond)
 
void addEventHandler (Object obj, String methodName)
 
void restoreImages ()
 
void update (double deltaTime)
 
boolean isOnScreem ()
 
void setDomain (double left, double top, double right, double bottom, int action)
 
void setDomain (Domain domain, int action)
 
Domain getDomain ()
 
void clearDomain ()
 
void mouseEvent (MouseEvent event)
 
void draw ()
 
boolean isOver (int mx, int my)
 
boolean cc_collision (Sprite spriteB)
 
boolean oop_collision (Sprite spriteB, float pcent)
 
boolean pp_collision (Sprite spriteB)
 
boolean oo_collision (Sprite spriteB, float pcent)
 
boolean bb_collision (Sprite spriteB)
 
void bite (int biteRadius)
 
void bite (int x, int y, int biteRadius)
 
int getHitX ()
 
int getHitY ()
 
PointD2D getHitXY ()
 
void setAnimInterval (double interval)
 
void setAnimInterval (double interval, int nrepeats)
 
void setFrameSequence (int firstFrame, int lastFrame)
 
void setFrameSequence (int firstFrame, int lastFrame, double interval)
 
void setFrameSequence (int firstFrame, int lastFrame, double interval, int nrepeats)
 
void stopImageAnim ()
 
boolean isImageAnimating ()
 
void setFrame (int frameNo)
 
int getFrame ()
 
void setSpeed (double speed)
 
void setSpeed (double speed, double angle)
 
double getSpeed ()
 
void setAcceleration (double acceleration)
 
void setAcceleration (double acceleration, double angle)
 
double getAcceleration ()
 
void setDirection (double dir)
 
double getDirection ()
 
void setRot (double d)
 
double getRot ()
 
void setScale (double scale)
 
double getScale ()
 
void setXY (double x, double y)
 
void setPos (Vector2D v)
 
Vector2D getPos ()
 
void setX (double x)
 
double getX ()
 
void setY (double y)
 
double getY ()
 
double getHeight ()
 
double getWidth ()
 
void setVelXY (double x, double y)
 
void setVelX (double x)
 
double getVelX ()
 
void setVelY (double y)
 
double getVelY ()
 
void setAccXY (double x, double y)
 
void setAccX (double x)
 
double getAccX ()
 
void setAccY (double y)
 
double getAccY ()
 
void setCollisionRadius (double colRadius)
 
double getCollisionRadius ()
 
void setVisible (boolean visible)
 
double getMass ()
 
void setMass (double mass)
 
double getElasticity ()
 
void setElasticity (double elasticity)
 
double getFriction ()
 
void setFriction (double friction)
 
boolean isVisible ()
 
void setZorder (int zOrder)
 
void setZorder (int zOrder, boolean sort)
 
int getZorder ()
 
boolean isBeingDragged ()
 
boolean isDraggable ()
 
void setDraggable (boolean draggable)
 
void setDead (boolean dead)
 
boolean isDead ()
 

Public Attributes

PApplet app
 
int eventType = 0
 
Object tag = null
 
int tagNo = 0
 

Static Public Attributes

static final int HALT = 0
 
static final int REBOUND = 1
 
static int ALPHALEVEL = 20
 
- Static Public Attributes inherited from sprites.SConstants
static final int DRAG = MouseEvent.DRAG
 
static final int CLICK = MouseEvent.CLICK
 
static final int PRESS = MouseEvent.PRESS
 
static final int RELEASE = MouseEvent.RELEASE
 
static final int RUNTIME_ERROR = 0xf0000000
 
static final int MISSING = 0x01000001
 
static final int NONEXISTANT = 0x01000002
 
static final int EXCP_IN_HANDLER = 0x81000003
 

Protected Member Functions

void calcCollisionImage ()
 
void calcOpaquePixelCount ()
 
void createEventHandler (Object handlerObj, String methodName, Class[] parameters)
 
void fireEvent ()
 
void updatePosition (double deltaTime)
 
void updateImageAnimation (double deltaTime)
 
void keepInsideDomain ()
 
void drawCollisionArea ()
 
boolean mouseHasMoved (int x, int y)
 
void setHitXY (int x, int y)
 

Protected Attributes

int mdx = Integer.MAX_VALUE
 
double moffX
 
Vector2D pos = new Vector2D()
 
Vector2D vel = new Vector2D()
 
Vector2D acc = new Vector2D()
 
double rot
 
double mass = 1.0
 
double elasticity = 1.0
 
double friction = 0.0
 
double scale = 1.0f
 
double colRadius
 
boolean dead = false
 
boolean visible = true
 
Domain domain = null
 
int domainAction = REBOUND
 
double animInterval
 
double animTime
 
int animDir = 0
 
int frameCurrent = 0
 
int frameBegin = 0
 
int frameEnd = 0
 
int nbrRepeats = Integer.MAX_VALUE
 
PImage[] frames
 
PImage[] colFrames = null
 
ImageInfo info
 
double halfWidth
 
double halfHeight
 
int hit_x
 
int[] nbrPixels = null
 
Object eventHandlerObject = null
 
Method eventHandlerMethod = null
 
String eventHandlerMethodName
 
boolean respondsToMouseEvents = false
 
boolean draggable = false
 
Integer zOrder = 0
 

Static Protected Attributes

static Sprite focusIsWith
 

Detailed Description

This class represents a sprite based upon a bitmap image file (eg jpeg, gif, png)
It provides methods to set the sprite's position, velocity, acceleration, scale and rotation.
Four types of collision detection is provided
(1) Collision circles
(2) Image border overlap (boxes)
(3) Pixel level - collision based on overlapping non-transparent pixels
(4) Overlap - similar to image border but there must be a user defined percentage overlap<br>

Methods 2, 3 & 4 will not work if the image has been rotated, attempting to use these on a rotated image will cause method 1 to be used instead.
Method 3 will not work with scaled images, attempting to use this method on a scaled image will cause method 2 to be used.
In method 1 the collision detection radius is calculated as (width+height)/2

Unless otherwise specified when creating the sprite it will be registered with the library so it can be updated with S4P.updateSprites(...) and drawn with S4P.drawSprites().
If you don't want the sprite to be registered then use the appropriate constructor.

Author
Peter Lager

Constructor & Destructor Documentation

sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
int  zOrder 
)

Create a sprite based on an image file.
You can specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
zOrderthe higher the z value the nearer the viewer
registerregister the sprite with S4P needed to maintain z-order drawing
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
int  zOrder,
boolean  register 
)

Create a sprite based on an image file.
You can specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
zOrderthe higher the z value the nearer the viewer
registerregister the sprite with S4P needed to maintain z-order drawing
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
String  alphaFname,
int  zOrder 
)

Create a sprite based on an image file and an alphaMask file.
You can specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
zOrderthe higher the z value the nearer the viewer
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
String  alphaFname,
int  zOrder,
boolean  register 
)

Create a sprite based on an image file and an alphaMask file.
You can specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
alphaFnamefilename for the alpha mask file
rowsthe number of vertical tiles
zOrderthe higher the z value the nearer the viewer
registerregister the sprite with S4P needed to maintain z-order drawing
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
int  cols,
int  rows,
int  zOrder 
)

Create a sprite based on an image file.
The actual image can be made up of a number of tiled pictures.
For animation purposes the images should be ordered left to right, top to bottom.
You can also specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
colsthe number of horizontal tiles
rowsthe number of vertical tiles
zOrderthe higher the z value the nearer the viewer
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
int  cols,
int  rows,
int  zOrder,
boolean  register 
)

Create a sprite based on an image file.
The actual image can be made up of a number of tiled pictures.
For animation purposes the images should be ordered left to right, top to bottom. You can also specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
colsthe number of horizontal tiles
rowsthe number of vertical tiles
zOrderthe higher the z value the nearer the viewer
registerregister the sprite with S4P needed to maintain z-order drawing
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
String  alphaFname,
int  cols,
int  rows,
int  zOrder 
)

Create a sprite based on an image file and an alphaMask file.
The actual image can be made up of a number of tiled pictures.
For animation purposes the images should be ordered left to right, top to bottom.
You can also specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
alphaFnamefilename for the alpha mask file
colsthe number of horizontal tiles
rowsthe number of vertical tiles
zOrderthe higher the z value the nearer the viewer
sprites.Sprite.Sprite ( PApplet  theApplet,
String  imageFname,
String  alphaFname,
int  cols,
int  rows,
int  zOrder,
boolean  register 
)

Create a sprite based on an image file and an alphaMask file.
The actual image can be made up of a number of tiled pictures.
For animation purposes the images should be ordered left to right, top to bottom. You can also specify the order the sprites are drawn using zOrder - the higher the value the nearer the viewer.

Parameters
theAppletthe PApplet
imageFnamefilename for this sprite's image
alphaFnamefilename for the alpha mask file
colsthe number of horizontal tiles
rowsthe number of vertical tiles
zOrderthe higher the z value the nearer the viewer
registerregister the sprite with S4P needed to maintain z-order drawing

Member Function Documentation

void sprites.Sprite.addEventHandler ( Object  obj,
String  methodName 
)

Attempt to create the mouse event handler for the sprite class.
The event handler is a method that returns void and has a single parameter of type Sprite and a method name.

Parameters
objthe object to handle the event
methodNamethe method to execute in the object handler class
boolean sprites.Sprite.bb_collision ( Sprite  spriteB)

Determines whether the spriteB overlaps this sprite. It uses the position, width, height and scale to represent its boundaries.

Parameters
spriteB
Returns
true if sprite boxes overlap (collision)
void sprites.Sprite.bite ( int  biteRadius)

This method makes a circular area of pixels centered around hit_x/hit_y transparent. This works fine with sprites using per-pixel transparency.

Parameters
biteRadius
void sprites.Sprite.bite ( int  x,
int  y,
int  biteRadius 
)

This method makes a circular area of pixels centered around x/y transparent. This works fine with sprites using per-pixel transparency.

Parameters
x
y
biteRadius
void sprites.Sprite.calcCollisionImage ( )
protected

Calculate the collision image for each frame.

void sprites.Sprite.calcOpaquePixelCount ( )
protected

Calculate the number of opaque pixels - used by collision detector code.

boolean sprites.Sprite.cc_collision ( Sprite  spriteB)

See if the sprite's collision circles overlap i.e. collision

Parameters
spriteB
Returns
true if sprite collision circles overlap (collision)
void sprites.Sprite.clearDomain ( )

Removes the current domain so the sprite's movement is no longer constrained.

void sprites.Sprite.createEventHandler ( Object  handlerObj,
String  methodName,
Class[]  parameters 
)
protected

Attempt to create the default event handler for the sprite class. The default event handler is a method that returns void and has a single parameter of type sprite and a method called handleSpriteEvents.

Parameters
handlerObjthe object to handle the event
methodNamethe method to execute in the object handler class
parametersthe parameter classes.
void sprites.Sprite.draw ( )

Draw this sprite

void sprites.Sprite.drawCollisionArea ( )
protected

Draw the collision area that will be used for this area

void sprites.Sprite.fireEvent ( )
protected

Attempt to fire an event for this sprite.

The method called must have a single parameter which is the object firing the event. If the method to be called is to have different parameters then it should be overridden in the child class The method

double sprites.Sprite.getAcceleration ( )

Get the acceleration based on x and y acceleration components

Returns
the resolved acceleration
double sprites.Sprite.getAccX ( )

Get the sprite's x acceleration

double sprites.Sprite.getAccY ( )

Get the sprite's y acceleration

double sprites.Sprite.getCollisionRadius ( )

Get the collision circle radius

double sprites.Sprite.getDirection ( )

Get the current direction based on x & y velocity vectors

Returns
the angle in radians
Domain sprites.Sprite.getDomain ( )

Get the sprite's movement domain.

Returns
the movement domain
double sprites.Sprite.getElasticity ( )
Returns
the elasticity
int sprites.Sprite.getFrame ( )

Get the frame number for the current frame.

double sprites.Sprite.getFriction ( )
Returns
the friction
double sprites.Sprite.getHeight ( )

Get the scaled height

int sprites.Sprite.getHitX ( )

Get the x pixel coordinate in the image where the first pixel level collision occurred.

Returns
the hit_x
PointD2D sprites.Sprite.getHitXY ( )

Get the x/y pixel coordinates in the image where the first pixel level collision occurred.

int sprites.Sprite.getHitY ( )

Get the y pixel coordinate in the image where the first pixel level collision occurred.

Returns
the hit_y
double sprites.Sprite.getMass ( )
Returns
the mass
Vector2D sprites.Sprite.getPos ( )
Returns
double sprites.Sprite.getRot ( )

Get the sprite's rotation angle

Returns
rotation angle in radians
double sprites.Sprite.getScale ( )

Get the scale used for sizing the sprite.

double sprites.Sprite.getSpeed ( )

Get the speed based on x and y velocity components

Returns
the speed scalar
double sprites.Sprite.getVelX ( )

Get the sprite's x velocity

double sprites.Sprite.getVelY ( )

Get the sprite's x velocity

double sprites.Sprite.getWidth ( )

Get the scaled width

double sprites.Sprite.getX ( )

Get the sprite's world x position

double sprites.Sprite.getY ( )

Get the sprite's world y position

int sprites.Sprite.getZorder ( )

get the current zorder

Returns
the current z order value
boolean sprites.Sprite.isBeingDragged ( )
Returns
the beingDragged
boolean sprites.Sprite.isDead ( )

See if the sprite is dead

Returns
true if dead else false
boolean sprites.Sprite.isDraggable ( )

Can the shape be dragged by the mouse

Returns
true if we can drag the sprite in GUI else false
boolean sprites.Sprite.isImageAnimating ( )

Returns true if the image is currently being animated

boolean sprites.Sprite.isOnScreem ( )

Calculates if part or all of the sprite is in the visible portion of the world.

Returns
true if part or all of the sprite is on screen
boolean sprites.Sprite.isOver ( int  mx,
int  my 
)

This method will test if a screen position is over a sprite.

Parameters
mxmouse x co-ordinate
mymouse x co-ordinate
Returns
true if the position mx, my is over a sprite
boolean sprites.Sprite.isVisible ( )

Is this sprite visible?

void sprites.Sprite.keepInsideDomain ( )
protected

Constrain sprite to domain when updating.

void sprites.Sprite.mouseEvent ( MouseEvent  event)

If you have said that this sprite is to respond to the mouse then this method will be called every loop.

Parameters
event
boolean sprites.Sprite.mouseHasMoved ( int  x,
int  y 
)
protected

Used on the MOUSE_RELEASED event

Parameters
x
y
Returns
boolean sprites.Sprite.oo_collision ( Sprite  spriteB,
float  pcent 
)

Collision detection based on the percentage overlap of THIS sprite caused by spriteB.
Note:
If we have 2 sprites with different sizes then
spriteA.oo_collision(spriteB, 40);
spriteB.oo_collision(spriteA, 40);
are not equivalent. The first returns true if >=40% of spriteA is covered by spriteB, the second statement returns true if >=40% of spriteB is covered by spriteA.

Parameters
spriteB
pcent
Returns
true if we have sufficient overlap between sprites
boolean sprites.Sprite.pp_collision ( Sprite  spriteB)

See if the spites have a none transparent pixel that collide
A pixel is transparent if its alpha component < ALPHALEVEL

Parameters
spriteB
Returns
true if the sprites collide at the pixel level
void sprites.Sprite.respondToMouse ( boolean  mouse_respond)

Ignore this method call if it does not cause a change in mouse responsiveness.
If set to true it will look for a method with the format

void handleSpriteEvents(Sprite sprite) 


in your sketch to handle mouse events PRESSED, RELEASED, CLICKED and DRAGGED
Inside the handleSpriteEvents method you can test for event type:

if(sprite.eventType == Sprite.PRESSED) ...


and so on.

Parameters
mouse_repondtrue or false
void sprites.Sprite.restoreImages ( )

Restore images back to their original state. Useful if you have used the bite() method.

void sprites.Sprite.setAcceleration ( double  acceleration)

Sets the acceleration of the sprite in its current direction. If it is not moving direction is set at the image rotation angle.

Parameters
acceleration
void sprites.Sprite.setAcceleration ( double  acceleration,
double  angle 
)

Set the sprite's acceleration rate and direction.

Parameters
acceleration
angleradians
void sprites.Sprite.setAccX ( double  x)

Set the sprite's x acceleration

Parameters
ax
void sprites.Sprite.setAccXY ( double  x,
double  y 
)

Set the sprite's x/y acceleration

Parameters
x
y
void sprites.Sprite.setAccY ( double  y)

Set the sprite's y acceleration

Parameters
y
void sprites.Sprite.setCollisionRadius ( double  colRadius)

Set the collision circle radius

Parameters
colRadius
void sprites.Sprite.setDead ( boolean  dead)

When set to true this sprite will be ignored by the updateremoved from the list of sprites to be updated and displayed.

Parameters
dead
void sprites.Sprite.setDirection ( double  dir)

Changes the velocities so the sprite is travelling in the desired direction. If the sprite is stopped has no effect.

Parameters
dir
void sprites.Sprite.setDomain ( double  left,
double  top,
double  right,
double  bottom,
int  action 
)

Set the sprite's movement domain. Normally you would ensure that the sprite starts inside the domain. The last attribute defines what happens to the sprite when it reaches the domain boundary - at present there are 2 options REBOUND and HALT.
The domain boundaries are specified by the top-left and bottom-right corner world co-ordinates.

Parameters
left
top
right
bottom
action
void sprites.Sprite.setDomain ( Domain  domain,
int  action 
)

Set the sprite's movement domain. Normally you would ensure that the sprite starts inside the domain. The last attribute defines what happens to the sprite when it reaches the domain boundary - at present there are 2 options REBOUND and HALT.

Parameters
domain
action
void sprites.Sprite.setDraggable ( boolean  draggable)
Parameters
draggablethe draggable to set
void sprites.Sprite.setElasticity ( double  elasticity)
Parameters
elasticitythe elasticity to set
void sprites.Sprite.setFrame ( int  frameNo)

Sets the frame to be displayed - will stop image animation

Parameters
frameNo
void sprites.Sprite.setFrameSequence ( int  firstFrame,
int  lastFrame 
)

When there are multiple frames then this can be used to animate through some or all of the images. Animation is repeated for the number of times specified.

Parameters
firstFramestart with this frame
lastFramego back to firstFrame after this frame
intervaltime in seconds between frames
nrepeatshow many times to repaet this
void sprites.Sprite.setFrameSequence ( int  firstFrame,
int  lastFrame,
double  interval 
)

Sets up the animation sequence.

Parameters
firstFramefirst frame number in sequence
lastFramelast frame in sequence
intervaltime in seconds to display each frame
void sprites.Sprite.setFrameSequence ( int  firstFrame,
int  lastFrame,
double  interval,
int  nrepeats 
)
Parameters
firstFramefirst frame number in sequence
lastFramelast frame in sequence
intervaltime in seconds to display each frame
nrepeatsnumber of times to repeat sequence
void sprites.Sprite.setFriction ( double  friction)
Parameters
frictionthe friction to set
void sprites.Sprite.setHitXY ( int  x,
int  y 
)
protected

INTERNAL USE ONLY Called by pp_collision() if a pixel level collision occured

Parameters
x
y
void sprites.Sprite.setMass ( double  mass)
Parameters
massthe mass to set
void sprites.Sprite.setPos ( Vector2D  v)
Parameters
v
void sprites.Sprite.setRot ( double  d)

Rotate the image

Parameters
ds
void sprites.Sprite.setScale ( double  scale)

Set the scale the image is to be displayed at.

Parameters
scale
void sprites.Sprite.setSpeed ( double  speed)

Sets the speed of the sprite in its current direction. If it is not moving direction is set at the image rotation angle.

Parameters
speed
void sprites.Sprite.setSpeed ( double  speed,
double  angle 
)

Set the sprites speed and direction of travel.

Parameters
speed
angleradians
void sprites.Sprite.setVelX ( double  x)

Set the sprite's x velocity

Parameters
x
void sprites.Sprite.setVelXY ( double  x,
double  y 
)

Set the sprite's x/y velocity

Parameters
vel.x
vel.y
void sprites.Sprite.setVelY ( double  y)

Set the sprite's y velocity

Parameters
y
void sprites.Sprite.setVisible ( boolean  visible)

Set the sprite's visibility

Parameters
visible
void sprites.Sprite.setX ( double  x)

Set the sprite's world x position

Parameters
x
void sprites.Sprite.setXY ( double  x,
double  y 
)

Set the sprite's world x/y position

Parameters
x
y
void sprites.Sprite.setY ( double  y)

Set the sprite's world y position

Parameters
y
void sprites.Sprite.setZorder ( int  zOrder)

Sets the order that sprites are drawn the low number are drawn before high numbers. This method will assume that you want to sort them immediately after the

Parameters
zOrderthe new z order value
void sprites.Sprite.setZorder ( int  zOrder,
boolean  sort 
)

Can change the z order value woth/without resorting

Parameters
zOrderthe new z order value
sort
void sprites.Sprite.stopImageAnim ( )

Stop the image animation at the current frame

void sprites.Sprite.update ( double  deltaTime)

Update the positions of all the sprites.

Parameters
deltaTimethe time in seconds since last called
void sprites.Sprite.updateImageAnimation ( double  deltaTime)
protected

Updates the image to be displayed

Parameters
deltaTime
void sprites.Sprite.updatePosition ( double  deltaTime)
protected

Update the sprites position based on the time since last call

Parameters
deltaTime

Member Data Documentation

Method sprites.Sprite.eventHandlerMethod = null
protected

The method in eventHandlerObject to execute

String sprites.Sprite.eventHandlerMethodName
protected

the name of the method to handle the event

Object sprites.Sprite.eventHandlerObject = null
protected

The object to handle the event

Sprite sprites.Sprite.focusIsWith
staticprotected

INTERNAL USE ONLY This holds a reference to the Sprite that currently has the focus.

final int sprites.Sprite.HALT = 0
static

Action sprite takes at domain edges

Object sprites.Sprite.tag = null

The user can attach any object for additional sprite information

int sprites.Sprite.tagNo = 0

The user can specify an integer for additional sprite information


The documentation for this class was generated from the following file: