对于一个学校项目,我需要制作一个多个椭圆移动的小游戏。 用我的方法同时制作多个的最后一个用我制作的计时器移动。
如何为所有省略号制作一个计时器。
class EnemyTeam
{
private Ellipse enemy;
private Canvas canvas;
private double xChange = 50, yChange = 50;
public DispatcherTimer enemyTimer;
private char direction = '0';
private Thickness enemyThickness;
public EnemyTeam(Canvas canvas, double startPosition, SolidColorBrush playerBrush)
{
this.canvas = canvas;
DrawTeam(canvas, 40, playerBrush);
enemyTimer.Interval = TimeSpan.FromMilliseconds(100);
enemyTimer.Start();
}
private void DrawBall(SolidColorBrush brush, Canvas canvas,double x,double y)
{
enemy = new Ellipse();
enemy.Stroke = brush;
enemy.Fill = brush;
enemy.Height = 30;
enemy.Width = 30;
enemy.Margin = new Thickness(x,y, 0, 0);
enemyTimer = new DispatcherTimer();
enemyThickness = enemy.Margin;
canvas.Children.Add(enemy);
enemyTimer.Tick += enemyTimer_Tick;
}
void enemyTimer_Tick(object sender, EventArgs e)
{
if (enemyThickness.Left >= canvas.Width - 100)
{
GoDown();
direction = '1';
}
if (enemyThickness.Left <= 0 + 20)
{
GoDown();
direction = '0';
}
MoveTeam(enemy);
}
private void MoveTeam(Ellipse enemy)
{
enemyThickness = enemy.Margin;
if (direction == '1')
{
enemyThickness.Left -= xChange;
}
if (direction == '0')
{
enemyThickness.Left += xChange;
}
enemy.Margin = enemyThickness;
}
private void GoDown()
{
enemyThickness.Top += yChange;
enemy.Margin = enemyThickness;
}
}
答案 0 :(得分:0)
将enemyTimer声明为静态字段:
class EnemyTeam
{
private static enemyTimer = new DispatcherTimer();
...
关键字static将使该字段共享该字段。
答案 1 :(得分:0)
不是在DrawBall
方法中初始化和分配事件处理程序,而是在EnemyTeam
类的构造函数中执行此操作。这将为每个EnemyTeam
对象提供计时器。
答案 2 :(得分:0)
你正在制作多个计时器并扔掉它们。见这一行:
enemyTimer = new DispatcherTimer();
每当你打电话给你时,你都会制作一个新的计时器并丢弃enemyTimer
持有引用的先前副本。因为在enemyTimer.Start()
之后调用DrawTeam
,所以只在最后创建的计时器上调用它。没有其他计时器开始。
但即使其他计时器开始,你仍然只能看到一个椭圆移动,因为在enemyTimer_Tick
中你只能对enemy
进行更改,这是一个类成员变量,指向最后创建的椭圆。
我建议您只使用一个计时器,将您在列表中创建的所有省略保存在以后使用,并在enemyTimer_Tick
中通过迭代列表来更新所有这些省略号。
编辑:这是您的代码的副本,重新设计一下,向您展示我的意思。我真的不明白你要对MoveTeam和敌人的变量做什么,所以我并没有把这些东西搞得一团糟。也就是说,这不是一个完整的工作解决方案,只是我所建议的变化的一个例子。
using System.Collections.Generic;
class EnemyTeam
{
private List<Ellipse> enemies = new List<Ellipse>();
private Canvas canvas;
private double xChange = 50, yChange = 50;
public DispatcherTimer enemyTimer;
private char direction = '0';
private Thickness enemyThickness;
public EnemyTeam(Canvas canvas, double startPosition, SolidColorBrush playerBrush)
{
this.canvas = canvas;
DrawTeam(canvas, 40, playerBrush);
enemyTimer = new DispatcherTimer();
enemyTimer.Interval = TimeSpan.FromMilliseconds(100);
enemyTimer.Tick += enemyTimer_Tick;
enemyTimer.Start();
}
private void DrawBall(SolidColorBrush brush, Canvas canvas,double x,double y)
{
enemy = new Ellipse();
enemy.Stroke = brush;
enemy.Fill = brush;
enemy.Height = 30;
enemy.Width = 30;
enemy.Margin = new Thickness(x,y, 0, 0);
enemyThickness = enemy.Margin; // what is this supposed to do?
canvas.Children.Add(enemy);
enemies.Add(enemy);
}
void enemyTimer_Tick(object sender, EventArgs e)
{
foreach (Ellipse enemy in enemies)
{
if (enemyThickness.Left >= canvas.Width - 100)
{
GoDown();
direction = '1';
}
if (enemyThickness.Left <= 0 + 20)
{
GoDown();
direction = '0';
}
MoveTeam(enemy);
}
}
private void MoveTeam(Ellipse enemy)
{
enemyThickness = enemy.Margin;
if (direction == '1')
{
enemyThickness.Left -= xChange;
}
if (direction == '0')
{
enemyThickness.Left += xChange;
}
enemy.Margin = enemyThickness;
}
private void GoDown()
{
enemyThickness.Top += yChange;
enemy.Margin = enemyThickness;
}
}