h3ckboy
|
 |
«
Posted
2009-02-02 06:17:00 » |
|
ok so to mkae a wariable global in a class you just declar it outside of and method. so I have this code. the prints are so that I can know if it is working before I write gui for that part. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.io.*; public class Enemy { boolean survive = true; String enemy; int[] enemyInfo = new int[5]; String line; int x; int health; public void subHealth(int i){System.out.println(enemyInfo[0]);health = health - i;System.out.println(health);} public void setEnemy(String i){ enemy = i; } public boolean checkAlive() { if(health<1){survive = false;} return survive; } public void getEnemyInfo() { try{ File inFile = new File("resources/enemies/"+enemy+".txt"); BufferedReader reader = new BufferedReader(new FileReader("resources/enemies/"+enemy+".txt")); line = null; while ((line=reader.readLine()) != null) { enemyInfo[x] = Integer.parseInt(line); x++; } }catch(FileNotFoundException e){System.err.println("Library not complete");} catch(IOException e){} catch(NumberFormatException e){} health = enemyInfo[0]; } } |
|
|
|
|
Mr_Light
|
 |
«
Reply #1 - Posted
2009-02-02 08:21:36 » |
|
- What is the question(if any)
- What your describing is a class variable, java doesn't have global' variables in the 'traditional' sense.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
h3ckboy
|
 |
«
Reply #2 - Posted
2009-02-02 12:15:42 » |
|
oh i am sorry I forgot to ask  . health = enemyInfo[0];//over here it is ten public void subHealth(int i){System.out.println(enemyInfo[0]);health = health - i;System.out.println(health);}//and then over here is 0 in one place it is ten and in another it is 0.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Mr_Light
|
 |
«
Reply #3 - Posted
2009-02-02 12:42:02 » |
|
Heh hence they are called variables.  Depending on how your code gets called this makes sense. So if subHealth(10); would get called some where or subHealth(5); subHealth(5); etc.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
h3ckboy
|
 |
«
Reply #4 - Posted
2009-02-02 12:43:18 » |
|
so are they in defferent instances of the class here is where it is called from 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.net.URL; import javax.swing.AbstractButton; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Dimension; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import java.awt.GridLayout; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.KeyStroke; public class battle extends JPanel implements ActionListener { int time = 1; Enemy enemy = new Enemy(); String created; JFrame frame; String enemyType; boolean survive; protected static JButton attack, jbnMiddle, jbnRight; public battle() { attack = new JButton("attack"); attack.setMnemonic(KeyEvent.VK_A); attack.setActionCommand("attack"); jbnMiddle = new JButton("Centre button"); jbnMiddle.setVerticalTextPosition(AbstractButton.BOTTOM); jbnMiddle.setHorizontalTextPosition(AbstractButton.CENTER); jbnMiddle.setMnemonic(KeyEvent.VK_M); jbnMiddle.setActionCommand("center"); jbnMiddle.setLayout(new GridLayout(1,2,2,2)); jbnMiddle.setToolTipText("Centre button"); jbnRight = new JButton("Enable centre button"); jbnRight.setMnemonic(KeyEvent.VK_E); jbnRight.setActionCommand("enable"); jbnRight.setEnabled(false); attack.addActionListener(this); jbnMiddle.addActionListener(this); jbnRight.addActionListener(this); jbnRight.setToolTipText("Enable the Centre button."); add(attack); } public void actionPerformed(ActionEvent e) { if ("attack".equals(e.getActionCommand())) { enemy.subHealth(10); survive = enemy.checkAlive(); if(survive == false) { System.out.println("dead"); frame.setVisible(false); } } if ("enable".equals(e.getActionCommand())){ } if ("center".equals(e.getActionCommand())){ } } public void setEnemy(String i){ enemyType = i; enemy.setEnemy(enemyType); } public void createGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("combat"); frame.setPreferredSize(new Dimension(200,75)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); battle buttonContentPane = new battle(); buttonContentPane.setOpaque(true); frame.getRootPane().setDefaultButton(attack); frame.setContentPane(buttonContentPane); frame.pack(); frame.setVisible(true); } protected static ImageIcon createImageIcon(String path) { URL imgURL = battle.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find image in system: " + path); return null; } } public static void main(String[] args) { battle bat = new battle(); bat.createGUI(); bat.setEnemy("imp"); } } |
|
|
|
|
Mr_Light
|
 |
«
Reply #5 - Posted
2009-02-02 12:48:05 » |
|
is it 0 after pressing the buttom?
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
h3ckboy
|
 |
«
Reply #6 - Posted
2009-02-02 12:52:57 » |
|
the subhealth works fine. it is in the enemy class. the getEnemyInfo method goes into a text file to get the info for that enemy. int he metho where it gets it, health = 10. but in the suhealth method health = 0. do you understand?
|
|
|
|
Mr_Light
|
 |
«
Reply #7 - Posted
2009-02-02 14:10:18 » |
|
you make an instance of the enemy class. you load the settings from the textfile health=10 then subHealth(10); gets called: 1 2 3 4 5
| public void subHealth(int i){ System.out.println(enemyInfo[0]); health = health - i; System.out.println(health); } |
System.out.println(enemyInfo[0]); should print 10. health = health - i; -> health = 10 - 10 -> health = 0; System.out.println(health); -> prints 0. Sounds like everything is happening as specified.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
h3ckboy
|
 |
«
Reply #8 - Posted
2009-02-02 14:25:35 » |
|
here is what appears in the console:
0 -10 dead
it does not go as it should.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Mr_Light
|
 |
«
Reply #10 - Posted
2009-02-02 15:09:05 » |
|
you make an instance of the enemy class. you load the settings from the textfile <- getEnemyInfo isn't being calledhealth= 10 0then subHealth(10); gets called: 1 2 3 4 5
| public void subHealth(int i){ System.out.println(enemyInfo[0]); health = health - i; System.out.println(health); } |
System.out.println(enemyInfo[0]); should print 10. 0health = health - i; -> health = 100 - 10 -> health = 0 -10; System.out.println(health); -> prints 0. -10Sounds like everything is happening as specified.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
h3ckboy
|
 |
«
Reply #11 - Posted
2009-02-02 15:19:35 » |
|
I changed it to 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.io.*; public class Enemy { boolean survive = true; String enemy; int[] enemyInfo = new int[5]; String line; int x; int health; public void subHealth(int i){System.out.println(enemyInfo[0]);health = health - i;System.out.println(health);} public void setEnemy(String i){ enemy = i; [u][b]getEnemyInfo();[/b][/u] } public boolean checkAlive() { if(health<1){survive = false;} return survive; } public void getEnemyInfo() { try{ File inFile = new File("resources/enemies/"+enemy+".txt"); BufferedReader reader = new BufferedReader(new FileReader("resources/enemies/"+enemy+".txt")); line = null; while ((line=reader.readLine()) != null) { enemyInfo[x] = Integer.parseInt(line); x++; } }catch(FileNotFoundException e){System.err.println("Library not complete");} catch(IOException e){} catch(NumberFormatException e){} health = enemyInfo[0]; } } |
end I still get the same in the console.
|
|
|
|
cylab
|
 |
«
Reply #12 - Posted
2009-02-02 20:07:38 » |
|
I don't know, if you reuse your Enemy instance on purpose, but I think you have problems to grasp the concepts of classes and instances. Take a look at http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html. I think you want something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| public class Enemy { private boolean survive = true; private String name; private int health;
public Enemy(String name) { this.name = name; loadEnemyInfo(name); }
public int getName() { return name; }
public int getHealth() { return health; }
public void hit(int damage) { health = health - damage; }
public boolean isAlive() { if(health<1){survive = false;} return survive; }
protected void loadEnemyInfo() { int[] enemyInfo = new int[5]; String line; BufferedReader reader = null; try { File inFile = new File("resources/enemies/"+enemy+".txt"); reader = new BufferedReader(new FileReader("resources/enemies/"+enemy+".txt")); line = null; int x = 0; while ((line=reader.readLine()) != null) { enemyInfo[x] = Integer.parseInt(line); x++; } } catch(FileNotFoundException e) { System.err.println("Library not complete"); } finally { if(reader!=null) { try { reader.close() } catch(IOException ignore){} } } catch(IOException e){} catch(NumberFormatException e){} health = enemyInfo[0]; } } |
So you can use it like 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class Battle extends JPanel implements ActionListener { private Enemy[] enemies = null; public Battle() { enemies = new Enemy[]{ new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Gimp"), new Enemy("Gimp"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Overlord") }; } } |
This way you create an array of enemies that consists of 4 instance with initial values loaded from the "Orge.txt" file, 2 instances from the "Gimp.txt" file, 5 instance loaded from the "Rat.txt" file and one from the "Overlord.txt". Some additional notes on common java code style: - Use uppercase classes (Battle instead of battle) - lowercase names are usually used as variable names - Use loadEnemyInfo is better than getEnemyInfo, because getXXX usually describes the accessor of a "Property" - Make every member variable of a class private and provide get/is/set Methods to expose them to the outside only if desired - I changes checkAlive to isAlive, since the status to be alive is a "Property" of the Enemy. (isXXX describes the accessor of a boolean "Property" Edit: missed the "String" type for the "name" variable and the "enemy[]" declaration was not correct.
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
h3ckboy
|
 |
«
Reply #13 - Posted
2009-02-02 20:27:49 » |
|
thank you for enlightening me. I will try and change my code to this way. If I have troubles I will post.
|
|
|
|
h3ckboy
|
 |
«
Reply #14 - Posted
2009-02-03 09:21:24 » |
|
why did you make it an array? there is only one enemy at a time so far.
|
|
|
|
cylab
|
 |
«
Reply #15 - Posted
2009-02-03 12:19:41 » |
|
I didn't know that. It was an example of how to use the Enemy class to create multiple Enemy instances based on different data files. If you want to create a Battle screen with a single enemy, you can just hand in the current Enemy instance: 1 2 3 4 5 6 7 8 9 10 11
| public class Battle extends JPanel implements ActionListener { private Enemy currentEnemy = null; public Battle(Enemy enemy) { this.currentEnemy = enemy; } } |
So you can call the Battle window from you main game window like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class GameWindow extends JFrame implements ActionListener { private Enemy[] enemies = null; public GameWindow() { enemies = new Enemy[]{ new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Gimp"), new Enemy("Gimp"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Overlord") }; } public void battleEnemy(int enemyIndex) { JFrame battleWindow = new JFrame(); Battle battle = new Battle(enemies[enemyIndex]); battleWindow.setContentPane(battle); battleWindow.setVisible(true); } } |
That's just a quick example. How exactly you code this depends on what you want to achieve... If you need assistance in windowing, take a look at the swing tutorials over at sun: http://java.sun.com/docs/books/tutorial/uiswing/
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Renoria
|
 |
