Java 颜色碰撞检测
Java colour collision detection
我正在制作一个 2d 平台游戏,它接收 "levels" 的几张预制图像并将它们随机插入到列表中。游戏永无止境,因此它只会根据需要不断添加更多图像。
我的角色在图像之间移动时遇到问题,因为当这个人到达一张图像的末尾时,它会抛出:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:918)
at GamePanel.fall(run.java:197)
at run.actionPerformed(run.java:41)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
但是我的图片是 1000 x 600 像素宽,它说它超出了 1000,413 的范围。
这是错误部分的代码:
public void getActImg(){
int tmp=0;
for(int i=0;i<inUse.size();i++){
tmp+=inUse.get(i).getWidth();
System.out.println(imgPix);
if(tmp/imgPix>=0){//int divide by width of all the pictures to find current img
actImg=inUse.get(i);
System.out.println(i);
break;
}
}
}
public void fall(){
if(posY+foot>getHeight()){
die();
}
System.out.println((totDist+posX)%imgPix+","+(posY+foot));
if(actImg.getImg().getRGB((totDist+posX)%imgPix,posY+foot)==Color.WHITE.getRGB()){
posY+=vy;
vy+=g;
onGround=false;
}
else{
onGround=true;
if(vy>3){
posY-=vy;
}
vy=0;
}
}
我在互联网上搜索过任何形式的帮助,但他们只谈论如何插入重力或使用盒子碰撞。我有重力但不了解盒子碰撞。这就是我使用颜色的原因。
任何帮助将不胜感激
谢谢
图像就像二维数组。位置 (1000,413) 在图像之外,因为索引从 0 开始到 999。在某个位置获取颜色时,确保该位置小于图像的宽度或高度,不小于或等于。
在 public void fall() 中:
int x = (totDist + posX) % imgPix; // NOTE: I am not sure what imgPix is or why you are modding the value by it.
int y = posY + foot;
if (x > 0 && x < actImg.getWidth() && y > 0 && y < actImg.getHeight()) {
if (actImg.getImg().getRGB(x, y) == Color.WHITE.getRGB()) {
posY += vy;
vy += g;
onGround = false;
}
}
我正在制作一个 2d 平台游戏,它接收 "levels" 的几张预制图像并将它们随机插入到列表中。游戏永无止境,因此它只会根据需要不断添加更多图像。
我的角色在图像之间移动时遇到问题,因为当这个人到达一张图像的末尾时,它会抛出:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:918)
at GamePanel.fall(run.java:197)
at run.actionPerformed(run.java:41)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
但是我的图片是 1000 x 600 像素宽,它说它超出了 1000,413 的范围。
这是错误部分的代码:
public void getActImg(){
int tmp=0;
for(int i=0;i<inUse.size();i++){
tmp+=inUse.get(i).getWidth();
System.out.println(imgPix);
if(tmp/imgPix>=0){//int divide by width of all the pictures to find current img
actImg=inUse.get(i);
System.out.println(i);
break;
}
}
}
public void fall(){
if(posY+foot>getHeight()){
die();
}
System.out.println((totDist+posX)%imgPix+","+(posY+foot));
if(actImg.getImg().getRGB((totDist+posX)%imgPix,posY+foot)==Color.WHITE.getRGB()){
posY+=vy;
vy+=g;
onGround=false;
}
else{
onGround=true;
if(vy>3){
posY-=vy;
}
vy=0;
}
}
我在互联网上搜索过任何形式的帮助,但他们只谈论如何插入重力或使用盒子碰撞。我有重力但不了解盒子碰撞。这就是我使用颜色的原因。 任何帮助将不胜感激
谢谢
图像就像二维数组。位置 (1000,413) 在图像之外,因为索引从 0 开始到 999。在某个位置获取颜色时,确保该位置小于图像的宽度或高度,不小于或等于。
在 public void fall() 中:
int x = (totDist + posX) % imgPix; // NOTE: I am not sure what imgPix is or why you are modding the value by it.
int y = posY + foot;
if (x > 0 && x < actImg.getWidth() && y > 0 && y < actImg.getHeight()) {
if (actImg.getImg().getRGB(x, y) == Color.WHITE.getRGB()) {
posY += vy;
vy += g;
onGround = false;
}
}