Jump to content
  • Advertisement
Sign in to follow this  
banhminds

OOP problem

This topic is 4104 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 creating a simple 2d game, and right now it's not being simple. Right now I'm trying to create a meter for strength and combos for the game. What I thought I created was a OO class that could create multiple objects of the meter class; however, when I put all this code together I only saw 1 meter on my screen when I needed to see 2. Here's the code.
// constructor
    m1_strength = new Meter(15, 15, 100, 5, Color.red, 0, 0, 0, 0, m1_strength.Combos[0]);
m1_special = new Meter(15, 25, 100, 5, Color.blue, 0, 0, 0, 0, m1_special.Combos[1]);

//draw this stuff on the screen
public void paint(Graphics g)
{
     gfx.setColor(myGray);
                gfx.fillRect(0, 0, getSize().width, getSize().height);
                gfx.drawImage(logo, getSize().width / 2 - 150, getSize().height / 2 - 100, this);
                m1_strength.paintMeter(gfx);
                m1_special.paintMeter(gfx);
}



Now here is the meter class that defines what the width, color, and draws the meter.

/**     Meter Class 
 *Date: 05-25-07 
 */

import java.awt.*;

public class Meter
{
    //Meteor vars
    private static int x,y;
    private static int width;
    private static int height = 5;
    private static int hit_count, combo_count, energy, health;
    private static Color color;
    private static String combo;
    private Ninja ninja;

    //Hidden vars to check to see what's up!
    public static final String[] Combos = {"Strength","Combo Meter","Insane","+1","+2","+3","+5","+10","+20","+30","+50","+100"};
    
    //const that don't change
    private static final int b_width = 200;
    private static final int b_height = 5;
    private static final Color b_color = Color.white;
    
    public Meter(Ninja ninja, int x, int y, int width, int height, Color color, int hit_count, int combo_count, int energy, int health, String combo)
    {
        this.ninja = ninja;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
        this.hit_count = hit_count;
        this.combo_count = combo_count;
        this.energy = energy;
        this.health = health;
        this.combo = combo;
    }
    
    public void GetWidthMeter()
    {
        width = ninja.Health * 2;
    }
    
    public void getString()
    {
        
    }
    
    public void paintMeter(Graphics g)
    {
        g.setColor(b_color);
        g.fillRect(x, y, b_width, b_height);
        g.setColor(color);
        g.fillRect(x, y, width, height);
        g.drawString(combo, x+3, y-1);
    }
}



However, it's only drawing the last meter, and not both of strength, and special meter that I thought would be created. How Come? I should also say that I first made this class an abstract class, and another class that was a child of this class, and created instances of the child class of the Meter class that I called TheMeter, but I only got the same results.

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if this will fix your problem, but I noticed that your constructor

public Meter(Ninja ninja, int x, int y, int width, int height, Color color, int hit_count, int combo_count, int energy, int health, String combo)

takes 11 arguments in the parameter, and you're only providing 10 when you initialize them:

m1_strength = new Meter(15, 15, 100, 5, Color.red, 0, 0, 0, 0, m1_strength.Combos[0]);

It looks like you're omitting the Ninja ninja. Could this be a problem?

Share this post


Link to post
Share on other sites

public class Meter
{
//Meteor vars
private static int x,y;
private static int width;
private static int height = 5;
private static int hit_count, combo_count, energy, health;
private static Color color;
private static String combo;
...


All instances of your Meter share the same x/y/... because these variables are all declared static.

Of course, I have no freakin' clue why a Meter has it's own energy and health and combo mechanism and.... Shouldn't these be replaced by asking the `Ninja ninja' which you have in your class as well what it's energy/health is when you need it? And the combos not be put into a meter which has nothing to do with combos?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!