Advertisement

I need help with having multiple player objects please!

Started by April 02, 2016 03:33 PM
16 comments, last by null; 8 years, 8 months ago

Yes, bugs in your code are absolutely normal. You will find and fix hundreds and many fixes will introduce new ones :) Just get used to it and learn how to debug and find them. Your particular problem is likely something simple that will make you scratch your head in disbelief when you find it, but as NoAdmiral said starting over won't help.

Also Mercurial is in my opinion good starting point to source control.

You still may want to show us some code like your initialization routine or the player class before anyone here can suggest anything.

Oh alright, TortoiseHg is kind of confusing in any case to be honest. I'll look into Mercurial.

And I'll also share my source code once I'm at my computer since I'm on vacation and I'm not using my actual PC.

Thanks for all the replies everyone!

Advertisement


Oh alright, TortoiseHg is kind of confusing in any case to be honest. I'll look into Mercurial.

TortoiseHg is a client for Mercurial (hence the Hg at the end). Any source control will be confusing at first but learning it will go a long way.

null; my advise would be to forget source control at this stage. You're just starting to learn to program, adding in another step for you is just overkill.

These problems are totally normal for someone learning. You'll soon understand what you did wrong, and why, and you'll be a better programmer because of it.

However, if you truly want help, post a small snippet of your code that shows the 2 player objects getting initialized, and the player object's class definitions. Someone on here will be able to help you in a second.

Good luck, and have fun!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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

I really can't tell you what's going on without seeing code. But what happens if you make both players, and only draw the first one? Does it draw in the same spot as when you don't make player 2? Or does it draw player 1 at player 2's position? Or maybe it doesn't draw player 1 at all?

Yeah, it draws player 1 at the same spot as when I don't make player 2. Anyway, it doesn't matter since I screwed stuff up and now my project is just kind of - well, broken I guess. I had an AI path-finding system and health bar and everything :(

That brings me to a follow-up question:

Is failure in a project like this normal?

Yeah, you usually dont make the next cutting edge game easily and you fail often.

The important thing is learning from your errors.

Didnt you use a version control system like Git? I suggest doing so making youre own repo on GitHub if it is open source or GitLab or BitBucket if youre making a proprietary engine.

Hey, have a look at my Telegram channel about programming: www.telegram.me/theprogrammingart
Advertisement

Can you post the whole file that contains the main method? (Consider uploading the code to github or gist but this is some work so you might just want to copy and paste it here in a code block)

Can you post the whole file that contains the main method? (Consider uploading the code to github or gist but this is some work so you might just want to copy and paste it here in a code block)

I fixed my problem :D Turns out I made my rectangle and variables static so it was all writing to one global variable.

This topic is closed to new replies.

Advertisement