Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Windows-Phone-7

Using the CommandScene Class to Create Command Scenes in the XNA, WPXNA (12)

5.00/5 (1 vote)
3 Jul 2013CPOL2 min read 6.8K  
Using the CommandScene class to create command scenes in the XNA, WPXNA (12)

The original post can be found here.

Introduction/Catalog

I have developed some games on Windows Phone. Here, I'll share my experiences and gradually upload some classes, no good name, I just call it WPXNA. (Some example code may not be stringent enough.)

  • CommandScene
  • Example

CommandScene

CommandScene class inherits from the Scene class, in this scene, we will control some buttons, and these can be passed to the outside world.

C#
internal event EventHandler<SceneEventArgs> Executing;

private readonly Shape backgroundShape;

protected readonly List<Button> buttons = new List<Button> ( );
private readonly List<Anime> animes = new List<Anime> ( );

The event Executing will tell the outside world which button is clicked. The field buttons are all buttons that are controlled by CommandScene.

Field backgroundShape is the background picture of the scene, field animes is animations in the scene.

The CommandScene contains a number of resources, including a font, a background image, a button clicking sound.

C#
internal CommandScene ( Vector2 location, GestureType gestureType, 
   string backgroundResourcePath, IList<Resource> resources, 
   IList<Making> makings, bool isBroken )
 : base ( location, gestureType,
 ResourceManager.Combine ( new Resource[] {
  new Resource ( "peg", ResourceType.Font, @"font\peg" ),
  new Resource ( "background", ResourceType.Image, 
    string.Format ( @"image\{0}", backgroundResourcePath ) ),
  new Resource ( "click.s", ResourceType.Sound, @"sound\click" ),
 }, resources ),
 combine ( new Making[] {
  new Shape ( "background.s", "background" )
 }, makings ),
 isBroken
 )
{
 this.backgroundShape = this.makings["background.s"] as Shape;

 foreach ( Making making in this.makings.Values )
  if ( making is Button )
  {
   Button button = making as Button;
   button.Selected += this.buttonSelected;
   this.buttons.Add ( button );
  }
  else if ( making is Anime )
   this.animes.Add ( making as Anime );
}

public override void Dispose ( )
{
 foreach ( Button button in this.buttons )
  button.Selected -= this.buttonSelected;

 this.buttons.Clear ( );
 this.animes.Clear ( );

 base.Dispose ( );
}

In the constructor, we get all the Button objects from the makings and set their Selected events. In the Dispose method, we do the opposite operations.

C#
private void buttonSelected ( object sender, ButtonEventArgs e )
{ this.Execute ( e.Command ); }

internal void Execute ( string command )
{
 if ( null != this.Executing )
  this.Executing ( this, new SceneEventArgs ( command ) );

}

Each button's Selected event will execute buttonSelected method, in the method, we will call the Execute method.

In the Execute method, we will trigger the Executing event of CommandScene, parameter command is the command field of button, so that the outside world can know which button was clicked. If button's IsSole is true, then only one button's click is valid.

C#
protected override void inputing ( Controller controller )
{

 foreach ( Button button in this.buttons )
  Button.PressTest ( button, controller.Motions );

 foreach ( Button button in this.buttons )
  if ( Button.ClickTest ( button, controller.Motions ) && button.IsSole )
   break;

}

In inputing method, we call the PressTest and ClickTest methods.

C#
protected override void drawing ( GameTime time, SpriteBatch batch )
{
 Shape.Draw ( this.backgroundShape, time, batch );

 foreach ( Anime anime in this.animes )
  Anime.Draw ( anime, time, batch );

 foreach ( Button button in this.buttons )
  button.Draw ( batch );

}

protected override void updating ( GameTime time )
{
 foreach ( Anime anime in this.animes )
  anime.Update ( time );

}

In drawing method, we will draw the buttons and animations, as well as the background picture of the scene. In updating method, we will update the animations in the scene, like a bird.

Example

SceneT13 is a simple scene, we will include two buttons.

C#
internal sealed class SceneT13
 : CommandScene
{

 internal SceneT13 ( )
  : base ( Vector2.Zero, GestureType.None, "background1",
  new Resource[] {
   new Resource ( "play.image", ResourceType.Image, @"image\button1" ),
   new Resource ( "stop.image", ResourceType.Image, @"image\button2" ),
  },
  new Making[] {
   new Button ( "b.play", "play.image", "PLAY", 
     new Vector2 ( 100, 100 ), 100, 50, new Point ( 1, 1 ) ),
   new Button ( "s.play", "stop.image", "STOP", 
     new Vector2 ( 100, 300 ), 100, 50, new Point ( 1, 1 ) )
  }
  )
 { }

}

In OnNavigatedTo method, we will add scene SceneT13 by the appendScene method.

C#
protected override void OnNavigatedTo ( NavigationEventArgs e )
{
 // ...

 this.appendScene ( new Scene[] { new mygame.test.SceneT13 ( ) } );

 base.OnNavigatedTo ( e );
}

We have slightly modified the appendScene and RemoveScene methods so they called sceneAppending and sceneRemoving. In this way, we can modify sceneAppending and sceneRemoving to get some work done.

C#
private void appendScene ( Scene scene, Type afterSceneType, bool isInitialized )
{
 if ( null == scene )
  return;

 this.sceneAppending ( scene );

 // ...
}

internal void RemoveScene ( Scene scene )
{

 if ( null == scene || !this.scenes.Contains ( scene ) )
  return;

 this.sceneRemoving ( scene );
 
 // ...
}

In the sceneAppending method, we set the event Executing of the scene SceneT13. In the sceneRemoving method, we remove the Executing event.

C#
private void sceneAppending ( Scene scene )
{
 if ( scene is mygame.test.SceneT13 )
  ( scene as mygame.test.SceneT13 ).Executing += this.sceneT13Executing;
}

private void sceneRemoving ( Scene scene )
{
 if ( scene is mygame.test.SceneT13 )
  ( scene as mygame.test.SceneT13 ).Executing -= this.sceneT13Executing;
}

In the sceneT13Executing method, we print the Command field of parameter e that says the button command.

C#
private void sceneT13Executing ( object sender, SceneEventArgs e )
{
 Debug.WriteLine ( "SceneT13: " + e.Command );
}

Get the code from here, for more contents, please visit WPXNA.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)