Sprites for Processing
V2.1
|
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 |
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.
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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
zOrder | the higher the z value the nearer the viewer |
register | register 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
zOrder | the higher the z value the nearer the viewer |
register | register 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
zOrder | the 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
alphaFname | filename for the alpha mask file |
rows | the number of vertical tiles |
zOrder | the higher the z value the nearer the viewer |
register | register 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
cols | the number of horizontal tiles |
rows | the number of vertical tiles |
zOrder | the 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
cols | the number of horizontal tiles |
rows | the number of vertical tiles |
zOrder | the higher the z value the nearer the viewer |
register | register 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
alphaFname | filename for the alpha mask file |
cols | the number of horizontal tiles |
rows | the number of vertical tiles |
zOrder | the 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.
theApplet | the PApplet |
imageFname | filename for this sprite's image |
alphaFname | filename for the alpha mask file |
cols | the number of horizontal tiles |
rows | the number of vertical tiles |
zOrder | the higher the z value the nearer the viewer |
register | register the sprite with S4P needed to maintain z-order drawing |
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.
obj | the object to handle the event |
methodName | the 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.
spriteB |
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.
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.
x | |
y | |
biteRadius |
|
protected |
Calculate the collision image for each frame.
|
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
spriteB |
void sprites.Sprite.clearDomain | ( | ) |
Removes the current domain so the sprite's movement is no longer constrained.
|
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.
handlerObj | the object to handle the event |
methodName | the method to execute in the object handler class |
parameters | the parameter classes. |
void sprites.Sprite.draw | ( | ) |
Draw this sprite
|
protected |
Draw the collision area that will be used for this area
|
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
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
Domain sprites.Sprite.getDomain | ( | ) |
Get the sprite's movement domain.
double sprites.Sprite.getElasticity | ( | ) |
int sprites.Sprite.getFrame | ( | ) |
Get the frame number for the current frame.
double sprites.Sprite.getFriction | ( | ) |
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.
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.
double sprites.Sprite.getMass | ( | ) |
Vector2D sprites.Sprite.getPos | ( | ) |
double sprites.Sprite.getRot | ( | ) |
Get the sprite's rotation angle
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
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
boolean sprites.Sprite.isBeingDragged | ( | ) |
boolean sprites.Sprite.isDead | ( | ) |
See if the sprite is dead
boolean sprites.Sprite.isDraggable | ( | ) |
Can the shape be dragged by the mouse
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.
boolean sprites.Sprite.isOver | ( | int | mx, |
int | my | ||
) |
This method will test if a screen position is over a sprite.
mx | mouse x co-ordinate |
my | mouse x co-ordinate |
boolean sprites.Sprite.isVisible | ( | ) |
Is this sprite visible?
|
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.
event |
|
protected |
Used on the MOUSE_RELEASED event
x | |
y |
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.
spriteB | |
pcent |
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
spriteB |
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.
mouse_repond | true 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.
acceleration |
void sprites.Sprite.setAcceleration | ( | double | acceleration, |
double | angle | ||
) |
Set the sprite's acceleration rate and direction.
acceleration | |
angle | radians |
void sprites.Sprite.setAccX | ( | double | x | ) |
Set the sprite's x acceleration
ax |
void sprites.Sprite.setAccXY | ( | double | x, |
double | y | ||
) |
Set the sprite's x/y acceleration
x | |
y |
void sprites.Sprite.setAccY | ( | double | y | ) |
Set the sprite's y acceleration
y |
void sprites.Sprite.setCollisionRadius | ( | double | colRadius | ) |
Set the collision circle radius
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.
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.
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.
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.
domain | |
action |
void sprites.Sprite.setDraggable | ( | boolean | draggable | ) |
draggable | the draggable to set |
void sprites.Sprite.setElasticity | ( | double | elasticity | ) |
elasticity | the elasticity to set |
void sprites.Sprite.setFrame | ( | int | frameNo | ) |
Sets the frame to be displayed - will stop image animation
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.
firstFrame | start with this frame |
lastFrame | go back to firstFrame after this frame |
interval | time in seconds between frames |
nrepeats | how many times to repaet this |
void sprites.Sprite.setFrameSequence | ( | int | firstFrame, |
int | lastFrame, | ||
double | interval | ||
) |
Sets up the animation sequence.
firstFrame | first frame number in sequence |
lastFrame | last frame in sequence |
interval | time in seconds to display each frame |
void sprites.Sprite.setFrameSequence | ( | int | firstFrame, |
int | lastFrame, | ||
double | interval, | ||
int | nrepeats | ||
) |
firstFrame | first frame number in sequence |
lastFrame | last frame in sequence |
interval | time in seconds to display each frame |
nrepeats | number of times to repeat sequence |
void sprites.Sprite.setFriction | ( | double | friction | ) |
friction | the friction to set |
|
protected |
INTERNAL USE ONLY Called by pp_collision() if a pixel level collision occured
x | |
y |
void sprites.Sprite.setMass | ( | double | mass | ) |
mass | the mass to set |
void sprites.Sprite.setPos | ( | Vector2D | v | ) |
v |
void sprites.Sprite.setRot | ( | double | d | ) |
Rotate the image
d | s |
void sprites.Sprite.setScale | ( | double | scale | ) |
Set the scale the image is to be displayed at.
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.
speed |
void sprites.Sprite.setSpeed | ( | double | speed, |
double | angle | ||
) |
Set the sprites speed and direction of travel.
speed | |
angle | radians |
void sprites.Sprite.setVelX | ( | double | x | ) |
Set the sprite's x velocity
x |
void sprites.Sprite.setVelXY | ( | double | x, |
double | y | ||
) |
Set the sprite's x/y velocity
vel.x | |
vel.y |
void sprites.Sprite.setVelY | ( | double | y | ) |
Set the sprite's y velocity
y |
void sprites.Sprite.setVisible | ( | boolean | visible | ) |
Set the sprite's visibility
visible |
void sprites.Sprite.setX | ( | double | x | ) |
Set the sprite's world x position
x |
void sprites.Sprite.setXY | ( | double | x, |
double | y | ||
) |
Set the sprite's world x/y position
x | |
y |
void sprites.Sprite.setY | ( | double | y | ) |
Set the sprite's world y position
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
zOrder | the new z order value |
void sprites.Sprite.setZorder | ( | int | zOrder, |
boolean | sort | ||
) |
Can change the z order value woth/without resorting
zOrder | the 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.
deltaTime | the time in seconds since last called |
|
protected |
Updates the image to be displayed
deltaTime |
|
protected |
Update the sprites position based on the time since last call
deltaTime |
|
protected |
The method in eventHandlerObject to execute
|
protected |
the name of the method to handle the event
|
protected |
The object to handle the event
|
staticprotected |
INTERNAL USE ONLY This holds a reference to the Sprite that currently has the focus.
|
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