XNa tutorial -- (2) mobile control

Source: Internet
Author: User
Document directory
  • XNa Controller
  • A simple example
  • How to operate the mouse?
  • However
  • Series list

In the previous chapter, we mainly introduced

What is xNa?

And made the first xNa helloworld

This article will be followed by the above. Continue Our xNa journey.

 

At the end of the previous article, we passed

Mousestate MS = mouse. getstate (); // get mouse information

 

The point position of the mouse is obtained, so that the image is displayed at the mouse position. Therefore, the custom Optical Mark function is implemented.

Now, we fully describe the xNa controller.

 

XNa Controller

The xNa controller is mainly defined under Microsoft. xNa. Framework. input,

It can be divided into the following types:

 

  1. Mouse
  2. Keyboard keyboard
  3. Gamepad handle

 

In the helloworld created by the default template, we will find that

If (gamepad. getstate (playerindex. One). Buttons. Back = buttonstate. Pressed)
This. Exit ();

 

This is to judge that if the back button of the game controller is pressed, it will automatically exit.

Similarly, we can use the above input State to obtain operations on the input end, such as buttons and positions.

A simple example

 

Now, we try to draw a simple sprite (sprite) on the interface and implement simultaneous control of the mouse and keyboard.

 

Code

Protected override void Update (gametime)
{
Keyboardstate state = keyboard. getstate ();
If (State. iskeydown (Keys. Up ))
{
This. position. Y-= 10;
}
If (State. iskeydown (Keys. Down ))
{
This. position. Y + = 10;

}
If (State. iskeydown (Keys. Left ))
{
This. position. X-= 10;

}
If (State. iskeydown (Keys. Right ))
{
This. position. x + = 10;

}
Base. Update (gametime );
}

 

 

Through the above Code, we have implemented simple listening to the keyboard.

In this way, when we press the keyboard up and down, our sprite will move.

Code

Protected override void draw (gametime)
{
Graphicsdevice. Clear (color. cornflowerblue );
Spritebatch. Begin ();
Spritebatch. Draw (IMG, new rectangle (INT) position. X, (INT) position. Y, 50, 80), color. White );
Spritebatch. End ();
Base. Draw (gametime );
}

 

How to operate the mouse?

Based on the similar logic above, we can certainly get the current position of the mouse. However, if we do this, our sprite will move from the original point to the target point instantly without intermediate transition.

In normal games, the above logic is often complicated, with obstacles and monsters in the middle. Therefore, you cannot simply perform point operations.

For future scalability, we will use the-star algorithm to solve this problem.

For what is a-star, please note here

Here, we use the-star class library written by Franco and Gustavo to calculate the shortest path.

Of course, the obstacles we build here are empty. In the future, we need to build relevant obstacles in the real map.

First, we construct an empty, unobstructed point set to describe obstacles.

Code

Private byte [,] m_matrix;
M_matrix = new byte [1024,1024];
// Fixme: In matrix, the obstacle is 0 else is 1
For (INT y = 0; y <m_matrix.getupperbound (1); y ++)
{
For (INT x = 0; x <m_matrix.getupperbound (0); X ++)
{
M_matrix [x, y] = 1;
}
}

 

Here, if there is an obstacle, it is 0. Otherwise, it is 1.

Code

// Calculate the shortest path with the mouse position added to update
Mousestate state1 = mouse. getstate ();
If (state1.leftbutton = buttonstate. Pressed)
{
Ipathfinder Pathfinder = new pathfinderfast (m_matrix );
Pathfinder. formula = heuristicformula. Manhattan;
Pathfinder. searchlimit = 2000;
M_path = Pathfinder. findpath (New vector2 (INT) This. position. X, (INT) This. position. Y), new vector2 (state1.x, state1.y ));
}
If (this. m_path! = NULL & this. m_path.count ()! = 0)
{
Int Index = This. m_path.count ()-1;
This. Position = new vector2 (this. m_path [Index]. X, this. m_path [Index]. y );
This. m_path.removeat (INDEX );
Return;
}

 

The above code first obtains the current mouse position and calculates the shortest path from the current point to the target point.

And then store it. In this way, if there is no action, we can automatically continue with the relevant automatic movement every cycle.

 

With the above Code, we have implemented simple control with the mouse or keyboard.

 

However

We may have discovered that the sprite here is a single frame movement, not an animation. How can we make the characters move? Please pay attention to the next decomposition.

Source File Download

To be continue ....

 


Series list

 


  1. XNa tutorial ---- (1)


  2. XNa tutorial ---- (2) mobile control


  3. XNa tutorial ---- (3) simple animation

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.