public class Sprite

  1. Object
  2. Sprite

ImplementsPhysicsLinkable

Known subtypesAnimatedSprite

A drawable image with position, rotation, scale, tint and a normalized anchor.

A sprite is a lightweight data holder: it describes what and where, while a SpriteRenderer turns it into a GPU textured quad each frame (using the com.codename1.gpu package – an orthographic camera in pixel space, a com.codename1.gpu.Material.Type#SPRITE material and alpha blending). Because a sprite never touches the GPU directly you can create one with just an com.codename1.ui.Image – the renderer uploads and caches the matching com.codename1.gpu.Texture on demand.

#getX()/#getY() is the location of the anchor point (the image center by default); rotation and scale pivot around that anchor. Sprite implements com.codename1.gaming.physics.PhysicsLinkable so a physics body can drive it – see com.codename1.gaming.physics.PhysicsWorld.

Constructors

public Sprite()Creates an empty sprite with no image.
public Sprite(Image image)Creates a sprite drawing the given image, anchored at its center.

Methods

protected void onUpdate(double deltaSeconds)Per frame update hook.
public float getRenderWidth()The width in pixels the sprite renders at before scaling – an explicit size if one was set, otherwise the image width.
public float getRenderHeight()The height in pixels the sprite renders at before scaling.
public Rectangle getBounds()Returns the axis aligned bounding box of the (scaled) sprite, ignoring rotation.
public boolean intersects(Sprite other)Returns true if this sprite’s bounding box intersects the other’s.
public void setPhysicsPosition(float xPx, float yPx)Sets the object’s position from the body center, in pixels.
public void setPhysicsRotation(float radians)Sets the object’s rotation from the body, in radians (clockwise positive in screen space).
public Image getImage()
public void setImage(Image image)
public double getX()
public double getY()
public void setX(double x)
public void setY(double y)
public void setPosition(double x, double y)
public double getZ()The world-space depth, used when the GameView’s GameCamera is in perspective mode (GameCamera#MODE_PERSPECTIVE).
public void setZ(double z)
public void setPosition(double x, double y, double z)Sets the full 3D position.
public float getRotation()The rotation in degrees, clockwise.
public void setRotation(float degrees)
public float getScaleX()
public float getScaleY()
public void setScale(float scale)
public void setScale(float scaleX, float scaleY)
public void setSize(float widthPx, float heightPx)Overrides the rendered size in pixels (before scaling).
public int getColor()The ARGB tint multiplied with the texture (opaque white = no tint).
public void setColor(int argb)
public int getAlpha()The alpha applied while drawing, 0 (transparent) to 255 (opaque).
public void setAlpha(int alpha)
public double getAnchorX()
public double getAnchorY()
public void setAnchor(double anchorX, double anchorY)Sets the normalized anchor (0..1) used as the position and pivot point.
public boolean isVisible()
public void setVisible(boolean visible)
public int getZOrder()
public void setZOrder(int zOrder)Sets the z-order used by Scene to sort sprites; higher values draw on top.
public Object getUserData()
public void setUserData(Object userData)
public float getParallaxX()The horizontal parallax factor (default 1): the fraction of the scene camera’s x-offset this sprite scrolls by.
public float getParallaxY()The vertical parallax factor (default 1); see #getParallaxX().
public void setParallax(float x, float y)Sets the parallax factors.

Inherited methods

Constructor details

Sprite

public Sprite()
Creates an empty sprite with no image.

Sprite

public Sprite(Image image)
Creates a sprite drawing the given image, anchored at its center.

Method details

onUpdate

protected void onUpdate(double deltaSeconds)
Per frame update hook. The default implementation does nothing; subclasses such as AnimatedSprite override it to advance over time. Scene#update(double) invokes this for every sprite it contains.

getRenderWidth

public float getRenderWidth()
The width in pixels the sprite renders at before scaling – an explicit size if one was set, otherwise the image width.

getRenderHeight

public float getRenderHeight()
The height in pixels the sprite renders at before scaling.

getBounds

public Rectangle getBounds()
Returns the axis aligned bounding box of the (scaled) sprite, ignoring rotation. Useful for broad phase collision checks.

intersects

public boolean intersects(Sprite other)
Returns true if this sprite’s bounding box intersects the other’s.

setPhysicsPosition

public void setPhysicsPosition(float xPx, float yPx)
Sets the object’s position from the body center, in pixels.

setPhysicsRotation

public void setPhysicsRotation(float radians)
Sets the object’s rotation from the body, in radians (clockwise positive in screen space).

getImage

public Image getImage()

setImage

public void setImage(Image image)

getX

public double getX()

getY

public double getY()

setX

public void setX(double x)

setY

public void setY(double y)

setPosition

public void setPosition(double x, double y)

getZ

public double getZ()
The world-space depth, used when the GameView’s GameCamera is in perspective mode (GameCamera#MODE_PERSPECTIVE). Ignored in 2D.

setZ

public void setZ(double z)

setPosition

public void setPosition(double x, double y, double z)
Sets the full 3D position. In a 2D camera only x and y matter; in a perspective camera all three place the (billboarded) sprite in world space.

getRotation

public float getRotation()
The rotation in degrees, clockwise.

setRotation

public void setRotation(float degrees)

getScaleX

public float getScaleX()

getScaleY

public float getScaleY()

setScale

public void setScale(float scale)

setScale

public void setScale(float scaleX, float scaleY)

setSize

public void setSize(float widthPx, float heightPx)
Overrides the rendered size in pixels (before scaling). Pass values <= 0 to revert to the image’s own dimensions.

getColor

public int getColor()
The ARGB tint multiplied with the texture (opaque white = no tint).

setColor

public void setColor(int argb)

getAlpha

public int getAlpha()
The alpha applied while drawing, 0 (transparent) to 255 (opaque). Stored in the high byte of #getColor().

setAlpha

public void setAlpha(int alpha)

getAnchorX

public double getAnchorX()

getAnchorY

public double getAnchorY()

setAnchor

public void setAnchor(double anchorX, double anchorY)
Sets the normalized anchor (0..1) used as the position and pivot point. 0.5,0.5 (the default) is the image center, 0,0 is the top left corner.

isVisible

public boolean isVisible()

setVisible

public void setVisible(boolean visible)

getZOrder

public int getZOrder()

setZOrder

public void setZOrder(int zOrder)
Sets the z-order used by Scene to sort sprites; higher values draw on top.

getUserData

public Object getUserData()

setUserData

public void setUserData(Object userData)

getParallaxX

public float getParallaxX()
The horizontal parallax factor (default 1): the fraction of the scene camera’s x-offset this sprite scrolls by. 1 scrolls with the world; a value below 1 scrolls slower so the sprite reads as more distant (clouds, far mountains); 0 pins it to the screen (a HUD-like backdrop).

getParallaxY

public float getParallaxY()
The vertical parallax factor (default 1); see #getParallaxX().

setParallax

public void setParallax(float x, float y)
Sets the parallax factors. A background layer typically uses a small factor (e.g. 0.2-0.5) so it drifts behind the foreground as the camera scrolls.