This chapter provides an explanation regarding user inputs.
The following explains how to enable controller operation for the displayed sprite.
Open Sample/Tutorial/Sample03_01.
Sample/Tutorial/Sample03_01/AppMain.cs
public static void Initialize ()
{
graphics = new GraphicsContext();
shaderProgram = new ShaderProgram("/Application/shaders/sprite.cgx");
shaderProgram.SetUniformBinding(0, "u_WorldMatrix");
ImageRect rectScreen = graphics.Screen.Rectangle;
texture = new Texture2D("/Application/resources/Player.png", false);
spritePlayer = new SimpleSprite(graphics, texture);
spritePlayer.Position.X = rectScreen.Width/2.0f;
spritePlayer.Position.Y = rectScreen.Height/2.0f;
spritePlayer.Position.Z = 0.0f;
unitScreenMatrix = new Matrix4(
2.0f/rectScreen.Width, 0.0f, 0.0f, 0.0f,
0.0f, -2.0f/rectScreen.Height, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
);
}
The SimpleSprite defined in the previous chapter will be used to display a sprite. Pass a reference to the GraphicsContext and Texture2D instances to the sprite constructor. After generating a SimpleSprite instance, insert a value to Position and specify the coordinates.
GamePad.GetData() is used to detect controller input. Input is detected in Update().
Sample/Tutorial/Sample03_01/AppMain.cs
public static void Update ()
{
gamePadData = GamePad.GetData(0);
int speed = 4;
if((gamePadData.Buttons & GamePadButtons.Left) != 0)
{
spritePlayer.Position.X -= speed;
}
if((gamePadData.Buttons & GamePadButtons.Right) != 0)
{
spritePlayer.Position.X += speed;
}
if((gamePadData.Buttons & GamePadButtons.Up) != 0)
{
spritePlayer.Position.Y -= speed;
}
if((gamePadData.Buttons & GamePadButtons.Down) != 0)
{
spritePlayer.Position.Y += speed;
}
}
The device number is set in the argument of GamePad.GetData(0). Normally, 0 is fine. Each key input state will be determined with if statements. If the keys are pressed, change the spritePlayer.Position value and update the coordinates of the player (image of the plane).
For execution with the PC Simulator, the gamepad buttons are each assigned to a key on a keyboard.
Keys on the gamepad Key assignments on the simulator Left directional key Cursor key: ← Up directional key Cursor key: ↑ Right directional key Cursor key: → Down directional key Cursor key: ↓ Square button A key Triangle button W key Circle button D key Cross button S key SELECT button Z key START button X key L button Q key R button E key
For details, refer to About the PlayStation(R)Mobile Simulator.
Let's try building and executing it. Player operation is possible with the directional keys.