Académique Documents
Professionnel Documents
Culture Documents
Presents
Session Eight:
Behavior
2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation
Contents
1 Behavior Overview
2 Game Objects Behavior
2.1 Game Implementation: Condor Behavior
2.2 Game Implementation: Fire Behavior
3
5
Behavior
1 Behavior Overview
A behavior is a set of functions, defined by the user, which describes how a game and its objects should behave
and act. You can define a behavior to the game and to some of its objects, like sprites and texts. The functions
assigned in a game object behavior (the game object is an instance of a class derived from the Game class) are
executed successively.
Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:
Trooper b = StarTrooper.Trooper;
if (AnimationIndex != 1)
{
Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y - Position.Y);
v.Normalize();
Velocity = v;
if (v.X >= 0)
ScaleX = 1;
else
ScaleX = -1;
List<Sprite> collidedSprites = Game.GetCollidedSprites(this);
if (collidedSprites != null)
{
foreach (Sprite s in collidedSprites)
{
if (s is Fire)
{
StarTrooper.Die.Play();
AnimationIndex = 1;
m_Score++;
StarTrooper.Score.Text = "Score: " + m_Score.ToString();
Game.Remove(s);
break;
}
else if(s is Trooper)
{
m_CollisionWithTrooper = true;
StarTrooper.Die.Play();
Animation.Stop();
m_Score--;
StarTrooper.Score.Text = "Score: " + m_Score.ToString();
break;
}
}
}
}
else
{
if (Animation.PlayingLastFrame)
Game.Remove(this);
}
When the spacebar is triggered, a new Fire sprite is created, the Shoot sound effect is played, and the text
Shoots is incremented and displayed.
Delete the following code in the StarTrooperSprites.cs file under public override void Update() function:
if (Keyboard.IsTriggered(Key.Space))
{
Fire fire = (Fire)StarTrooper.Fire.Clone();
fire.Position = new PointF(Position.X, Position.Y - 35);
fire.Velocity = new Vector2(0, -4);
Game.Add(fire);
}
Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:
if (Keyboard.IsTriggered(Key.Space))
{
Fire fire = (Fire)StarTrooper.Fire.Clone();
fire.Position = new PointF(Position.X, Position.Y - 35);
fire.Velocity = new Vector2(0, -4);
Game.Add(fire);
StarTrooper.Shoot.Play();
m_Shoots++;
StarTrooper.Shoots.Text = "Shoots:" + m_Shoots.ToString();
}