Jump to content
  • Advertisement
Sign in to follow this  

[java] 2D game classes

This topic is 4606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a simple 2d game, and I have a basic sprite class that manages 1 image(or animation) and the velocity and position of a sprite. The player in my game is made up of different body parts, each which its own image, since each part needs to rotate and such (for example, the arm rotates to follow the cursor). Right now I'm putting each body part in a seperate class as a seperate sprite, and creating a player class that just links them together. Is this method okay? Or should I just have one player sprite that handles all the body parts and draws them in one class? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Well, personally a Sprite is a sprite and shouldn't be extended beyond that basic functionality. I think if you wanted to create a single class to deal with it, you'd make a hierarchical structure or a bone-sprite or something else that specificially sets it apart from a 'normal' sprite.

As a side note, its probably better to have your sprite class independant from the actual image data, as you might want to have multiple instances of the same image running around at the same time. Thus, to conserve memory, your sprite would have a pointer to the image rather than the image itself.

On a final note, I usually find its good to make a distinction between graphical elements and game elements, especially for code-recycling purposes. It might be a good idea to create a "game object" class which either contains a sprite or is derived from Sprite to do what you need to do. Separation of the game logic from graphics code not only makes cleaner code, but is the first step to API abstraction.

Good Luck!

Share this post


Link to post
Share on other sites
What Mushu wants to say is that the image data is loaded only once in your app, and shared as a reference between all sprites who use it.

A GameObject instance is the derivation of a series of abstractions in your code, but still represented as one basic game object, with only the basic "functions" available to it. Let me try to make it simpler... If your game has many types of actors, but all of them use similar patterns of movement, animation and game-logic actions, you should group them in subclasses of GameObject.

GameObject obj = new Player();
// you have a player instance, but you can only access functions
// available in the GameObject class/interface

If you must animate your body parts separately, them I'm sure they're all sprites who are bound togheter. So, your "Player" class is not an extension of Sprite, but a composition of many sprites: one for the body, two for the arms (in case it is human, I mean =)... Separating your logic (the player) from the graphical aspect (sprite) is what Mushu advised you to do.

Son Of Cain

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!