C# Class SunsetHigh.Sprite

Class for anything that can be drawn with a sprite. Characters and Pickups (and other drawable game components) can extend from this class to add behavior to the sprite. Alternative, just use this class for simple or static drawings.
Inheritance: IInteractable
Datei anzeigen Open project: ErraticUnicorn/MOSH Class Usage Examples

Public Methods

Method Description
Sprite ( ) : System

Initializes a Sprite at the origin which will match the dimensions of its texture when the texture is loaded

Sprite ( int x, int y ) : System

Initializes a Sprite at the given position. It will match the dimensions of its texture when the texture is loaded.

Sprite ( int x, int y, int width, int height ) : System

Initializes a Sprite at the given position with the given dimensions

draw ( SpriteBatch sb ) : void

Draws this sprite. Call this method on every Sprite in the Game's draw cycle. Override this method in child classes as necessary (i.e. if drawing additional components)

getAnimationTime ( ) : float
getBoundingRect ( ) : Rectangle
getColor ( ) : Color
getCommonImage ( string imageFileName ) : Texture2D

Retrieves a pre-loaded 2D texture to associate with an instantiated Sprite object. Good for multiple objects using the same texture

getHeight ( ) : int
getImage ( ) : Texture2D
getImageColumns ( ) : int
getImageRows ( ) : int
getWidth ( ) : int
getX ( ) : int
getXCenter ( ) : int
getY ( ) : int
getYCenter ( ) : int
isVisible ( ) : bool
loadCommonImage ( ContentManager content, string imageFileName ) : void

Loads a 2D texture in the "Content" directory for use by multiple Sprite objects. Call this method in the loading content cycle, and when instantiating new objects in-game, call getCommonImage()

loadContent ( ContentManager content ) : void

Loads all content necessary for this Sprite. Call this method on every Sprite in the Game's load cycle. Override this method when writing the classes that must load specific sprites and sound effects.

loadImage ( ContentManager content, string fileName ) : void

Loads a static 2D texture with the given filename in the "Content" directory

loadImage ( ContentManager content, string fileName, int numRows, int numCols, float anTime ) : void

Loads a 2D spritesheet with the given filename in the "Content" directory

onCollide ( IInteractable other ) : void
onInteract ( ) : void
reset ( ) : void
setCenter ( int x, int y ) : void

Sets the position of this Sprite with given center coordinates

setColor ( Color color ) : void
setDimensions ( int width, int height ) : void

Sets the dimensions of this Sprite

setHeight ( int height ) : void
setImage ( Texture2D image ) : void

Sets a static 2D texture that has already been loaded to be used to draw this Sprite

setImage ( Texture2D image, int numRows, int numCols, float anTime ) : void

Sets an animating 2D texture that has already been loaded to be used to draw this Sprite

setPosition ( int x, int y ) : void

Sets the position of this Sprite given the coordinates of its top-left corner

setSourceRect ( int x, int y, int width, int height ) : void

Sets the source rectangle of a loaded texture for this Sprite (if the particular texture is a collection of different sprites).

setVisible ( bool visible ) : void
setWidth ( int width ) : void
setX ( int x ) : void
setXCenter ( int x ) : void
setY ( int y ) : void
setYCenter ( int y ) : void
unloadCommonImages ( ) : void

Clears memory of all loaded textures. Be sure to call unload on the ContentManager as well.

update ( float elapsed ) : void

Updates this sprite based on time. If the sprite can animate, then it will do so here. Call this method on every Sprite in the Game's update cycle. Override this method in child classes as necessary.

Protected Methods

Method Description
matchToTextureDimensions ( ) : void
resetAnimation ( ) : void
setAnimationTime ( float time ) : void
setFrameColumn ( int col ) : void
setFrameRow ( int row ) : void
setImageColumns ( int cols ) : void
setImageRows ( int rows ) : void

Private Methods

Method Description
nullCheck ( ) : void
searchDirectories ( ContentManager content, string fileName ) : Texture2D

Method Details

Sprite() public method

Initializes a Sprite at the origin which will match the dimensions of its texture when the texture is loaded
public Sprite ( ) : System
return System

Sprite() public method

Initializes a Sprite at the given position. It will match the dimensions of its texture when the texture is loaded.
public Sprite ( int x, int y ) : System
x int X coordinate of top-left corner
y int Y coordinate of top-left corner
return System

Sprite() public method

Initializes a Sprite at the given position with the given dimensions
public Sprite ( int x, int y, int width, int height ) : System
x int X coordinate of top-left corner
y int Y coordinate of top-left corner
width int Width in pixels; specify 0 to use texture dimensions
height int Height in pixels; specify 0 to use texture dimensions
return System

draw() public method

Draws this sprite. Call this method on every Sprite in the Game's draw cycle. Override this method in child classes as necessary (i.e. if drawing additional components)
public draw ( SpriteBatch sb ) : void
sb Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch passed in through Game
return void

getAnimationTime() public method

public getAnimationTime ( ) : float
return float

getBoundingRect() public method

public getBoundingRect ( ) : Rectangle
return Rectangle

getColor() public method

public getColor ( ) : Color
return Color

getCommonImage() public static method

