Class Sprite

java.lang.Object
com.codename1.gaming.Sprite
All Implemented Interfaces:
PhysicsLinkable
Direct Known Subclasses:
AnimatedSprite

public class Sprite extends Object implements PhysicsLinkable

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.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty sprite with no image.
    Sprite(Image image)
    Creates a sprite drawing the given image, anchored at its center.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    The alpha applied while drawing, 0 (transparent) to 255 (opaque).
    double
     
    double
     
    Returns the axis aligned bounding box of the (scaled) sprite, ignoring rotation.
    int
    The ARGB tint multiplied with the texture (opaque white = no tint).
     
    float
    The height in pixels the sprite renders at before scaling.
    float
    The width in pixels the sprite renders at before scaling -- an explicit size if one was set, otherwise the image width.
    float
    The rotation in degrees, clockwise.
    float
     
    float
     
     
    double
     
    double
     
    int
     
    boolean
    Returns true if this sprite's bounding box intersects the other's.
    boolean
     
    protected void
    onUpdate(double deltaSeconds)
    Per frame update hook.
    void
    setAlpha(int alpha)
     
    void
    setAnchor(double anchorX, double anchorY)
    Sets the normalized anchor (0..1) used as the position and pivot point.
    void
    setColor(int argb)
     
    void
    setImage(Image image)
     
    void
    setPhysicsPosition(float xPx, float yPx)
    Sets the object's position from the body center, in pixels.
    void
    setPhysicsRotation(float radians)
    Sets the object's rotation from the body, in radians (clockwise positive in screen space).
    void
    setPosition(double x, double y)
     
    void
    setRotation(float degrees)
     
    void
    setScale(float scale)
     
    void
    setScale(float scaleX, float scaleY)
     
    void
    setSize(float widthPx, float heightPx)
    Overrides the rendered size in pixels (before scaling).
    void
    setUserData(Object userData)
     
    void
    setVisible(boolean visible)
     
    void
    setX(double x)
     
    void
    setY(double y)
     
    void
    setZOrder(int zOrder)
    Sets the z-order used by Scene to sort sprites; higher values draw on top.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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)
      Description copied from interface: PhysicsLinkable
      Sets the object's position from the body center, in pixels.
      Specified by:
      setPhysicsPosition in interface PhysicsLinkable
    • setPhysicsRotation

      public void setPhysicsRotation(float radians)
      Description copied from interface: PhysicsLinkable
      Sets the object's rotation from the body, in radians (clockwise positive in screen space).
      Specified by:
      setPhysicsRotation in interface PhysicsLinkable
    • 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)
    • 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)