Class Sprite
- All Implemented Interfaces:
PhysicsLinkable
- Direct Known Subclasses:
AnimatedSprite
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 -
Method Summary
Modifier and TypeMethodDescriptionintgetAlpha()The alpha applied while drawing, 0 (transparent) to 255 (opaque).doubledoubleReturns the axis aligned bounding box of the (scaled) sprite, ignoring rotation.intgetColor()The ARGB tint multiplied with the texture (opaque white = no tint).getImage()floatThe height in pixels the sprite renders at before scaling.floatThe width in pixels the sprite renders at before scaling -- an explicit size if one was set, otherwise the image width.floatThe rotation in degrees, clockwise.floatfloatdoublegetX()doublegetY()intbooleanintersects(Sprite other) Returns true if this sprite's bounding box intersects the other's.booleanprotected voidonUpdate(double deltaSeconds) Per frame update hook.voidsetAlpha(int alpha) voidsetAnchor(double anchorX, double anchorY) Sets the normalized anchor (0..1) used as the position and pivot point.voidsetColor(int argb) voidvoidsetPhysicsPosition(float xPx, float yPx) Sets the object's position from the body center, in pixels.voidsetPhysicsRotation(float radians) Sets the object's rotation from the body, in radians (clockwise positive in screen space).voidsetPosition(double x, double y) voidsetRotation(float degrees) voidsetScale(float scale) voidsetScale(float scaleX, float scaleY) voidsetSize(float widthPx, float heightPx) Overrides the rendered size in pixels (before scaling).voidsetUserData(Object userData) voidsetVisible(boolean visible) voidsetX(double x) voidsetY(double y) voidsetZOrder(int zOrder) Sets the z-order used bySceneto sort sprites; higher values draw on top.
-
Constructor Details
-
Sprite
public Sprite()Creates an empty sprite with no image. -
Sprite
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 asAnimatedSpriteoverride 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
Returns the axis aligned bounding box of the (scaled) sprite, ignoring rotation. Useful for broad phase collision checks. -
intersects
Returns true if this sprite's bounding box intersects the other's. -
setPhysicsPosition
public void setPhysicsPosition(float xPx, float yPx) Description copied from interface:PhysicsLinkableSets the object's position from the body center, in pixels.- Specified by:
setPhysicsPositionin interfacePhysicsLinkable
-
setPhysicsRotation
public void setPhysicsRotation(float radians) Description copied from interface:PhysicsLinkableSets the object's rotation from the body, in radians (clockwise positive in screen space).- Specified by:
setPhysicsRotationin interfacePhysicsLinkable
-
getImage
-
setImage
-
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 bySceneto sort sprites; higher values draw on top. -
getUserData
-
setUserData
-