Created
May 20, 2012 20:49
-
-
Save demonixis/2759484 to your computer and use it in GitHub Desktop.
PlayState example with YNA Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
using Yna.Display; | |
using Yna.Input; | |
namespace Yna.Test | |
{ | |
public class SpriteState : YnState | |
{ | |
SpriteFont font; // Font pour le texte | |
Sprite sonicSprite; // Sprite du joueur | |
public SpriteState () { } | |
public override void Initialize () | |
{ | |
// Création d'un Sprite à la position 50, 50 en utilisant la texture soniclg4 du dossier 2d | |
sonicSprite = new Sprite (new Vector2 (50, 50), "2d//soniclg4"); | |
// Ajoute le Sprite à la scène | |
Add (sonicSprite); | |
} | |
public override void LoadContent () | |
{ | |
base.LoadContent (); | |
// Chargement du Font | |
font = YnG.Content.Load<SpriteFont> ("debug"); | |
// Chargement de la texture | |
// Discutable car peut etre fait après l'initialisation | |
sonicSprite.LoadContent (); | |
// Indique que le Sprite est animé, renseigne la taille d'un Sprite sur la feuille de Sprite | |
sonicSprite.PrepareAnimation (50, 41); | |
// Ajoute des animations | |
// 1 - Nom de l'animation | |
// 2 - Tableau d'indices ciblant les frames | |
// 3 - Temps d'affichage d'une frame | |
// 4 - Indique si l'animation doit etre retournée | |
sonicSprite.AddAnimation ("down", new int[] { 0, 1, 2, 3 }, 150, false); | |
sonicSprite.AddAnimation ("left", new int[] { 4, 5, 6, 7 }, 150, false); | |
sonicSprite.AddAnimation ("right", new int[] { 8, 9, 10, 11 }, 150, false); | |
sonicSprite.AddAnimation ("up", new int[] { 12, 13, 14, 15 }, 150, false); | |
// Joue l'animation "up" | |
sonicSprite.Play ("up"); | |
// Modifie la taille du Sprite | |
// Tout est automatiquement mis à jour | |
sonicSprite.Scale = new Vector2 (2.5f, 2.5f); | |
// Force le sprite à rester sur l'écran | |
sonicSprite.ForceInsideScreen = true; | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
base.Update(gameTime); // Les objets attachés sont mis à jour | |
// Déplacement du Sprite | |
if (YnG.Keys.Pressed(Keys.Up)) | |
{ | |
sonicSprite.Play("up"); | |
sonicSprite.Y--; | |
} | |
else if (YnG.Keys.Pressed(Keys.Down)) | |
{ | |
sonicSprite.Play("down"); | |
sonicSprite.Y++; | |
} | |
else if (YnG.Keys.Pressed(Keys.Left)) | |
{ | |
sonicSprite.Play("left"); | |
sonicSprite.X--; | |
} | |
else if (YnG.Keys.Pressed(Keys.Right)) | |
{ | |
sonicSprite.Play("right"); | |
sonicSprite.X++; | |
} | |
} | |
public override void Draw (GameTime gameTime, SpriteBatch spriteBatch) | |
{ | |
base.Draw (gameTime, spriteBatch); // Les objets attachés sont dessinés | |
spriteBatch.Begin (); | |
// Affichage du titre en gros et centré | |
string title = "Animated Sprite !"; | |
Vector2 size = font.MeasureString (title); | |
Vector2 zoom = new Vector2 (1.5f, 1.5f); | |
Vector2 position = new Vector2 (YnG.Width / 2 - ((size.X / 2) * zoom.X), 35); | |
spriteBatch.DrawString (font, title, position, Color.AntiqueWhite, 0.0f, Vector2.Zero, zoom, SpriteEffects.None, 1.0f ); | |
spriteBatch.End (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment