Jump to content

  • Log In with Google      Sign In   
  • Create Account


NuLogic

Member Since 17 Mar 2012
Offline Last Active Apr 22 2012 10:45 AM

Topics I've Started

Making action delay on keyPressed

19 April 2012 - 04:42 AM

Hello, I am making fighting java game, and I have one problem, when I press attack key, enemy must respond and also attack player, I know how to do it, but I need to make a short delay between actions, player attack action must be performed immediately, and enemy must attack only after one second, how to make such thing?
Sample code on playerEnemy:


public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_ENTER) {

attack();
}
}

Creating object in external class

11 April 2012 - 08:02 AM

Hello, I am creating Java game, it is my first game, so I am still a noob in that case. I got stuck in one thing:
I have created main menu, after selecting start new game, user must select weapon (ex. dagger with JButton), this is how it looks:


public class GUI {

   public GUI() {
		   initGUI();
   }

public void initGUI {

(..................some methods.........................)

final JButton newGame = new JButton(new ImageIcon(..));
		newGame.setLocation(250, 420);
        content.add(newGame); 

final JButton buttonDagger = new JButton(new ImageIcon(..));
        buttonDagger.setLocation(250, 420);
        content.add(buttonDagger);

class NewGameAction implements ActionListener {
@Override
        public void actionPerformed(ActionEvent e){
        newGame.setVisible(false);
        daggerButton.setVisible(true);   
        frame.add(new Barracks());
   }
}


class DaggerAction implements ActionListener {
			@Override
			public void actionPerformed(ActionEvent e){
			   daggerButton.setVisible(false);
			   Weapon w = new Weapon("Dagger", 3, 8, 5);
			   frame.add(new Arena());
			}
		}
      buttonDagger.addActionListener(new DaggerAction());
      newGame.addActionListener(new NewGameAction());
	 }
}

Class DaggerAction is inside initGUI function, is an initialization of daggerButton, after clicking on that, dagger must be created. So my problem is, how I can reach that Weapon w object? Or maybe there is an option to create it's copy somewhere during DaggerAction initialization? Or maybe there is an other way to initialize such select thigs? I will really appreciate your help, thanks. GUI is the main class which handle all the graphics. And I want to appear weapon object in Barracks class, which is initialized after starting new game.

Creating inGame menu

19 March 2012 - 05:58 AM

Hello, I just started to work with my graphical game, it is simple, but I need some information, how the game menu is created? I have been searching everywhere, but there is only explained how to create menu bars, but I need simple menu like New Game, Options for example in the middle of the screen. This is my GUI class, it contains only background and menu bar on the top, maybe someone can say, how I can implement normal one? :

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public final class GUI {   
    JFrame frame;
   
    public GUI() {	   
	    frame();
    }
// Adding background image and JMenuBar on the top
    public void frame() {
	    frame = new JFrame("My Game");
	    try {
		  frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("D:/bg.png"))))); 
	    }
	    catch(IOException e) { System.out.println("No image"); }
	   
	    frame.setResizable(false);
	    frame.setSize(800, 449);
	   
	    JMenuBar menubar = new JMenuBar();
	    frame.setJMenuBar(menubar);
	    JMenu file = new JMenu("Meniu");
	    menubar.add(file);
	    JMenuItem exit = new JMenuItem("Exit");
	    file.add(exit);
	    JMenu help = new JMenu("Help");
	    menubar.add(help);
        JMenuItem about = new JMenuItem("About");
	    help.add(about);

	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	 
	    frame.setVisible(true); 
	   
	    class ExitAction implements ActionListener {
		    @Override
		    public void actionPerformed(ActionEvent e){
			    System.exit(0);
		    }
	    }
	    class AboutAction implements ActionListener {
		    @Override
		    public void actionPerformed(ActionEvent e){
			    JLabel label;
                frame = new JFrame("About");
			    frame.setResizable(false);
			    frame.setSize(200, 200);
                label = new JLabel("Author: ED");
			    Font font = new Font("Arial", Font.ITALIC, 12);
			    label.setFont(font);
			    frame.add(label);
			    frame.setBackground(Color.BLUE);
			    frame.setVisible(true);
		    }
	    }
	    exit.addActionListener (new ExitAction());
	    about.addActionListener(new AboutAction());
    }
   
}

How I you would add menu? Also I tried to put a text box, but it hides under the background, how to take it from the back?

Implementing Objects with specifications

17 March 2012 - 01:47 PM

Hello, I have class Weapon:

class Weapon {
    private String name;
    private int min_damage;
    private int max_damage;
    private int weight;
   
    public Weapon(String name, int weight) {
	    this.name = name;
	    this.weight = weight;
    }
   
    public int  getMinDamage() { return min_damage; }
    public int  getMaxDamage() { return max_damage; }
   
    public void setMinDamage(int x) { min_damage = x; }
    public void setMaxDamage(int x) { max_damage = x; }
   
}

and I want at beginning of the program to create three weapons like longSword, shortSword, saber, each of it would have parameters min_damage, max_damage, name and weight, so how I need to create them? I am thinking about two ways, first:

Weapon longSword = new Weapon("Long Sword", 10);
longSword.setMinDamage(5);
longSword.setMaxDamage(10);

second:
class longSword extends Weapon{
		  // same actions like upper
}

but I realise, that both methods are wrong, maybe you have any advice for java programming newbie? Thank you in advance :)

PARTNERS