WP 8.1计时器更新更快的方式(游戏) [英] WP 8.1 Timer Update faster way (game)

查看:63
本文介绍了WP 8.1计时器更新更快的方式(游戏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我试图制作一个简单的擦拭游戏,你有三条生成敌人的线,你必须控制你的玩家。 (这里是截图的链接:

屏幕截图
)。


现在问题出现了:


它是亲切的因为它不会足够快地刷新我的敌人并且它可以逐个移动它们你可以看到它。那么我怎样才能让这项工作变得更好? (我试过一个Task,但我不知道怎么在WP上调用)。


所以这里是用过的GUI代码:

< Image Margin =" 10,0,10,0"的Horizo​​ntalAlignment = QUOT;中心" VerticalAlignment = QUOT;底座" X:名称= QUOT; playerImage"宽度= QUOT; 50"高度= QUOT; 50"源= QUOT;资产/ playerModel1.png" /> 

< Grid x:Name =" enemyGrid"背景= QUOT;透明"的Horizo​​ntalAlignment = QUOT;拉伸" VerticalAlignment = QUOT;拉伸">
< / Grid>

使用的反码:

 DispatcherTimer enemyTimer = new DispatcherTimer(); 

public MainPage()
{
//一些代码

//玩家模型的操纵事件
this.ManipulationMode = ManipulationModes。平移X;
this.ManipulationCompleted + =(o,e)=>
{
if(e.Cumulative.Translation.X< = -50)
{
if(playerImage.Horizo​​ntalAlignment == Windows.UI.Xaml.Horizo​​ntalAlignment.Right)
playerImage.Horizo​​ntalAlignment = Windows.UI.Xaml.Horizo​​ntalAlignment.Center;
else
playerImage.Horizo​​ntalAlignment = Windows.UI.Xaml.Horizo​​ntalAlignment.Left;

ApplicationData.Current.LocalSettings.Values [" TotalSwap"] =(int)ApplicationData.Current.LocalSettings.Values [" TotalSwap"] + 1;
}

if(e.Cumulative.Translation.X> = 50)
{
if(playerImage.Horizo​​ntalAlignment == Windows.UI.Xaml.Horizo​​ntalAlignment .Left)
playerImage.Horizo​​ntalAlignment = Windows.UI.Xaml.Horizo​​ntalAlignment.Center;
else
playerImage.Horizo​​ntalAlignment = Windows.UI.Xaml.Horizo​​ntalAlignment.Right;
ApplicationData.Current.LocalSettings.Values [" TotalSwap"] =(int)ApplicationData.Current.LocalSettings.Values [" TotalSwap"] + 1;
}
};

//敌人更新计时器
enemyTimer.Tick + =(o,e)=> updateEnemy();
enemyTimer.Interval = new TimeSpan(0,0,0,0,1);

//一些代码
}

void updateEnemy()
{
//循环每个控件(敌人模型的所有图像)在敌人的网格中
foreach(图片img in enemyGrid.Children)
{
//检查玩家模型是否与敌人模型结合
if(checkEnemy(img))
{
//结束游戏
end();
休息;
}

//检查敌人模型是否在屏幕外并删除它如果为真
if(img.Margin.Top + 5> this.ActualHeight)
{
enemyGrid.Children.Remove(img);
enemyCounter ++;
ApplicationData.Current.LocalSettings.Values [" TotalScore"] =(int)ApplicationData.Current.LocalSettings.Values [" TotalScore"] + 1;
}

//通过设置margin-top
img.Margin = new Thickness(15,img.Margin.Top + 5,15,0)将图像向下移动5个像素);
}

//如果它是前一段时间以来的最后一个敌人并且随机创造了一个新的敌人,那么其他敌人就会筹集到最后一个敌人
if(random.Next(1,10)== 1 && lastEnemy> =(minLastEnemy - Math.Floor(enemyCounter / 20.0)))
{
//创建一个nem敌人模型并将其添加到enemyGrid
createEnemy() ;
lastEnemy = 0;
}
else
lastEnemy ++;

//显示计数器
output2.Text = enemyCounter.ToString();
}

