Rectangle.Contains方法的问题

时间:2011-04-15 14:33:29

标签: c# winforms contains

我建立了一个太空入侵者游戏,问题是,命中敏感度过于敏感。以下是我如何追踪玩家是否被击中。

 bool playersWasShot=false;
 foreach (var shotsInvader in Invadershots)
 {    // below is the Area representing the image size. and the location of the invaders shot
      if(playership.Area.Contains(shotsInvader.Location))
      {
           //player was shot game over.
           playersWasShot=true;
           break;
      }
 }

PlayerShip类的区域属性:

// move simply updates the position of the ship on the x-axis as i move the ship.
// as you can guess. the second variable represents the size of the picture

Area =new Rectangle(new Point(move,900), Properties.Resources.player.Size); 

Shots类中的镜头位置属性正在更新,因为镜头移动到 玩家..

// the shot, travels to the player on the y-axis only
Location=new Point(Location.X, invaderShotLocation);

我在游戏结束时检查了调试器......这就是我得到的:

这一行:

if(playership.Area.Contains(shotsInvader.Location))
{
//i put a debugging point inside this if statment!!!
}

表示船上的信息:

player Area {X=90,Y=900,Width=54,Height=33}
Location    {X=60,Y=900}

表示入侵者的信息:

Location {X=140,Y=900}

镜头甚至没有进入球员的船内..来吧

        public static Bitmap SHIP = Properties.Resources.player;
    public Point Location;
    public int move=10;
    public Rectangle Area { set { } get { return new Rectangle(new Point(move, 900), Properties.Resources.player.Size); } }

    public void Draw(Graphics g)
    {

这是船舶类中控制所有运动和绘图的两种方法

        if (game.Alive) //checks if the player is alive.
        {
             // draws the SHIP picture. 900 is a fixed field..thats where the ship is
             // it is on the y axis that never changes.. x-axis do change..depending
          // on where i move the ship to.
                 g.DrawImage(SHIP, move, 900); 
                Location = new Point(move, 900); 
        }
        else
        {

            g.DrawImage(SHIP, move, 900, SHIP.Width, SHIP.Height);
            Location = new Point(move, 900);
        }
    }



    public void Move(Direction d)
    {

        switch (d)
        {

//按下箭头指示向左或向右移动时传递枚举                 case Direction.LEFT:move - = 10;打破;                 case Direction.RIGHT:move + = 10;打破;             }             Area = new Rectangle(new Point(move,900),SHIP.Size);
        }

注意=当玩家被玩家击中时应用相同的逻辑,并且只有当玩家击中他时,退出者才会消失

在Shot类中发现了Bug。球员和入侵者分享:

..我发现了这个错误。这是在镜头课上。我现在修改了它。问题已经解决

2 个答案:

答案 0 :(得分:3)

似乎射击确实进了船。镜头位于(140,900),船舶的区域角落位于(90,900)和(144,900)。 (140,900)就在那里。

在船舶区域列表之后列出的“位置{X = 60,Y = 900}”部分是什么?您是否可以将船舶绘制在与命中测试区域不同的位置?

答案 1 :(得分:1)

听起来你PlayerShip.Area.Contains有一个错误。仔细检查逻辑。