0

I am making a 2d platformer game that takes in several premade images of "levels" and randomly inserts them into a list. The game never ends so it just keeps adding more images as needed.

I am having a problem with the character moving between the images because when the guy reaches the end of one image, it throws:

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.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.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)

But my picture is 1000 by 600 pixels wide and it says it goes out of range at 1000,413.

This is the code for the section with the error:

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;
    }
}

I have searched the internet for any form of help but they only talk about how to insert gravity or use box collision. I have the gravity but do not understand box collision. That is why I am using colour. Any help would be greatly appreciated

Thank you

1 Answer 1

1

Images are just like 2D arrays. The position (1000,413) IS outside the image, since indices start at 0 and go up to 999. When getting a color at a position, make sure the position is less than the width or height of the image, not less than or equal.

In 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;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

So you mean position < limit rather that position <= limit, correct?
@JamesSmith yes, assuming position is the x or y value you are checking and limit is the width or height of the image
@Vincent Thank you for the help but I do not know how to modify the code to do this. can you please clear this up?
@Collin I will add some code to my answer to clarify
@Vincent Sorry to bug you again but I found it. That was part of my problem and my math was off on how to find the active image
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.