Retrieves a pre-loaded 2D texture to associate with an instantiated Sprite object. Good for multiple objects using the same texture
public static getCommonImage ( string imageFileName ) : Texture2D
imageFileName string The file name of the image that was previously loaded
return Texture2D

getHeight() public method

public getHeight ( ) : int
return int

getImage() public method

public getImage ( ) : Texture2D
return Texture2D

getImageColumns() public method

public getImageColumns ( ) : int
return int

getImageRows() public method

public getImageRows ( ) : int
return int

getWidth() public method

public getWidth ( ) : int
return int

getX() public method

public getX ( ) : int
return int

getXCenter() public method

public getXCenter ( ) : int
return int

getY() public method

public getY ( ) : int
return int

getYCenter() public method

public getYCenter ( ) : int
return int

isVisible() public method

public isVisible ( ) : bool
return bool

loadCommonImage() public static method

Loads a 2D texture in the "Content" directory for use by multiple Sprite objects. Call this method in the loading content cycle, and when instantiating new objects in-game, call getCommonImage()
public static loadCommonImage ( ContentManager content, string imageFileName ) : void
content ContentManager The content manager
imageFileName string The file name of the image, without the extension
return void

loadContent() public method

Loads all content necessary for this Sprite. Call this method on every Sprite in the Game's load cycle. Override this method when writing the classes that must load specific sprites and sound effects.
public loadContent ( ContentManager content ) : void
content ContentManager ContentManager passed in through Game
return void

loadImage() public method

Loads a static 2D texture with the given filename in the "Content" directory
public loadImage ( ContentManager content, string fileName ) : void
content ContentManager ContentManager passed in through Game
fileName string File name of XNB image in "Content" directory
return void

loadImage() public method

Loads a 2D spritesheet with the given filename in the "Content" directory
public loadImage ( ContentManager content, string fileName, int numRows, int numCols, float anTime ) : void
content ContentManager ContentManager passed in through Game
fileName string File name of XNB image in "Content" directory
numRows int Number of rows in this spritesheet
numCols int Number of columns in this spritesheet
anTime float Time between frames (in seconds) when animating the sprite
return void

matchToTextureDimensions() protected method

protected matchToTextureDimensions ( ) : void
return void

onCollide() public method

public onCollide ( IInteractable other ) : void
other IInteractable
return void

onInteract() public method

public onInteract ( ) : void
return void

reset() public method

public reset ( ) : void
return void

resetAnimation() protected method

protected resetAnimation ( ) : void
return void

setAnimationTime() protected method

protected setAnimationTime ( float time ) : void
time float
return void

setCenter() public method

Sets the position of this Sprite with given center coordinates
public setCenter ( int x, int y ) : void
x int X coordinate of center
y int Y coordinate of center
return void

setColor() public method

public setColor ( Color color ) : void
color Color
return void

setDimensions() public method

Sets the dimensions of this Sprite
public setDimensions ( int width, int height ) : void
width int Width in pixels
height int Height in pixels
return void

setFrameColumn() protected method

protected setFrameColumn ( int col ) : void
col int
return void

setFrameRow() protected method

protected setFrameRow ( int row ) : void
row int
return void

setHeight() public method

public setHeight ( int height ) : void
height int
return void

setImage() public method

Sets a static 2D texture that has already been loaded to be used to draw this Sprite
public setImage ( Texture2D image ) : void
image Texture2D
return void

setImage() public method

Sets an animating 2D texture that has already been loaded to be used to draw this Sprite
public setImage ( Texture2D image, int numRows, int numCols, float anTime ) : void
image Texture2D The pre-loaded image
numRows int Number of rows in this spritesheet
numCols int Number of columns in this spritesheet
anTime float Time between frames (in seconds) when animating the sprite
return void

setImageColumns() protected method

protected setImageColumns ( int cols ) : void
cols int
return void

setImageRows() protected method

protected setImageRows ( int rows ) : void
rows int
return void

setPosition() public method

Sets the position of this Sprite given the coordinates of its top-left corner
public setPosition ( int x, int y ) : void
x int X coordinate of top-left corner
y int Y coordinate of top-left corner
return void

setSourceRect() public method

Sets the source rectangle of a loaded texture for this Sprite (if the particular texture is a collection of different sprites).
public setSourceRect ( int x, int y, int width, int height ) : void
x int X coordinate of top-left corner
y int Y coordinate of top-left corner
width int Width in pixels
height int Height in pixels
return void

setVisible() public method

public setVisible ( bool visible ) : void
visible bool
return void

setWidth() public method

public setWidth ( int width ) : void
width int
return void

setX() public method

public setX ( int x ) : void
x int
return void

setXCenter() public method

public setXCenter ( int x ) : void
x int
return void

setY() public method

public setY ( int y ) : void
y int
return void

setYCenter() public method

public setYCenter ( int y ) : void
y int
return void

unloadCommonImages() public static method

Clears memory of all loaded textures. Be sure to call unload on the ContentManager as well.
public static unloadCommonImages ( ) : void
return void

update() public method

Updates this sprite based on time. If the sprite can animate, then it will do so here. Call this method on every Sprite in the Game's update cycle. Override this method in child classes as necessary.
public update ( float elapsed ) : void
elapsed float Time (in seconds) that has elapsed since the last update
return void