SpriteRuntime

Introduction

The SpriteRuntime object can be used to draw a Texture2D to screen. It supports drawing the entire texture or a portion. It also provides the ability to scale according to the source texture size and aspect ratio.

Code Example

The following code can be used to instantiate a Sprite which uses a Texture (png file) named BearTexture.png.

var sprite = new SpriteRuntime();
sprite.SourceFileName = "BearTexture.png";
container.Children.Add(sprite);

SpriteRuntime Texture

A SpriteRuntime's texture can be assigned using the name of the file or a direct Texture2D reference.

Assigning SourceFileName

The SourceFileName property is used to assign the SpriteRuntime's texture using a file name. The name of the file should (by default) be relative to the Content folder. For example, consider the following line:

sprite.SourceFileName = "BearTexture.png";

This code assumes that the file is relative to the project's Content folder, as shown in the following screenshot:

For more information on working with files at runtime, see the File Loading page.

Assigning Texture

If your game manages its own textures, you can assign a Texture on the SpriteRuntime through its Texture property as shown in the following code.

// This code assumes that MyTexture is a valid Texture2D
sprite.Texture = MyTexture;

Note that assigning the SourceFileName property results in the Texture property referencing a texture if a valid texture is found.

Last updated