|
Genuts ME API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectCanvas
com.genuts.gameui.PlayField
A playfield is a Canvas Component that can contain Sprite objects.
Sprites added to a playfield are tracked in a list. The order of the list will define the sprites' back-to-front stacking order within the playfield. If no index is specified when adding a sprite to a playfield, it will be added to the end of the list (the sprite with a lowest id is displayed before a sprite a sprite with a high id).
The collision manager associated with a playfield checks collision when a sprite moves.
To perform animation, a playfield has its own thread. It can advise objects which implement the Tickable
interface. It can be a sprite or something else. This is another stacking order, the first in is the first called.
We recommand to use the Tickable interface rather than its own Thread for an object.
At start, the playfield is in pause mode, you have to activate it by setting the pause to false
.
When you want to stop or to dispose the playfield, don't forget to stop it. Otherwise, the associated thread
will not stop.
The display manager associated with a playfield draws efficiently the background of the playfield and all sprites.
By default, a playfield has a double buffering rendering if the device doesn't it by default. This will reduce the flicking effect when an
object is drawn.
The size of the display could be different from the size the Canvas. Therefore
the size of this display is set to the preferred size of the playfield. The setOffset
method sets the offset of the specified visual location. It should be useful for scrolling.
If the preferred size is smaller than the size of the playfield, offsets are set the place the visible area in the middle
of the Canvas.
addSprite(Sprite, int)
,
getSpriteIndex(Sprite)
,
setPause(boolean)
,
stop()
,
setOffset(int, int)
,
Tickable
,
CollisionManager
,
DisplayManager
,
Serialized FormConstructor Summary | |
PlayField()
Initializes the simple playfield. |
|
PlayField(CollisionManager collisionManager)
Initializes the simple playfield with a collision manager. |
|
PlayField(CollisionManager collisionManager,
DisplayManager displayManager,
int w,
int h)
Initializes the playfield with a collision manager and a preferred size. |
|
PlayField(CollisionManager collisionManager,
int w,
int h)
Initializes the playfield with a preferred size and a collision manager. |
|
PlayField(DisplayManager displayManager)
Initializes the simple playfield with a display manager. |
|
PlayField(DisplayManager displayManager,
int w,
int h)
Initializes the playfield with a preferred size and a display manager. |
|
PlayField(int w,
int h)
Initializes the playfield with a preferred size. |
Method Summary | |
void |
addSprite(Sprite s)
Adds the specified sprite to the end of this playfield. |
void |
addSprite(Sprite s,
int index)
Adds the specified sprite to this playfield at the given position. |
void |
addTickable(Tickable t)
Adds a tickable object to this playfield to the end of list. |
void |
addTickable(Tickable t,
int index)
Adds a tickable object to this playfield at the given position in the list. |
void |
checkAllCollisions()
Checks collisions of sprites with all other sprites in this playfield, and with the edges of this playfield. |
void |
checkCollision(Sprite sprite)
Checks collision of the given sprite with all other sprites in this playfield, and with the edges of this playfield. |
protected void |
fireCollisionEvent(Sprite s1,
Sprite s2)
Fires collision event to both sprites. |
protected boolean |
firePreCollisionEvent(Sprite s1,
Sprite s2)
Fires pre-collision event to sprite. |
int |
getBackground()
Gets the background color of this sprite. |
Image |
getBackgroundImage()
Returns the background image of this playfield. |
CollisionManager |
getCollisionManager()
Gets the collision manager of this playfield. |
DisplayManager |
getDisplayManager()
Gets the display manager of this playfield. |
int |
getPreferredHeight()
Returns the preferred height of this playfield. |
int |
getPreferredWidth()
Returns the preferred width of this playfield. |
int |
getSleepTime()
Returns the time delay between 2 ticks. |
Sprite |
getSprite(int n)
Gets the nth sprite in this playfield. |
int |
getSpriteCount()
Gets the number of sprites in this playfield. |
int |
getSpriteIndex(Sprite sprite)
Returns the index of the given sprite in this playfield. |
int |
getTicks()
Returns the number of ticks from the start of the this playfield. |
int |
getXOffset()
Gets the X offset of the display image. |
int |
getYOffset()
Gets the Y offset of the display image. |
void |
invalidate()
Invalidates this playfield. |
void |
invalidateSprite(Sprite sprite)
Invalidates a sprite for this playfield. |
boolean |
isPaused()
Gets pause state for this playfield. |
boolean |
isRunning()
Indicates if the playfield is running or not. |
void |
paint(Graphics g)
Paints this playfield and all visible sprites. |
void |
paintPlayField(Graphics g)
Paints this playfield. |
boolean |
preCheckCollision(Sprite sprite)
Checks pre-collision of the given sprite with all other sprites in this playfield, and with the edges of this playfield. |
void |
removeAllSprites()
Removes all sprites from this playfield. |
void |
removeSprite(int index)
Removes the sprite, specified by index , from this playfield. |
void |
removeSprite(Sprite s)
Removes the specified sprite from this playfield |
void |
removeTickable(Tickable t)
Removes a tickable object from this playfield. |
void |
run()
Used to send ticks. |
protected void |
sendTicks()
Send a tick to all tickable objects in this playfield. |
void |
setBackground(int color)
Sets the background color for this sprite. |
void |
setBackgroundImage(Image bg)
Sets the background image for this playfield. |
void |
setCollisionManager(CollisionManager manager)
Sets the collision manager for this playfield. |
void |
setDisplayManager(DisplayManager manager)
Sets the display manager for this playfield. |
void |
setOffset(int x,
int y)
Sets the paint offset of the display image. |
void |
setPause(boolean pause)
Sets pause state for this playfield. |
void |
setPause(int n)
Sets pause state for this playfield during n cycles. |
void |
setPreferredSize(int width,
int height)
Sets the preferred size for this playfield. |
void |
setSleepTime(int sleep)
Sets the time delay between 2 ticks. |
protected void |
setTicks(int ticks)
Sets the number of ticks from the start of the this playfield. |
void |
stop()
Stops explicitly the execution of this playfield. |
void |
update(Graphics g)
Overrides update to not erase the background before painting. |
void |
validate()
Validates this playfield. |
void |
validateSprite(Sprite sprite)
Validates a sprite for this playfield. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public PlayField(CollisionManager collisionManager, DisplayManager displayManager, int w, int h)
collisionManager
- Collision manager associated with this playfield.displayManager
- Display manager associated with this playfield.w
- Preferred width of the playfield.h
- Preferred height of the playfield.public PlayField(CollisionManager collisionManager)
The default display manager is a SpriteDisplayManager
.
The default display step is set to 8.
collisionManager
- Collision manager associated with this playfield.public PlayField(DisplayManager displayManager)
The default collision manager is a SpriteCollisionManager
.
The default collision step is set to 8.
displayManager
- Display manager associated with this playfield.public PlayField(int w, int h)
The default collision manager is a SpriteCollisionManager
.
The default collision step is set to 8.
The default display manager is a SpriteDisplayManager
.
The default display step is set to 8.
w
- Preferred width of the playfield.h
- Preferred height of the playfield.SpriteCollisionManager
public PlayField(CollisionManager collisionManager, int w, int h)
The default display manager is a SpriteDisplayManager
.
The default display step is set to 8.
collisionManager
- Collision manager associated with this playfield.w
- Preferred width of the playfield.h
- Preferred height of the playfield.SpriteCollisionManager
public PlayField(DisplayManager displayManager, int w, int h)
The default collision manager is a SpriteCollisionManager
.
The default collision step is set to 8.
displayManager
- Display manager associated with this playfield.w
- Preferred width of the playfield.h
- Preferred height of the playfield.SpriteCollisionManager
public PlayField()
The default collision manager is a SpriteCollisionManager
.
The default collision step is set to 8.
The default display manager is a SpriteDisplayManager
.
The default display step is set to 8.
SpriteCollisionManager
Method Detail |
public void setBackgroundImage(Image bg)
bg
- Background image for this playfield.public Image getBackgroundImage()
public int getBackground()
public void setBackground(int color)
color
- Color to set.public void setCollisionManager(CollisionManager manager)
manager
- Collision manager to set for this playfield.public CollisionManager getCollisionManager()
public void setDisplayManager(DisplayManager manager)
manager
- Display manager to set for this playfield.public DisplayManager getDisplayManager()
public int getPreferredWidth()
public int getPreferredHeight()
public void setPreferredSize(int width, int height)
width
- Preferred width for this playfield.height
- Preferred height for this playfield.public void addTickable(Tickable t)
t
- Tickable object to addpublic void addTickable(Tickable t, int index)
t
- Tickable object to addindex
- the position at which to insert the
tickable, or -1 to insert the tickable at the end.public void removeTickable(Tickable t)
t
- Tickable object to removepublic int getSpriteCount()
public void removeAllSprites()
addSprite(Sprite, int)
,
removeSprite(int)
public void addSprite(Sprite s, int index)
s
- Sprite to add.index
- the position at which to insert the
sprite, or -1 to insert the sprite at the end.public void addSprite(Sprite s)
s
- Sprite to addpublic void removeSprite(int index)
index
, from this playfield.
index
- the index of the sprite to remove
java.lang.ArrayIndexOutOfBoundsException
- if the index
value does not existpublic void removeSprite(Sprite s)
s
- Sprite to removepublic boolean preCheckCollision(Sprite sprite)
If one or more collisions are detected, firePreCollisionEvent(Sprite, Sprite)
is called.
The action is delgated to the collision manager associated with this playfield.
Return the validate state given by the collision manager or true
if
there is no collision manager associated with this playfield.
sprite
- Sprite to checkCollisionManager.checkCollision(Sprite)
,
firePreCollisionEvent(Sprite, Sprite)
public void checkCollision(Sprite sprite)
If one or more collisions are detected, fireCollisionEvent(Sprite, Sprite)
is called.
The action is delgated to the collision manager associated with this playfield.
sprite
- Sprite to checkCollisionManager.checkCollision(Sprite)
,
fireCollisionEvent(Sprite, Sprite)
public void checkAllCollisions()
If one or more collisions are detected, fireCollisionEvent(Sprite, Sprite)
is called.
The action is delgated to the collision manager associated with this playfield.
CollisionManager.checkCollision(Sprite)
,
fireCollisionEvent(Sprite, Sprite)
public void invalidateSprite(Sprite sprite)
sprite
- Sprite to invalidate.public void validateSprite(Sprite sprite)
sprite
- Sprite to validate.public Sprite getSprite(int n)
n
- the index of the sprite to get.
java.lang.ArrayIndexOutOfBoundsException
- if the nth value does not existpublic int getSpriteIndex(Sprite sprite)
sprite
- The sprite to look for.
protected boolean firePreCollisionEvent(Sprite s1, Sprite s2)
Calls the method preCollisionWith(Sprite) of both sprites.
s1
- Sprite 1 to send the event.s2
- Sprite 2 to send the event.
true
if a pre-collision is confirmed.Sprite.preCollisionWith(Sprite)
protected void fireCollisionEvent(Sprite s1, Sprite s2)
s1
- Sprite 1 to send the event.s2
- Sprite 2 to send the event.public void invalidate()
public void validate()
public void setOffset(int x, int y)
Useful when the display image is greater than the size of the playfield. This feature can be used to scroll in the playfield.
x
- X offset of the imagey
- Y offset of the imagepublic int getXOffset()
public int getYOffset()
public void paintPlayField(Graphics g)
g
- The graphics context to use for painting.public void update(Graphics g)
g
- The graphics context to use for painting.public void paint(Graphics g)
g
- The graphics context to use for painting.public int getSleepTime()
By default, sleep time is of 40ms.
public void setSleepTime(int sleep)
sleep
- Number of milliseconds between 2 ticks.public int getTicks()
protected void setTicks(int ticks)
ticks
- The number of ticks of this playfield, every 100000 ticks
the counter restarts to 0.public void setPause(int n)
n
- Number of cycles to be in pause.public void setPause(boolean pause)
pause
- true
to set the playfield in
pause state, false
otherwise.public boolean isPaused()
true
to set the playfield in
pause state, false
otherwise.public boolean isRunning()
true
if the playfield is running,
false
otherwise.public void stop()
public final void run()
run
in interface java.lang.Runnable
Thread.run()
protected void sendTicks()
|
Genuts ME API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |