私のプログラムの中で、どのような矢印キーが押されているかに応じてオブジェクトを左右に動かしたいと思います。私は2つの矢印キーのいずれかが押されたときに、底に沿って画像ボックスを移動させました。しかし、何が起こっているかは、いずれかのキーを押すと、画像ボックスが上部に移動し、そこを左右に移動します。なぜこれがわからないのですか?私のピクチャボックスがフォームの上部に移動するのはなぜですか?
Form1のコードは次のとおりです。Form2のコードは無視してください。それは、今の実験の目的のためです:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Move
{
public partial class Form1 : Form
{
public int lives = 0;
Form2 menu = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int i;
for (i = 0; i < 500; i++)
{
if (e.KeyCode == Keys.Left)
{
pictureBox1.Location = new Point(pictureBox1.Left - 1);
Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
if (e.KeyCode == Keys.Right)
{
pictureBox1.Location = new Point(pictureBox1.Left + 1);
Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
var rect1 = new System.Drawing.Rectangle(pictureBox1.Location, pictureBox1.Size);
var rect2 = new System.Drawing.Rectangle(pictureBox2.Location, pictureBox2.Size);
if (rect1.IntersectsWith(rect2))
{
MessageBox.Show("Game Over!");
System.Threading.Thread.Sleep(1000);
Application.Exit();
}
if (e.KeyCode == Keys.Down)
{
this.Hide();
menu.Show();
}
}
}
}
}
ポイントは通常2つの引数をとります。秒を渡さないと、明らかにデフォルトは0になります。y = 0はウィンドウフォームの一番上です。あなたの新しいポイントを 'new point(pictureBox1.Left +/- 1、pictureBox1.Top)'に変更してください。 –