2017-06-07 9 views
0

が起こって同じことを二つの事象を防ぐためにどのように、私はC#で初心者をしていると私はちょうど基本的なVisual Studioの2015 tutorial.This次のような単純なプラットフォーマーゲームにコードされた作業を開始しました:基本的

public partial class Form1 : Form 
{ 
    bool goleft = false; 
    bool goright = false; 
    bool jumping = false; 

    int JumpSpeed = 10; 
    int force = 8; 
    int score = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void keyisdown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left) 
     { 
      goleft = true; 
     } 
     if (e.KeyCode == Keys.Right) 
     { 
      goright = true; 
     } 
     if (e.KeyCode == Keys.Space && !jumping) 
     { 
      jumping = true; 
     } 
    } 

    private void keyisup(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left) 
     { 
      goleft = false; 
     } 
     if (e.KeyCode == Keys.Right) 
     { 
      goright = false; 
     } 
     if (jumping) 
     { 
      jumping = false; 
     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     player.Top += JumpSpeed; 

     if (jumping && force < 0) 
     { 
      jumping = false; 
     } 
     if (goleft) 
     { 
      player.Left -= 5; 
     } 
     if (goright) 
     { 
      player.Left += 5; 
     } 
     if (jumping) 
     { 
      JumpSpeed = -12; 
      force -= 1; 
     } 
     else 
     { 
      JumpSpeed = 12; 
     } 
     foreach (Control x in this.Controls) 
     { 
      if (x is PictureBox && x.Tag == "p") 
      { 
       if (player.Bounds.IntersectsWith(x.Bounds) && !jumping) 
       { 
        force = 8; 
        player.Top = x.Top - player.Height; 
       } 
      } 

      if (player.Bounds.IntersectsWith(Door.Bounds)) 
      { 
       timer1.Stop(); 
       MessageBox.Show("You Won!"); 
       this.Close(); 
      } 
     } 
    } 
} 

重力とすべてが素晴らしいですが、私は確かに何の助けもなければ解決できない、大きな問題を1人だけプレイヤーに与えます。

問題は次のとおりです。プレイヤーが完全にぎくしゃくしていて、上下に動き続けています。これらの

player.Top += JumpSpeed; 

player.Top = x.Top - player.Height; 

両方 player.Topを変更し、互いに干渉してみてください:私はこれが原因 private void timer1_Tick(object sender, EventArgs e)で2行であることに気づきました。問題は、重力コードを破壊することなく、2つのうちの1つを切り取ることができないことです。私がうまくいくと思うのは、 player.Top += JumpSpeed;のみ if(player.Bounds.IntersectsWith(x.Bounds) && !jumping) = falseを適用していますが、この方法でコードを書く方法はわかりません(以前の試みは常にエラーに終わってしまいました)。

私はネイティブの英語話者ではないため、何か助けを借りて、私が作った文法上の誤りを許してくれてありがとう。

+1

質問を視覚化することは、言葉で説明するよりも優れています。あなたの質問があっても、あなたのコードで作業しようとしました。私は本当にプレイヤーが何であるか分からないのですか?私たちが直接使用できるコードを共有してください。 – Berkay

答えて

-1

私はあなたのフォームがどのように見えるかを知っていますが、私はそのようなコードを変更します提供するものではありません:

public partial class Form1 : Form 
{ 
    bool goleft = false; 
    bool goright = false; 

    int JumpSpeed = 0; //current vertical speed 
    int force = 8; //initial jump speed 
    int score = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void keyisdown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left) 
     { 
      goleft = true; 
     } 
     if (e.KeyCode == Keys.Right) 
     { 
      goright = true; 
     } 
     if (e.KeyCode == Keys.Space && JumpSpeed==0) //JumpSpeed==0 to prevent "double jump" 
     { 
      JumpSpeed = force; //start jumping with full power 
     } 
    } 

    private void keyisup(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left) 
     { 
      goleft = false; 
     } 
     if (e.KeyCode == Keys.Right) 
     { 
      goright = false; 
     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (goleft) 
     { 
      player.Left -= 5; 
     } 
     if (goright) 
     { 
      player.Left += 5; 
     } 

     JumpSpeed -= 1; //decrement the vertical speed = gravity -> choose higher value to bring him back on the ground quicker 
     player.Top -= JumpSpeed; 


     if (player.Bounds.IntersectsWith(Door.Bounds)) 
     { 
      timer1.Stop(); 
      MessageBox.Show("You Won!"); 
      this.Close(); 
     } 


     foreach (PictureBox x in this.Controls.OfType<PictureBox>()) 
     { 
      if (x.Tag == "p") 
      { 
       if (player.Bounds.IntersectsWith(x.Bounds)) //if the player should be able to jump/walk on something the code is 
       { 
        player.Top = x.Top - player.Height; 
        JumpSpeed = 0; //the player stops falling 
       } 
      } 
     } 
    } 
} 

プレイヤーに確認するためでウィンドウの場所PictureBoxesの外に落ちません床にタグ== p!

PS:

コメントをお気軽に!

+0

改善できる点は? (ダウン投票?) – RoJaIt