【问题标题】:boundary codes - Java [closed]边界代码 - Java [关闭]
【发布时间】:2012-11-27 10:18:58
【问题描述】:

我正在编写游戏代码,我需要帮助的是我将对象放在地图上并尝试制作它,以便玩家无法穿过对象。n 问题是 x 边界有效,但 y 无效,我不知道如何继续。

 public Boolean checkPathClear(int x, int y){
     xPPos=x;
     yPPos=y;
     int tX1=350-50-xPos,tX2=450-xPos,tY1=200,tY2=300;
     if ((xPPos>=tX1)&&(xPPos<=tX2)&&(yPPos>=tY1)&&(yPPos<tY2))
       return(false);
     else
       return(true);
   }
   public void paintComponent(Graphics g){    
     g.drawImage(imgTurret,350-xPos,200,450-xPos,300,0,0,126,110, this);

     g.drawImage(imgPlayer,xPPos,yPPos,xPPos+50,yPPos+50,xPFace,yPFace,xPFace+50,yPFace+50, this);
       }
 }
 //xPPos is 200 at the start.
 //yPPos is 200 at the start.
 //xPos is 0 at the start.
 //xPFace, yPFace is just the direction the player is facing so it doesnt matter

我编辑了代码,所以人们不需要查看所有内容:)

如果有人可以帮助我,我将不胜感激:D

编辑:对于任何想查看我在其上实现代码的人。当您向右移动时,图像显示向左移动,因此看起来屏幕在向右移动。

【问题讨论】:

  • 我现在能给你的最好建议可能是写出清晰、简洁的问题。基本上,这个问题的反面。

标签: java boundary


【解决方案1】:

您的图片是drawn,尺寸:

imageX: 350-xPos,
imageY: 200,
width: 450-xPos,
height: 300

所以:

return !(imageX <= x && x' < imageX + width
    && imageY <= y && y' < imageY + height);

【讨论】:

  • 我不明白这一点,对不起,我不是最擅长 java 的。为什么在 x 之前有 '?
  • x = x + 50. Sorry, I was entirely abstracting. It looks a bit, like you think drawImage's width` 和 height 参数分别为 right-xbottom-y。所以我去重新制定所有的。
猜你喜欢
  • 2012-06-01
  • 2014-10-06
  • 1970-01-01
  • 2010-11-26
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多