Advertisement

Figured out the problem, need help with fixing it (continuation of previous post)

Started by April 08, 2016 03:01 PM
1 comment, last by null; 8 years, 8 months ago

So, a while ago, I had a problem with rendering multiple player objects (which eventually spiraled into me failing, as seen here: http://www.gamedev.net/topic/677595-i-need-help-with-having-multiple-player-objects-please/

I'm honestly not sure if this counts as spam (I read the guidelines but it just says 'don't spam' :P), but in the last thread, I posted something without multi-quoting it so I don't think anyone realized... Just thought I would ask in a separate thread.

Read my first problem in the link provided above, because this is a continuation of it:

Alright, so I was recreating the issue in a sample class, and I realized something.

So, basically, in my game, I have collision. Since i'm new to programming, my players are rectangles and I'm using the 'intersects' method to detect collision. To do that, in my player initialization, I create a new rectangle with correlating coordinates to the one in the parameters. If I DON'T do that, I can render more than 1 player with no problem. BUT, I can't have a collision system like that which is kind of important. Plus, I've seen tutorials where people do the EXACT same thing with the rectangles yet they don't have any problems.

This is kind of what the initialization looks like (not creating the instance of the player, but how it's rendered):

NOTE: This is an example and not from my actual game, but it's accurate (no formatting either).

This is the initialization in the player class:

public Player(int x, int y, int width, int height, Color playerColor) {

this.x = x;

this.y = y;

this.width = width;

this.height = height;

this.whatevercoloriscalled = playerColor;

player = new Rectangle(x, y, width, height);

}

This is my draw method in the player class:

public void draw(Graphics g) {

g.setColor(whatevercoloriscalled);

g.fillRect(player.x, player.y, player.width, player.height);

}

This is how I create an instance of the player in my Main class:

Player player1 = new Player(---, ---, ---, ---, ---);

Player player2 = new Player(---, ---, ---, ---, ---);

public void draw(Graphics g) {

player1.draw(g);

player2.draw(g);

....

}

If I DON'T create a new rectangle in the player initialization in the Player class, it works just fine. But in the above example, it doesn't render more than 1.

I apologize for the inevitable facepalming many will do because of this, but I can't figure it out yet XD

player = new Rectangle(x, y, width, height);
The interesting thing is this line. What is "player" here?

g.fillRect(player.x, player.y, player.width, player.height);
You use it in draw.

If "player" is a data member, it should work, but it doesn't, so I am going to guess it's a global variable. If that is the case, both "new Player()" calls write to the same global variable, and the second player wins, since you cannot store more than one rectangle in it.

I am also wondering why you don't use "this.x, this.y, this.width, this.height" in the draw code, since you store that data in the player.

Advertisement

player = new Rectangle(x, y, width, height);
The interesting thing is this line. What is "player" here?

g.fillRect(player.x, player.y, player.width, player.height);
You use it in draw.

If "player" is a data member, it should work, but it doesn't, so I am going to guess it's a global variable. If that is the case, both "new Player()" calls write to the same global variable, and the second player wins, since you cannot store more than one rectangle in it.

I am also wondering why you don't use "this.x, this.y, this.width, this.height" in the draw code, since you store that data in the player.

This fixed my problem. Upvoted.

I know it may seem extremely dumb to you, but this really helped me. Thanks.

EDIT: What the heck? I just undid what I thought fixed it but it still works... Thanks though!

EDIT 2: I'm a retard. I made the 'player' rectangle static. All my code before would have worked perfectly fine if I hadn't made 'player' static... UUUGH. So yeah, you definitely helped.

This topic is closed to new replies.

Advertisement