碰撞系统不起作用
我正在和一个朋友一起制作一个小游戏。现在,我有一个相机系统,我基本上只是改变环境对象的 x 和 y 值。示例:
public void move(){
if(player.up){
enemyposy += player.speedY;
hostageposy += player.speedY;
grassy += player.speedY;
ammoboxy += player.speedY;
healthkity += player.speedY;
helipady += player.speedY;
bushy += player.speedY;
}
当您按下向上按钮时,这将向下移动所有这些对象,因此会产生玩家正在移动的错觉,但事实并非如此。现在,我尝试通过这样做创建一个碰撞系统:
public void checkCollision(){
if(player.getBounds().intersects(enemy.getBounds())){
System.out.println("Colliding with Enemy");
healthdown.start();
}else{
healthdown.stop();
}
但是,当我走向敌人并触摸它时,它不会像触摸一样做出反应,它并不是说“与敌人碰撞。”,但是我在玩家。有人可以告诉我为什么它没有响应,并给我一个解决方案吗?
getBounds方法:
public Rectangle getBounds(){
return new Rectangle(getX(),getY(), 20, 20);
}
和玩家的getBounds方法一模一样。
intersects 是一个已经实现的方法。
boolean java.awt.Rectangle.intersects(Rectangle r)
确定此 Rectangle 与指定的 Rectangle 是否相交。如果两个矩形的交集非空,则它们相交。
参数: r 指定的矩形 返回: 如果指定的 Rectangle 与此 Rectangle 相交,则为 true;否则为假。
I am making a small game, with a friend. Now, I have a camera-system, I am walking by basicly just changing x and y values of environment objects. Example:
public void move(){
if(player.up){
enemyposy += player.speedY;
hostageposy += player.speedY;
grassy += player.speedY;
ammoboxy += player.speedY;
healthkity += player.speedY;
helipady += player.speedY;
bushy += player.speedY;
}
This will move all those objects, down when you press the up button, so it creates an illusion that the player is moving, which it isn't. Now, I tried and create a collision system by doing this:
public void checkCollision(){
if(player.getBounds().intersects(enemy.getBounds())){
System.out.println("Colliding with Enemy");
healthdown.start();
}else{
healthdown.stop();
}
But, when I go to the enemy, and touch it, it won't react like it's touching, it's not saying "Colliding with Enemy.", however I AM in the player. Can somebody perhaps tell me why it's not responding, and give me a solution on how to solve it?
getBounds method:
public Rectangle getBounds(){
return new Rectangle(getX(),getY(), 20, 20);
}
player getBounds is exactly the same.
intersects is an already implemented method.
boolean java.awt.Rectangle.intersects(Rectangle r)
Determines whether or not this Rectangle and the specified Rectangle intersect. Two rectangles intersect if their intersection is nonempty.
Parameters:
r the specified Rectangle
Returns:
true if the specified Rectangle and this Rectangle intersect; false otherwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您显示的代码中,您没有更新player.x,因此player.getX不会更改。对于敌人也一样:)
In the code you show you don't update player.x so player.getX won't change. The same for enemy :)