我希望我写清楚并且有人有个主意。


谢谢,DK


解决方案

嗨< a href ="https://social.msdn.microsoft.com/profile/damon_kronski/?ws=usercard-mini"target ="_ blank">
Damon_Kronski,


就像你提到的标题一样,你的案例与Windows Phone有关,恐怕你发布在一个不合适的论坛上。我会将您的主题移至
Windows和Windows手机应用    > 

为Windows Phone开发
论坛以获得更好的效果支持。谢谢。




祝你好运,


Kristin


Hello there

I tried to create a simple wipe-game where you got 3 lines on which enemys are spawned and you have to control your Player trough them. (here's a link to a screenshot: Screenshot).

Here's now the problem:

It's kind of laggy because it won't refresh my enemys fast enough and it moves them one by one which you can see. So how can I make this work better? (I tried with a Task but i don't know how to invoke on WP).

So here's the used GUI code:

<Image Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Bottom" x:Name="playerImage" Width="50" Height="50" Source="assets/playerModel1.png"/>

<Grid x:Name="enemyGrid" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</Grid>

And the used back-code:

DispatcherTimer enemyTimer = new DispatcherTimer();

public MainPage()
{
  // Some Code
  
  // Manipulation Event for Player Model
  this.ManipulationMode = ManipulationModes.TranslateX;
  this.ManipulationCompleted += (o, e) =>
  {
    if (e.Cumulative.Translation.X <= -50)
    {
      if (playerImage.HorizontalAlignment == Windows.UI.Xaml.HorizontalAlignment.Right)
        playerImage.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
      else
        playerImage.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
	
      ApplicationData.Current.LocalSettings.Values["TotalSwap"] = (int)ApplicationData.Current.LocalSettings.Values["TotalSwap"] + 1;
    }

    if (e.Cumulative.Translation.X >= 50)
    {
      if (playerImage.HorizontalAlignment == Windows.UI.Xaml.HorizontalAlignment.Left)
        playerImage.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
      else
        playerImage.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;
      ApplicationData.Current.LocalSettings.Values["TotalSwap"] = (int)ApplicationData.Current.LocalSettings.Values["TotalSwap"] + 1;
    }
  };
  
  // Timer for enemy Update
  enemyTimer.Tick += (o, e) => updateEnemy();
  enemyTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
  
  // Some Code
  }
  
void updateEnemy()
{
  // Loop every control (all images for enemy model) in the enemy grid
  foreach (Image img in enemyGrid.Children)
  {
	// Checks if the playermodel colides with the enemy model
    if (checkEnemy(img))
    {
      // End Game
      end();
      break;
    }

    // Check if enemy model is out of screen and deletes it if true
    if (img.Margin.Top + 5 > this.ActualHeight)
    {
      enemyGrid.Children.Remove(img);
      enemyCounter++;
      ApplicationData.Current.LocalSettings.Values["TotalScore"] = (int)ApplicationData.Current.LocalSettings.Values["TotalScore"] + 1;
    }

    // Move image 5 pixel down by setting the margin-top
    img.Margin = new Thickness(15, img.Margin.Top + 5, 15, 0);
  }
  
  // If it's some time ago since last enemy and random create a new enemy else raise last enemy
  if (random.Next(1, 10) == 1 && lastEnemy >= (minLastEnemy - Math.Floor(enemyCounter / 20.0)))
  {
    // Creates a nem enemy model and add it to the enemyGrid
    createEnemy();
    lastEnemy = 0;
  }
  else
    lastEnemy++;

  // Show the counter
  output2.Text = enemyCounter.ToString();
}

I hope I wrote it clearly and someone has an idea.

Thanks, DK

解决方案

Hi Damon_Kronski,

Like your title mentioned, your case related to Windows Phone, I am afraid you posted in an inappropriate forum. I will move your thread to Windows and Windows phone apps   >  Developing for Windows Phone forum for better support. Thanks.

Best regards,

Kristin


这篇关于WP 8.1计时器更新更快的方式(游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