矩形应该在屏幕上移动但消失了
Rectangle is supposed to move on the screen but disappears instead
所以我想做一个迷你 "tower defense" 游戏。
现在,我目前的问题是我无法将图片框设置为 "shoot" 矩形 "bullet"
我在开头声明了这些变量
Rectangle Bullet = new Rectangle(225, 400, 10, 25); //position of this shouldn't matter since it's not drawed instantly and it's later moved moved under the picturebox.
private bool bulletIsActive = false;
private int update_speed = 250;
此外,定时器被声明为 AFTER InitializeComponent();
InitializeComponent();
timer1.Interval = update_speed;
timer1.Start();
我有用于按键的事件处理程序,而且我有(只包括 Keys.Space 因为其他的无关紧要。
private void Paaikkuna_KeyDown(object sender, KeyEventArgs e)
{
case Keys.Space:
if(bulletIsActive == false)
{
bulletIsActive = true; //set bool bullet to true and draw the bullet
Bullet.Location = new Point(playerX, playerY); //set bullet location at picturebox
}
break;
那我有Paaikkuna_paint(形式明显)
private void Paaikkuna_Paint(object sender, PaintEventArgs e)
{
if(bulletIsActive == true) //if bulletIsActive == true (Which happens when I press space, draw bullet)
{
e.Graphics.DrawRectangle(Pens.Blue, Bullet);
}
一切正常,当我按 space 并将 "tower"(图片框)移动到任一侧时,矩形就会在那里。
但是问题来了
我的想法是使用 timer_tick 移动子弹,代码如下所示。它应该在每个计时器滴答声中移动 Bullet -10Y,但相反,它只是消失了。
private void timer1_Tick(object sender, EventArgs e)
{
int bulletY = Bullet.Location.Y;
int bulletX = Bullet.Location.X;
if (bulletY>-10 && bulletIsActive == true) //checks if Bullet.Location.Y is >-10 (if not, it's not on screen and it can be "killed") and also if bullet == true
{
Bullet.Location = new Point(bulletX, bulletY - 10);
}
else
{
bulletIsActive = false;
}
}
TL;DR 矩形 "disappears" 即使坐标表明它应该在图片框上方的屏幕上向上移动。
我真的不知道为什么会这样,已经尝试了很多东西。
这里有完整代码http://pastebin.com/WVpLzxUT(如果你想看得更清楚)
如果 Bullet
是一个 Rectangle
并且您希望它在每个 Tick
的新坐标上绘制,您需要 Invalidate
Control
它应该画在:
private void timer1_Tick(object sender, EventArgs e)
{
int bulletY = Bullet.Location.Y;
int bulletX = Bullet.Location.X;
//checks if Bullet.Location.Y is >-10
//(if not, it's not on screen and it can be "killed")
//and also if bullet == true
if (bulletY > -10 && bullet == true)
{
Bullet.Location = new Point(bulletX, bulletY - 10);
Paaikkuna.Invalidate(); // <-- trigger the paint event!
}
else
{
bullet = false;
}
}
顺便说一句:您应该 1) 更好地命名它,例如 bulletIsActive,而不是 if (bullet == true)
和 2) 使用它具有的数据类型 bool
:if (bulletIsActive)
所以我想做一个迷你 "tower defense" 游戏。 现在,我目前的问题是我无法将图片框设置为 "shoot" 矩形 "bullet"
我在开头声明了这些变量
Rectangle Bullet = new Rectangle(225, 400, 10, 25); //position of this shouldn't matter since it's not drawed instantly and it's later moved moved under the picturebox.
private bool bulletIsActive = false;
private int update_speed = 250;
此外,定时器被声明为 AFTER InitializeComponent();
InitializeComponent();
timer1.Interval = update_speed;
timer1.Start();
我有用于按键的事件处理程序,而且我有(只包括 Keys.Space 因为其他的无关紧要。
private void Paaikkuna_KeyDown(object sender, KeyEventArgs e)
{
case Keys.Space:
if(bulletIsActive == false)
{
bulletIsActive = true; //set bool bullet to true and draw the bullet
Bullet.Location = new Point(playerX, playerY); //set bullet location at picturebox
}
break;
那我有Paaikkuna_paint(形式明显)
private void Paaikkuna_Paint(object sender, PaintEventArgs e)
{
if(bulletIsActive == true) //if bulletIsActive == true (Which happens when I press space, draw bullet)
{
e.Graphics.DrawRectangle(Pens.Blue, Bullet);
}
一切正常,当我按 space 并将 "tower"(图片框)移动到任一侧时,矩形就会在那里。 但是问题来了
我的想法是使用 timer_tick 移动子弹,代码如下所示。它应该在每个计时器滴答声中移动 Bullet -10Y,但相反,它只是消失了。
private void timer1_Tick(object sender, EventArgs e)
{
int bulletY = Bullet.Location.Y;
int bulletX = Bullet.Location.X;
if (bulletY>-10 && bulletIsActive == true) //checks if Bullet.Location.Y is >-10 (if not, it's not on screen and it can be "killed") and also if bullet == true
{
Bullet.Location = new Point(bulletX, bulletY - 10);
}
else
{
bulletIsActive = false;
}
}
TL;DR 矩形 "disappears" 即使坐标表明它应该在图片框上方的屏幕上向上移动。
我真的不知道为什么会这样,已经尝试了很多东西。
这里有完整代码http://pastebin.com/WVpLzxUT(如果你想看得更清楚)
如果 Bullet
是一个 Rectangle
并且您希望它在每个 Tick
的新坐标上绘制,您需要 Invalidate
Control
它应该画在:
private void timer1_Tick(object sender, EventArgs e)
{
int bulletY = Bullet.Location.Y;
int bulletX = Bullet.Location.X;
//checks if Bullet.Location.Y is >-10
//(if not, it's not on screen and it can be "killed")
//and also if bullet == true
if (bulletY > -10 && bullet == true)
{
Bullet.Location = new Point(bulletX, bulletY - 10);
Paaikkuna.Invalidate(); // <-- trigger the paint event!
}
else
{
bullet = false;
}
}
顺便说一句:您应该 1) 更好地命名它,例如 bulletIsActive,而不是 if (bullet == true)
和 2) 使用它具有的数据类型 bool
:if (bulletIsActive)