Jump to content

Doing a fighting game, would like to have transparent hitbox. (JComponent) anyone knows the line/got an idea?

 

Google's not giving anything at the moment, must not be looking for the right thing, only find something for COLOR.

 

Edit: Silly me, I'm using Netbeans.

Link to comment
https://linustechtips.com/topic/18430-trying-to-do-transparent-jcomponent/
Share on other sites

Link to post
Share on other sites

Hmm, make a transparent color?

Color transColor = new Color( 0, 0, 255, 12 );  f.setBackground(transColor); 

EDIT: You can also use:

setOpaque(false);

http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JComponent.html

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

Problem is that I'd like doing that with predefine component not just custom one.

Like when the characters get hit.

Can we get some code? Then its much more easy to do :)

 

Also would it not be better to make a collision detection system, by detecting if the two chars hit each others?

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

public class CharacterIcon extends JComponent{    private Image img;    private int w, h;    private boolean P1 = true;    private boolean P2 = true;    private JLabel lblP1 = new JLabel("P1");;    private JLabel lblP2 = new JLabel("P2");    public CharacterIcon(int height, int width, String i)    {       w = height;       h = width;       img = Toolkit.getDefaultToolkit().getImage(i);       setTextColor(Color.RED, Color.BLUE);       add(lblP1).setSize(100, 100);       lblP1.setLocation(5, 0);       add(lblP2).setSize(100, 100);       lblP2.setLocation(w-19, 0);    }    @Override    public void paintComponent(Graphics g)    {       super.paintComponent(g);       g.drawImage(img, 0, 0, w, h, this);       lblP1.setVisible(P1);       lblP2.setVisible(P2);    }    protected void setTextColor(Color c1, Color c2)    {       lblP1.setForeground(c1);       lblP2.setForeground(c2);    }}

Working on the labels too ATM.

Link to post
Share on other sites

Why don't you calculate if there is collision between the "CharacterIcon" (assuming they are the players).

 

You can read about it here: http://zetcode.com/tutorials/javagamestutorial/collision/

 

Also you should read about game rendering as you are just painting (which will mess the FPS up).

 

What makes or breaks a JAVA game is how you render the game and how your gameloop is.

 

Here is a MUST READ article for those who create a "custom" engine: http://dewitters.koonsolo.com/gameloop.html

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

That's just a Icon not the actual player, trying not to post to much of the code as it's an assignment, sorry for confusion.

But the Character "rendering" work on the same idea.

 

 

Well first of all make a good game loop (i think the teacher will be happy about that) the problem with what you do (you probably use the Thread.sleep(x); method) is that faster computers will run the game faster then slower ones. This is not only the FPS that is higher, but the actual game. So on fast systems a character will move faster then on a slower system, not good when you want consistency over different systems and the game should just spit out more FPS not calculation about game behavior. (old games used to do this as hardware was pre-defined: NES, ps1, ps2 etc.)

 

So make a good gameloop. And for the collision detect calculate whether the two "characters" overlap each others  if true there is a hit and you can deal damage. (maybe calculate where it is head-shot etc.)

 

But my advice is to make a good gameloop before you continue with anything!

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

Have to go for 1½ hour but then I can post a previous assignment, threads were broken for some reason but you can still play one game correctly.

 

Dude, the issue is not the Threading, but your gameloop. You just spit out frames and game behavior calculations as fast as possible  Frames should only be done this way (so higher end GPU's get higher fps) however ghame behavior (char. movement etc) should be at a fixed speed.

 

You need to do something like:

    const int TICKS_PER_SECOND = 60;    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;    const int MAX_FRAMESKIP = 10;    DWORD next_game_tick = (int)System.currentTimeMillis();    int loops;    bool game_is_running = true;    public void gameLoop()    {        while(game_is_running)         {            loops = 0;            while((int)System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP)             {                update_game(); // do game logic and game behavior here (char movement, collision detection etc.                next_game_tick += SKIP_TICKS;                loops++;            }            render_game(); // add your "paintComponent(Graphics g)" here or make a function that calls all your render stuff.    }    }

Instead of:

    public void paintComponent(Graphics g)    {       super.paintComponent(g);       g.drawImage(img, 0, 0, w, h, this);       lblP1.setVisible(P1);       lblP2.setVisible(P2);    }

As stated before read more about it here: http://www.koonsolo.com/news/dewitters-gameloop/

 

Maybe its not part of the current assignment, however this is something that is very important if you wish to create a engine for your game. I think its essential in creating a game in JAVA to have a good gameloop.

 

EDIT: the best thing however would be to use interpolation, however you can start using that once this method sinks in :)

 

EDIT2: added some things in the code so its good2go

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

Saw your code you PM'ed me.

 

All the:

public void paintComponent(Graphics g)     {        super.paintComponent(g);        g.drawImage(img, 0, 0, null);     } 

is VERY BAD, as i stated above... Please read all the post i made before as they will help you.

 

Your code is also.... Very basic and you need to get the fundamentals right before you create something else :)

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

In four weeks I'm deserting to C++ and CUDA never to return again. (probably)

Is the idea also valid for C++?

 

 

Yes, it is valid in all coding languages where you want to create a custom engine.

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×