简体   繁体   English

带有加速度的加速度计-移动图像Windows Phone 8

[英]Accelerometer with acceleration - move image Windows Phone 8

Developing a very simple-learning game for windows phone 8 in C#, i have created an acceleration vector like this: 在C#中为Windows Phone 8开发一个非常简单的学习游戏,我创建了一个加速向量,如下所示:

Vector3 acceleration = accelerometerReading.Acceleration;

i cannot do anything like: 我不能做任何事情:

mycontrol.Xacceleration = mycontrol.Xacceleration + acceleration.X * 200;

How can i apply that acceleration to an image, i cannot find any cordinates or acceleration property. 如何将加速度应用于图像,找不到坐标或加速度属性。 I am trying to move an image, like in labyrinth game, how can i achieve this with the acceleration? 我正在尝试移动图像,就像在迷宫游戏中一样,如何通过加速实现此目的? How can i apply it? 我该如何申请?

Thanks for help! 感谢帮助!

If you want to make a game with moving objects, you should look into using XNA or a game engine like Unity because the performance of C#/XAML will not be good. 如果要制作带有移动对象的游戏,则应考虑使用XNA或Unity之类的游戏引擎,因为C#/ XAML的性能不好。
If you really want to use C#/xaml you will need to calculate the postion of your image control yourself. 如果您真的想使用C#/ xaml,则需要自己计算图像控件的位置。 To set the position you can put your image inside a Canvas and set the position like this: 要设置位置,您可以将图像放置在“画布”中并按如下所示设置位置:

Canvas.SetTop(myControl,XPosition);
Canvas.SetLeft(myControl,YPosition);

You can also use StoryBoard to animate the objects but it will probably be more complex if the acceleration and direction keep changing 您还可以使用StoryBoard为对象设置动画,但如果加速度和方向不断变化,则可能会更复杂

To take advantage of hardware acceleration you will need to set CacheMode="BitmapCache" on the moving object. 要利用硬件加速,您将需要在移动对象上设置CacheMode =“ BitmapCache”。

Here is some code to calculate the object position: 这是一些计算对象位置的代码:

public class ObjectInfo
    {
        public Vector2 Position { get; set; }
        public Vector2 Speed { get; set; }
        public Vector2 Acceleration { get; set; }
    }


    private DispatcherTimer dispatcherTimer;
    private int refreshTimeMilisecond = 100;
    private ObjectInfo myObject;

    public void Init()
    {
        myObject = new ObjectInfo();
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromMilliseconds(refreshTimeMilisecond);
        dispatcherTimer.Tick += dispatcherTimer_Tick;
    }

void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        myObject.Position = myObject.Position + myObject.Speed*refreshTimeMilisecond;
        myObject.Speed = myObject.Speed + myObject.Acceleration * refreshTimeMilisecond;

        Canvas.SetTop(myControl, myObject.Position.X);
        Canvas.SetLeft(myControl, myObject.Position.Y);
    }

In xaml: 在xaml中:

<Canvas>
    <Image x:Name="myControl" CacheMode="BitmapCache" Source="SmyleyImagePath"/>
</Canvas>

You will need to play with the refreshTimeMilisecond parameter to find out what work the best 您将需要使用refreshTimeMilisecond参数来找出最有效的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM