开发者

Making an Image a "boundary"

I am working on a game in which you are a simple circle that fires bullets and its multiplayer and so on. Well, I am trying to make boundaries sort of like a maze type thing that u have to go through I have tried collision detection like this:

public void checkCollisions(){
    Rectangle r1 = bo.getBounds();
    Rectangle d = p.getBounds();
    if (d.intersects(r1))
        border = true;
}

And basically if border = true then i stop the character from moving. I have 2 problems when i do this,

  1. He doesnt stop just goes REALLY slo开发者_开发问答w.
  2. He stays at the REALLY slow state even off the border.

I use border like this:

boolean border = false;

then in my paint method i state this:

if (border)
        p.dx = 0;
        p.dy = 0;

p represents the Guy class :P More of the dx and dy:

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_A)
    dx = -2;

if (key == KeyEvent.VK_D)
    dx = 2;

if (key == KeyEvent.VK_W)
    dy = -2;

if (key == KeyEvent.VK_S)
    dy = 2;

and for keyReleased i just change the value of dx and dy to 0 also for how the guy moves:

public void move() {
x = x + dx;
y = y + dy;
}

Please help me figure out why this isn't working.


OK, I still think a full restructuring of your game logic is in order, but I think I can shed light as to what's going on. Let's look at the various places where things are happening:

  1. PAINT: On the Swing thread, when paint() is called, you see if there were collisions and if so zero out the speeds (assuming you fix that if block).
  2. KEY: On the Swing thread, when a key is pressed, you set the speed according to the key pressed.
  3. CHECK: At some unknown point, you check for collisions and record whether there was one.
  4. MOVE: At some unknown point, you update your "guy's" position with the speed.

So here's the problem: in Java, just like any other program, you get multiple key pressed events when you're holding down a key. There will be a short delay between the first and second, and then they will repeat rapidly. Try it in a text box in your browser, the same behaviour occurs there.

So how does that affect you? Well, you're probably getting into a scenario like this:

PAINT -> speed set to zero
KEY -> speed set back to -2
MOVE -> guy is moved -2
CHECK -> border = false
PAINT -> speed set to zero again

Really, if you restructure the code so that you get a game loop that looks something like this:

public void runGame() {
    while(true) {
        updateSpeeds();
        updatePositionFromSpeed();
        repaint();
    }
}

Where updateSpeeds() would instead query whether the key is down or up and also compute whether the guy could move in that direction, and updatePositionFromSpeed() would update the guy's position. Then paint() would rely only on the guy's x and y coordinates, would not write to them, and would not need to know about the speed.


here's a very easy solution.

Here's a bit of my pseudo code.

        if(player.getBounds().intersects(wall.getBounds())){
            //Go Back to prior position, regardless of direction coming from. Since the reverse velocity X and velocity Y directions are taken care off
            x -= velX;
            y -= velY;

            //Then Stop at that prior position to make next move
            velX = 0;
            velY = 0;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