«
Reply #16 - Posted
2009-02-03 12:33:24 » |
|
umm 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| enemies = new Enemy[]{ new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Gimp"), new Enemy("Gimp"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Overlord") }; |
should be 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Enemy[] enemies = { new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Ogre"), new Enemy("Gimp"), new Enemy("Gimp"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Rat"), new Enemy("Overlord") }; |
|
|
|
|
cylab
|
 |
«
Reply #17 - Posted
2009-02-03 12:56:41 » |
|
Nope.  The enemies Array is already defined as a member variable to make it available to other methods of the same class. If you redeclare it locally, the class member called enemies would remain unset. Also in java you can't use a short form for array initialization, it has to be <variable> = new <Type>[]{ new <Type>(), new <Type>(), ...}
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Del-ONE
Senior Newbie 
|
 |
«
Reply #18 - Posted
2009-02-03 13:15:28 » |
|
Nope.  The enemies Array is already defined as a member variable to make it available to other methods of the same class. If you redeclare it locally, the class member called enemies would remain unset. Also in java you can't use a short form for array initialization, it has to be <variable> = new <Type>[]{ new <Type>(), new <Type>(), ...}err...int[] arr = new int[5] is shorter, unless your talking about a short-form to give the array locations starting values. On a side note I may just seen the way to pass an array that is created in the method call. I always had a syntax problem.
|
|
|
|
cylab
|
 |
«
Reply #19 - Posted
2009-02-03 13:21:00 » |
|
It's about array initialization, not declaration.
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Del-ONE
Senior Newbie 
|
 |
«
Reply #20 - Posted
2009-02-03 13:24:47 » |
|
That is initializing the array
int[] arr;
is declaring it.
Also, this works
Object[] arr = {new String("HI"),new Integer("HI")};
Renoria was at least right on that part (though he did declare a new array)
Edit: I take that second part back, I forgot about that annoying caveat that you can only do that when declaring and initializing on the same line.
|
|
|
|
h3ckboy
|
 |
«
Reply #21 - Posted
2009-02-03 14:52:50 » |
|
OH well. but why is it an array??
|
|
|
|
cylab
|
 |
«
Reply #22 - Posted
2009-02-03 15:17:52 » |
|
That is initializing the array
Ok, you are right. I just meant providing inital values. Object[] arr = {new String("HI"),new Integer("HI")};
Actually I really didn't know that this could work in any circumstance. You live, you learn 
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
cylab
|
 |
«
Reply #23 - Posted
2009-02-03 15:18:59 » |
|
OH well. but why is it an array??
Have you read my post? It was an example for having more than one enemy in your game.
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
h3ckboy
|
 |
«
Reply #24 - Posted
2009-02-03 15:57:13 » |
|
OH ok so it owuld work fine wiht jsut one String going through?
|
|
|
|
cylab
|
 |
«
Reply #25 - Posted
2009-02-03 16:41:40 » |
|
Sure. Just create one Enemy with new Enemy("Name"). The only thing you have to understand is, that member variables of a class keep their value for the specific instance you create with new and if you change the value of a member variable, it is changed in scope of that instance. That is the difference to a global variable, which would change for the whole program. For more information, see http://java.sun.com/docs/books/tutorial/java/concepts/index.html
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
|