【问题标题】:JAVA: 2D Game Adventure. Collision Detection problemJAVA:2D 游戏冒险。碰撞检测问题
【发布时间】:2011-06-11 18:20:02
【问题描述】:
这是关于 JAVA 中的动画。在所有图片上使用相同尺寸时我取得了成功。但是,如果我将所有图片尺寸保持在相同的尺寸(宽度和高度)上,我会在玩家打孔时遇到一些错误。在玩家的手接触敌人身体之前,敌人已经死亡
但在我的情况下,闲置、奔跑和打孔的其他人则不同
方面。面向左侧的打孔动画变得非常奇怪。
如果他的手向左打,但他的身体向右移动。这个
是因为我画的x&y是一样的。
我该如何解决?需要说明:D
我使用 png 因为支持透明
我认为这可以通过 2 个选项来解决
1.修复我的碰撞检测
2. 修复我的图片在某些情况下的绘制位置
【问题讨论】:
标签:
java
collision-detection
collision
adventure
【解决方案1】:
试图描绘您的问题,希望这会有所帮助。
我是直接从脑海中输入的,所以代码中可能有错误
-
修复联合缺陷
我会试试这个
Image fist
Image enemy
//in paint
g2D.drawImage(fist,x,y,this);
g2D.drawImage(enemy,x1,y1,this);
Rectangle2D myFist = new Rectangle2D.Double(x,y,fist.getWidth(this),fist.getHeight(this));
Rectangle2D myEnemy = new Rectangle2D.Double(x1,y1,enemy.getWidth(this),enemy.getHeight(this));
if (myEnemy.contains(myFist){
//action u want to happend
}
我认为这样的事情应该可以解决联合问题
我认为这是马里奥在世嘉上的游戏
-
绘制位置的修正
//arm image can be the same image if u want
Image leftArm;
Image rightArm;
image headLegsAndTorsoLeft;
image headLegsAndTorsoRight;
//where am i looking in game if true i look to the leftside of user thats playing
boolean turnedLeft
//in paint
if(turnedLeft){
//this lets it look like he is turned to the left with his right arm in the visible behind his left.
//draw right arm
g2D.drawImage(rightArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoLeft,x-3,y,this);
// draw left arm a bit more in x coor
g2D.drawImage(leftArm,x-6,y,this);
}else{
//this lets it look like he is turned to the right with his left arm in the visible behind his right.
// draw left arm
g2D.drawImage(leftArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoRight,x-3,y,this);
//draw right arm a bit more in x coor
g2D.drawImage(rightArm,x-6,y,this);
}
手臂动画的顺序相同,最终我会为躯干、左臂、右臂使用不同的方法动画
像 keypressed leftarrow 躯干这样的东西会向左移动动画,点击左臂键移动左臂,点击右臂键移动右臂,让我们说左臂是 3,现在当你的 char 向右移动时,你需要另外 3 个。
这就是我尝试做事的方式。