Some methods that are common to the Entity Modifiers are especially useful. XxxModifier clone()
Returns a copy of the Modifier. This method is useful if you need two Entities to behave the same way, but not be driven by the identical Modifier. Each Modifier executes its own animation sequence during the duration, and you might not want the Entities’ ani- mations linked, for example.
boolean isFinished()
Returns true if the Modifier has finished its action.
void setRemoveWhenFinished(final boolean pRemoveWhenFinished) boolean isRemoveWhenFinished()
Sets or gets a f lag saying whether the Modifier should remove itself once it’s finished.
void setModifierListener(final IModifierListener<T> pModifierListener) IModifierListener<T> getModifierListener()
Sets or gets a listener routine that is called when the Modifier is finished. Only one such ModifierListener can be registered.
ptg999
Chapter 4 Scenes, Layers, Transitions, and Modifiers 64
Position
These Modifiers change the current position of the Entity. They are not methods, but rather are classes that are instantiated as shown in the pattern earlier, under “Entity Modifiers.”
MoveModifier(final float pDuration, final float pFromX, final float pToX, final float pFromY, final float pToY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction
pEaseFunction)MoveModifier(final float pDuration, final float pFromX, final float pToX,
final float pFromY, final float pToY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) MoveModifier(final float pDuration, final float pFromX, final float pToX,
final float pFromY, final float pToY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) MoveModifier(final float pDuration, final float pFromX, final float pToX,
final float pFromY, final float pToY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
All of these methods cause the Entity to move in both the X and Y directions at the same time. In each case pDuration is the length of time, in seconds, that you want the movement to last. We will talk about EaseFunctions in the next section, but for the moment think of them as “modifiers to the Modifiers.” EntityModifierListeners are callbacks that are initiated when the Modifier is finished with its action.
MoveXModifier(final float pDuration, final float pFromX, final float pToX) MoveXModifier(final float pDuration, final float pFromX, final float pToX, final
IEaseFunction pEaseFunction)
MoveXModifier(final float pDuration, final float pFromX, final float pToX, final IEntityModifierListener pEntityModifierListener)
MoveXModifier(final float pDuration, final float pFromX, final float pToX, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
MoveXModifier(final MoveXModifier pMoveXModifier)
MoveYModifier(final float pDuration, final float pFromY, final float pToY) MoveYModifier(final float pDuration, final float pFromY, final float pToY, final
IEaseFunction pEaseFunction)
MoveYModifier(final float pDuration, final float pFromY, final float pToY, final IEntityModifierListener pEntityModifierListener)
ptg999
Scenes in AndEngine 65
MoveYModifier(final float pDuration, final float pFromY, final float pToY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
MoveYModifier(final MoveYModifier pMoveYModifier)
These Modifiers move the Entity in only the X direction or the Y direction, as indi- cated. Otherwise, they are the same.
Path
Moving from one point to another is all well and good, but AndEngine also allows us to specify a multipoint path and have an Entity follow that Path. What’s more, we can register a callback to be initiated whenever the Entity reaches each waypoint in the Path.
There are three constructors for Paths: Path(final int pLength)
Path(final float[] pCoordinatesX, final float[] pCoordinatesY) Path(final Path pPath)
The first constructor says that the Path will consist of pLength points. The actual Path is then constructed using the to method to add each segment:
to(final float pX, final float pY)
The second constructor uses arrays of x and y coordinates to construct the Path, and the third simply uses a Path that already exists.
The constructors for PathModifier are as follows: PathModifier(final float pDuration, final Path pPath) PathModifier(final float pDuration, final Path pPath,
final IEaseFunction pEaseFunction)
PathModifier(final float pDuration, final Path pPath, final IEntityModifierListener pEntityModiferListener) PathModifier(final float pDuration, final Path pPath,
final IEntityModifierListener pEntityModiferListener, final IEaseFunction pEaseFunction)
PathModifier(final float pDuration, final Path pPath, final IEntityModifierListener pEntityModiferListener, final IPathModifierListener pPathModifierListener) PathModifier(final float pDuration, final Path pPath,
final IEntityModifierListener pEntityModiferListener, final IPathModifierListener pPathModifierListener, final IEaseFunction pEaseFunction)
Listing 4.1 shows a brief example of creating a Path and registering a PathModifier with a callback method.
ptg999
Chapter 4 Scenes, Layers, Transitions, and Modifiers 66
Listing 4.1 PathModifier Example
. . .
final Path path = new Path(5).to(10, 10).to(10, 50).to(50, 50). to(50, 10).to(10, 10);
entity.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path, null, new IPathModifierListener() {
@Override
public void onWaypointPassed(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
switch(pWaypointIndex) { case 0: entity.setColor(1.0f, 0.0f, 0.0f); break; case 1: entity.setColor(0.0f, 1.0f, 0.0f); break; case 2: entity.setColor(0.0f, 0.0f, 1.0f); break; case 3: entity.setColor(1.0f, 0.0f, 0.0f); break; } } )); scene.getLastChild().attachChild(entity); . . .
Scale
ScaleModifiers modify the displayed scale of the Entity. The set of constructors is similar to the set we saw for Position:
ScaleModifier(final float pDuration, final float pFromScale, final float pToScale) ScaleModifier(final float pDuration, final float pFromScale, final float pToScale,
final IEaseFunction pEaseFunction)
ScaleModifier(final float pDuration, final float pFromScale, final float pToScale, final IEntityModifierListener pEntityModifierListener)
ScaleModifier(final float pDuration, final float pFromScale, final float pToScale, final IEntityModifierListener pEntityModifierListener, final
IEaseFunction pEaseFunction)
All of these methods cause the Entity to be scaled for display around its scale position over some specific duration. The parameters are straightforward. A scale of 1.0f is the same size as the original.
ScaleModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY)
ptg999
Scenes in AndEngine 67
This Modifier scales the Entity immediately and allows the scaling to be different in the X and Y directions.
ScaleAtModifier(final float pDuration, final float pFromScale, final
float pToScale, final float pScaleCenterX, final float pScaleCenterY) ScaleAtModifier(final float pDuration, final float pFromScale, final
float pToScale, final float pScaleCenterX, final float pScaleCenterY, final IEaseFunction pEaseFunction)
ScaleAtModifier(final float pDuration, final float pFromScale, final
float pToScale, final float pScaleCenterX, final float pScaleCenterY, final IEntityModifierListener pEntityModifierListener)
ScaleAtModifier(final float pDuration, final float pFromScale, final
float pToScale, final float pScaleCenterX, final float pScaleCenterY, final IEntityModifierListener pEntityModifierListener,
final IEaseFunction pEaseFunction)
ScaleAtModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final float pScaleCenterX, final float pScaleCenterY)
ScaleAtModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final float pScaleCenterX, final float pScaleCenterY,
final IEaseFunction pEaseFunction)
ScaleAtModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final float pScaleCenterX, final float pScaleCenterY,
final IEntityModifierListener pEntityModifierListener)
ScaleAtModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final float pScaleCenterX, final float pScaleCenterY,
final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
These Modifiers are the same as the ScaleModifiers given previously, but they scale around a point different from the current ScaleCenter position.
Color
The ColorModifiers change the color, as you would expect. All color values range from 0.0f (zero intensity) to 1.0f (full intensity).
ColorModifier(final float pDuration, final float pFromRed, final float pToRed, final float pFromGreen, final float pToGreen, final float pFromBlue, final float pToBlue)
ColorModifier(final float pDuration, final float pFromRed, final float pToRed, final float pFromGreen, final float pToGreen, final float pFromBlue, final float pToBlue, final IEaseFunction pEaseFunction)
ptg999
Chapter 4 Scenes, Layers, Transitions, and Modifiers 68
ColorModifier(final float pDuration, final float pFromRed, final float pToRed, final float pFromGreen, final float pToGreen, final float pFromBlue, final float pToBlue, final IEntityModifierListener
pEntityModifierListener, final IEaseFunction pEaseFunction)
ColorModifier(final float pDuration, final float pFromRed, final float pToRed, final float pFromGreen, final float pToGreen, final float pFromBlue, final float pToBlue, final IEntityModifierListener
pEntityModifierListener, final IEaseFunction pEaseFunction)
All of these methods cause the Entity color to change over the duration. The EaseFunction and EntityModifierListener function just as with the other Entity Modifiers.
Rotation
RotationModifiers rotate an Entity about a point—either the current RotationCenter position or some other point. Rotations are always given in degrees.
RotationModifier(final float pDuration, final float pFromRotation, final float pToRotation)
RotationModifier(final float pDuration, final float pFromRotation, final float pToRotation, final IEntityModifierListener pEntityModifierListener)
RotationModifier(final float pDuration, final float pFromRotation, final float pToRotation final IEaseFunction pEaseFunction) RotationModifier(final float pDuration, final float pFromRotation,
final float pToRotation, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
These Modifiers are the ones to use when you want the Entity to rotate around the current RotationCenter over a given duration.
RotationAtModifier(final float pDuration, final float pFromRotation, final float pToRotation, final float pRotationCenterX, final float pRotationCenterY)
RotationAtModifier(final float pDuration, final float pFromRotation, final float pToRotation, final float pRotationCenterX, final float pRotationCenterY, final IEntityModifierListener pEntityModifierListener)
RotationAtModifier(final float pDuration, final float pFromRotation, final float pToRotation, final float pRotationCenterX,
final float pRotationCenterY, final IEaseFunction pEaseFunction) RotationAtModifier(final float pDuration, final float pFromRotation,
final float pToRotation, final float pRotationCenterX, final float pRotationCenterY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
ptg999
Scenes in AndEngine 69
These Modifiers are used when you want rotation to occur about a point that is not the current RotationCenter for a duration. This technique avoids changing the RotationCenter, which you might not want to do.
RotationByModifier(final float pDuration, final float pRotation) RotationByModifier(final RotationByModifier pRotationByModifier)
These Modifiers rotate an Entity about the current RotationCenter immediately. You could do the same thing with RotationModifier and a pDuration of 0.0f, but these methods are clearer (and more efficient).
Transparency
Transparency of an Entity is controlled through its Alpha value. These Modifiers include the ones used to cause an Entity to fade in or out.
AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha)
AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha, final IEntityModifierListener)
AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha, final IEaseFunction pEaseFunction)
AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
The parameters are straightforward. Alpha values range from 0.0f (invisible, or com- pletely transparent) to 1.0f (opaque).
FadeInModifier(final float pDuration)
FadeInModifier(final float pDuration, final IEaseFunction pEaseFunction) FadeInModifier(final float pDuration, final IEntityModifierListener
pEntityModifierListener)
FadeInModifier(final float pDuration, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction)
These Modifiers are used to fade Entities in and out. They are convenience classes that wrap the AlphaModifier classes given earlier with Alpha values of 0.0f and 1.0f.
Delay
It may not be obvious why you would ever want a DelayModifier, but in the next section we will talk about combining Modifiers into sequences. In those cases, the ability to delay for a given time is a requirement.
DelayModifier(final float pDuration)
DelayModifier(final float pDuration, final IEntityModifierListener pEntityModifierListener)
ptg999
Chapter 4 Scenes, Layers, Transitions, and Modifiers 70
These Modifiers simply delay their action for the specified duration, and optionally call the EntityModifierListener when finished.