Silverlight C # Game Development: Mobile farming system (3) mobile farming

Source: Internet
Author: User

Moving with the wind is a kind of feeling, and it is enough to show it well. Many friends have proposed various methods to make it more natural to move with the wind. I believe they can all achieve it, and the effect is better than mine, the purpose of writing this article is to develop new ideas and use some simple methods to combine exciting results. This time, let's move the grass with the mouse, it seems that there is an invisible hand in the grass :)

This time, we still use the block division of the grid to determine the route of the mouse, and then generate the logic of the rotation on the grass. See:

This seems very simple. It is also to increase efficiency, but the grass on the corresponding grid will execute the logic, and the mouse moves there will trigger it. I think this is touch ), to achieve the desired effect, first transform the animation of the Grass01 control and add the following code to the UserControl In The XAML. in Resources, this is an animation of the touch effect.

Ani_Touch code

1 <Storyboard x: Name = "Ani_Touch" AutoReverse = "False">
2 <DoubleAnimationUsingKeyFrames Storyboard. TargetProperty = "(UIElement. RenderTransform). (CompositeTransform. Rotation)" Storyboard. TargetName = "LayoutRoot">
3 <EasingDoubleKeyFrame KeyTime = "0" Value = "0"/>
4 <EasingDoubleKeyFrame KeyTime = "0: 0. 2" Value = "-1.839"/>
5 <EasingDoubleKeyFrame KeyTime = ". 4" Value = "1.1"/>
6 <EasingDoubleKeyFrame KeyTime = "0: 0. 6" Value = "0"/>
7 </DoubleAnimationUsingKeyFrames>
8 </Storyboard>

 

Then add the public GrassTouch () method for the Grass01 control. This method only includes the execution of Ani_Touch.Begin.

Now we can refer to the method in the previous article to add the corresponding behavior for the entire logic, and then write a set of Touch logic, such as GrassTouchLogic implementation, but isn't it very troublesome? We found that the composition method of network data is consistent with the basic functions of GrassLogic. Therefore, we inherit GrassLogic and obtain its features, in this way, only the required operation data can be written as protected.

Now a GrassLogicTouch class is implemented, inherited from GrassLogic:

GrassLogicTouch code

Public class GrassLogicTouch: GrassLogic
{
DispatcherTimer TouchTimer = new DispatcherTimer ();

Public GrassLogicTouch (int arrayW, int arrayH, int rangeW, int rangeH)
: Base (arrayW, arrayH, rangeW, rangeH)
{
TouchTimer. Tick + = new EventHandler (TouchTimer_Tick );
TouchTimer. Interval = TimeSpan. FromMilliseconds (10 );
}

Void TouchTimer_Tick (object sender, EventArgs e)
{
TouchTimer. Stop ();
}

Public void ITouchGrass (Point pt)
{
If (! TouchTimer. IsEnabled)
{
TouchTimer. Start ();
If (pt. X> RangeW | pt. Y> RangeH | pt. X <0 | pt. Y <0)
Return;
Int tx = (int) pt. X/(base. RangeW/base. ArrayW );
Int ty = (int) pt. Y/(base. RangeH/base. ArrayH );
List <Grass01> items = GrassBuffArray [ty, tx];
If (items! = Null)
{
Foreach (var item in items)
{
Item. GrassTouch ();
}
}
}
}
}

 

This is actually very simple. We use ITouchGrass to complete the touch logic. The input parameter is the mouse coordinate point, but in order to avoid the mouse moving too frequently, it will trigger events constantly, we added a TouchTimer with an interval of 10 ms to control it. When it is triggered again within 10 ms, it is invalid. This method is generally used for the determination similar to double-click, but it is easy to use, it can reduce the additional overhead of the mouse. You can modify this value to see the effect.

Modify MainPage as follows:

_ GrassLogic changed from GrassLogic to GrassLogicTouch class

Remove _ GrassLogic. StartWave (); from the constructor.

Move event with the mouse added: this. MouseMove + = new MouseEventHandler (MainPage_MouseMove );

Void MainPage_MouseMove (object sender, MouseEventArgs e)
{
_ GrassLogic. ITouchGrass (e. GetPosition (_ Image ));
}

Very good. There may be some minor changes, but this is basically the only one. Run it and check it out:


The source code is here: the lawn System (3) moves with the mouse

The grass animation effect has been completed, and there may be more and more effects. For example, when people walk, they will touch grass, and when they use skills, they will touch grass, the effects of such an extension depend entirely on our imagination. Good things are not necessarily complex, and complicated things are not necessarily good. When we write long code, do you want to calm down and think about these interesting gadgets? The Details determine success or failure. At this point, I would like to express my deep gratitude to the right hand of dark blue. Let's show off the light of Silverlight.

 

We recommend the Silverlight game development blog:Dark blue right hand

 

Related Article

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.