public abstract class AbstractStateMachineSprite
extends org.piccolo2d.PNode
This abstract sprite node utilizes a state machine to manage all its state transitions. Consider the following simple state machine in State Chart XML (SCXML) format:
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initialstate="normal"> <state id="normal"> <transition event="walk" target="walking"/> </state> <state id="walking"> <transition event="stop" target="normal"/> </state> </scxml>
Subclasses may provide state transition methods that fire an event to the underlying state machine.
public void walk() { fireStateMachineEvent("walk"); } public void stop() { fireStateMachineEvent("stop"); }
Subclasses may associate visual properties and behavior with states by providing private no-arg state methods which will be called via reflection on entry by the state machine engine.
private void normal() { walkingActivity.stop(); } private void walking() { walkingActivity.start(); }
Animations can be associated with states by implementing the
protected createAnimation
method. Create and
return an animation for the specified state id, or return
null
if no such animation exists.
protected Animation createAnimation(final String id) { Image image = loadImage(getClass(), id + ".png"); return Animations.createAnimation(image); }
Altogether, the typical implementation pattern for a subclass of this abstract sprite node looks like
class MySprite extends AbstractStateMachineSprite { // walking activity private final WalkingActivity walkingActivity = ...; // load the state machine backing all instances of this MySprite private static final SCXML STATE_MACHINE = loadStateMachine(MySprite.class, "stateMachine.xml"); MySprite() { super(); // initialize the state machine initializeStateMachine(STATE_MACHINE); // sprites have no bounds by default setWidth(14.0d); setHeight(24.0d); } protected Animation createAnimation(final String id) { // load a single PNG image for each state id Image image = loadImage(getClass(), id + ".png"); return Animations.createAnimation(image); } // methods to fire state transition events public void walk() { fireStateMachineEvent("walk"); } public void stop() { fireStateMachineEvent("stop"); } // methods that receive notification of state transitions private void normal() { walkingActivity.stop(); } private void walking() { walkingActivity.start(); } }
FILL_STRATEGY_ASPECT_COVER, FILL_STRATEGY_ASPECT_FIT, FILL_STRATEGY_EXACT_FIT, PROPERTY_BOUNDS, PROPERTY_CHILDREN, PROPERTY_CHILDREN_PICKABLE, PROPERTY_CLIENT_PROPERTIES, PROPERTY_CODE_BOUNDS, PROPERTY_CODE_CHILDREN, PROPERTY_CODE_CHILDREN_PICKABLE, PROPERTY_CODE_CLIENT_PROPERTIES, PROPERTY_CODE_FULL_BOUNDS, PROPERTY_CODE_PAINT, PROPERTY_CODE_PARENT, PROPERTY_CODE_PICKABLE, PROPERTY_CODE_TRANSFORM, PROPERTY_CODE_TRANSPARENCY, PROPERTY_CODE_VISIBLE, PROPERTY_FULL_BOUNDS, PROPERTY_PAINT, PROPERTY_PARENT, PROPERTY_PICKABLE, PROPERTY_TRANSFORM, PROPERTY_TRANSPARENCY, PROPERTY_VISIBLE, SCENE_GRAPH_DELEGATE
NO_SUCH_PAGE, PAGE_EXISTS
Modifier | Constructor and Description |
---|---|
protected |
AbstractStateMachineSprite()
Create a new abstract state machine sprite node.
|
Modifier and Type | Method and Description |
---|---|
void |
advance()
Advance this state machine sprite node one frame.
|
protected abstract Animation |
createAnimation(String id)
Create and return an animation for the specified state id, if any.
|
protected void |
fireStateMachineEvent(String eventName)
Fire a state machine event with the specified event name.
|
protected Animation |
getCurrentAnimation()
Return the current animation for this state machine sprite.
|
protected int |
getFrameSkip()
Return the number of frames to skip.
|
protected void |
initializeStateMachine(SCXML stateMachine)
Initialize the specified state machine.
|
protected static <T> BufferedImage |
loadImage(Class<T> cls,
String name)
Load the image resource with the specified name, if any.
|
protected static <T> SCXML |
loadStateMachine(Class<T> cls,
String name)
Load the state machine resource with the specified name, if any.
|
void |
paint(org.piccolo2d.util.PPaintContext paintContext) |
protected void |
resetStateMachine()
Reset the state machine to its "initial" configuration.
|
protected void |
setFrameSkip(int frameSkip)
Set the number of frames to skip to
frameSkip . |
addActivity, addAttribute, addChild, addChild, addChildren, addInputEventListener, addPropertyChangeListener, addPropertyChangeListener, animateToBounds, animateToColor, animateToPositionScaleRotation, animateToRelativePosition, animateToTransform, animateToTransparency, animateTransformToBounds, centerBoundsOnPoint, centerFullBoundsOnPoint, clone, computeFullBounds, endResizeBounds, findIntersectingNodes, fireChildPropertyChange, firePropertyChange, fullIntersects, fullPaint, fullPick, getAllNodes, getAllNodes, getAttribute, getAttribute, getBooleanAttribute, getBounds, getBoundsChanged, getBoundsReference, getBoundsVolatile, getChild, getChildBoundsInvalid, getChildBoundsVolatile, getChildPaintInvalid, getChildrenCount, getChildrenIterator, getChildrenPickable, getChildrenReference, getClientProperties, getClientPropertyKeysEnumeration, getDoubleAttribute, getFullBounds, getFullBoundsInvalid, getFullBoundsReference, getGlobalBounds, getGlobalFullBounds, getGlobalRotation, getGlobalScale, getGlobalToLocalTransform, getGlobalTranslation, getHeight, getInputEventListeners, getIntegerAttribute, getInverseTransform, getListenerList, getLocalToGlobalTransform, getName, getOccluded, getOffset, getPaint, getPaintInvalid, getParent, getPickable, getPropertyChangeParentMask, getRoot, getRotation, getScale, getTransform, getTransformReference, getTransparency, getUnionOfChildrenBounds, getVisible, getWidth, getX, getXOffset, getY, getYOffset, globalToLocal, globalToLocal, globalToLocal, indexOfChild, internalUpdateBounds, intersects, invalidateFullBounds, invalidateLayout, invalidatePaint, isAncestorOf, isDescendentOf, isDescendentOfRoot, isOpaque, layoutChildren, lerp, localToGlobal, localToGlobal, localToGlobal, localToParent, localToParent, localToParent, lower, lower, lowerBelow, lowerToBottom, lowerToBottom, offset, paintAfterChildren, parentBoundsChanged, parentToLocal, parentToLocal, parentToLocal, pick, pickAfterChildren, print, print, raise, raise, raiseAbove, raiseToTop, raiseToTop, removeAllChildren, removeChild, removeChild, removeChildren, removeFromParent, removeInputEventListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaintFrom, reparent, replaceWith, resetBounds, rotate, rotateAboutPoint, rotateAboutPoint, rotateInPlace, scale, scaleAboutPoint, scaleAboutPoint, setBounds, setBounds, setBoundsChanged, setChildBoundsInvalid, setChildBoundsVolatile, setChildPaintInvalid, setChildrenPickable, setFullBoundsInvalid, setGlobalRotation, setGlobalScale, setGlobalTranslation, setHeight, setName, setOccluded, setOffset, setOffset, setPaint, setPaintInvalid, setParent, setPickable, setPropertyChangeParentMask, setRotation, setScale, setTransform, setTransparency, setVisible, setWidth, setX, setY, signalBoundsChanged, startResizeBounds, toImage, toImage, toImage, toImage, transformBy, translate, validateFullBounds, validateFullPaint
protected AbstractStateMachineSprite()
protected abstract Animation createAnimation(String id)
id
- state idnull
if
no such animation existsprotected final void initializeStateMachine(SCXML stateMachine)
Note: this method should be called from the constructor of a subclass after its state machine has been instantiated.
stateMachine
- state machine to initialize, must not be nullprotected final void resetStateMachine()
protected final void fireStateMachineEvent(String eventName)
eventName
- event name, must not be nullprotected final int getFrameSkip()
0
.protected final void setFrameSkip(int frameSkip)
frameSkip
.frameSkip
- number of frames to skip, must be >= 0
protected final Animation getCurrentAnimation()
public final void advance()
public final void paint(org.piccolo2d.util.PPaintContext paintContext)
paint
in class org.piccolo2d.PNode
protected static final <T> SCXML loadStateMachine(Class<T> cls, String name)
cls
- classname
- namenull
if no such resource existsprotected static final <T> BufferedImage loadImage(Class<T> cls, String name)
cls
- classname
- namenull
if no such
resource existsCopyright (c) 2007-2013 held jointly by the individual authors. Licensed under the GNU Lesser General Public License (LGPL).