Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: compareTo is final in Enum... on: 2011-02-25 17:23:23
Ah, I've just read through the Comparable interface's API and it somewhat makes sense now. So what I really want is my own original method compareTypes because compareTo is designed so that it sets the elements in a specific order for sorting maps and the like. I get it. I feel better about writing my own method now. Thanks.
2  Game Development / Newbie & Debugging Questions / compareTo is final in Enum... on: 2011-02-25 16:10:26
OK, so I have an enum with about 15 types in it, representing elements such as water, grass, fire and such. I made the enum implement Comparable<E> and rewrote the compareTo method. BUT apparently compareTo is final and I am not allowed to override it. The final version of compareTo compares them based on their ordering, but there is no logical way to order the types so that the relationships are modeled correctly. For example, the ordering

WATER FIRE GRASS GROUND...

water is strong to fire, fire is strong to grass and grass is strong to ground. BUT water is also strong to ground, and grass is strong to water.....

the obvious option is to write a different method called compareTypes( Type ) or something to that effect but it feels wrong. It feels broken... I really wanted a compareTo method...
3  Game Development / Newbie & Debugging Questions / Re: Pass By Reference Doesn't Work For Wrapper Classes?? on: 2011-02-19 09:40:23
So how exactly would I go about changing the global reference rather than creating a new local reference?

I see where the break is...
 
It seemed to me that when I say

changeInteger( wrapperI );

that I'm passing a pointer to an Integer object into the method and inside the method, the parameter i is pointing to the same thing as global variable wrapperI. So when I changed i inside of the method it should also change wrapperI.

BUT, when I said i = new Integer( 100 ), i no longer pointed to what wrapperI pointed to. And I'm assuming ( by looking at the API ) that Integer is immutable, therefore there is no way to change its value without creating a new instance of Integer and assigning to the previous Integer object reference. So it is impossible to change an integer( of any kind) by passing it into a method. You HAVE to assign it in its original scope.

Tell me if I'm wrong.
4  Game Development / Newbie & Debugging Questions / Pass By Reference Doesn't Work For Wrapper Classes?? on: 2011-02-17 11:05:16
Here's a curve ball, maybe someone can explain it to me.
Apparently someone decided Wrapper classes won't act like references, but like values?

Program Output:
Quote
intital primitive int: 6
value returned by method call: 100
after method call: 6

inital Wrapper value: 12
value returned by method call: 100
after method call: 12

initial Atomic value: 24
value returned by method call: 100
after method call: 24

correct method primitive: 30
value returned by method call: 100
after method call: 100

Process completed.


Source Code:
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  
public class TestPassByRef
{
   public static void main( String [] args )
   {
      /* not supposed to work. Pass By Value */
      int i = 6;
      System.out.println( "intital primitive int: " + i );
      changeInt( i );
      System.out.println( "after method call: " + i );

      /* Doesn't work. Pass By Ref acts like Pass By Value*/
      Integer wrapperI = new Integer( 12 );
      System.out.println( "\ninital Wrapper value: " + wrapperI );
      changeInteger( wrapperI );
      System.out.println( "after method call: " + wrapperI );

      /* Doesn't work also. */
      AtomicInteger ai = new AtomicInteger( 24 );
      System.out.println( "\ninitial Atomic value: " + ai );
      changeAtomicInteger( ai );
      System.out.println( "after method call: " + ai );

      /* what should be done */
      int i2 = 30;
      System.out.println( "\ncorrect method primitive: " + i2 );
      i2 = changeInt( i2 ); //assign return value of method to original integer
     System.out.println( "after method call: " + i2 );

   }

   private static Integer changeInt( int i )
   {
      i = 100;
      System.out.println( "value returned by method call: " + i );
      return i;
   }

   private static Integer changeInteger( Integer i )
   {
      i = 100;
      System.out.println( "value returned by method call: " + i );
      return i;
   }

   private static AtomicInteger changeAtomicInteger( AtomicInteger i )
   {
      i = new AtomicInteger( 100 );
      System.out.println( "value returned by method call: " + i );
      return i;
   }

}
5  Game Development / Newbie & Debugging Questions / Re: abstractMethodError on: 2011-02-14 09:16:39
classic n00b mistake. Thanks ra4king (and lhkbob). The problem was runEffect() in Attack's subclasses. They all had an undefined method in them, which made them unusable by the compiler so it went up to the parent class version which was abstract hence, the AbstractMethodError.

I use JCreator, because my first professor used it and I've grown accustomed to it. Eclipse and netbeans both take extremely long to startup and I don't consider myself in need of their advanced features just yet. Everytime I build a project in JCreator, it saves all of the files that are open in JCreator. I thought it compiled them as well, but lesson learned. Thanks everybody.
6  Game Development / Newbie & Debugging Questions / Re: abstractMethodError on: 2011-02-14 07:37:06
EDIT: I understand the concept behind what is going on... if the method were

Monster.attack( SwordSlash, Monster )

then this would work fine. The idea I'm missing is why even bother using an Attack parameter if I can't use its subclasses methods.



There isn't much to the stack trace...

It's a runtime error( sorry, I should have mentioned that. In my head it was a compiler error ).
The runtime error occurs when the program comes across nonstatic Monster.attack( Attack, Monster ).

The Battle class essentially accepts two Monsters and loops them through a series of attacks until one's health is 0.
The battle loop is currently in the constructor of the Battle class... bad, I know.

Quote
Exception in thread "main" java.lang.AbstractMethodError: Attack.runEffect(LMonster;)V
    at Monster.attack(Monster.java:194)
    at Battle.<init>(Battle.java:62)
    at Battle.main(Battle.java:127)
7  Game Development / Newbie & Debugging Questions / abstractMethodError on: 2011-02-14 06:45:55
OK so...

the attack() method is inside the Monster class

class Attack is an abstract class. It has MANY subclasses( so far about 10, planned to exponentiate ). The idea is, that attacks cause damage( or don't ), and also have some other effect in the battle such as raising its own attack power or lowering an enemy's defense power. So, class Attack has an abstract method called runEffect( ), which each subclass is forced to override.

For example,
1  
2  
Monster m = new Monster( );
m.attack( new SwordSlash( ), badGuy );


SwordSlash not only does damage, but with a 50% chance it can also lower the target's defense. So its runEffect() method runs the math, if the Monster is lucky, he lowers his target's defense.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void attack( Attack a, Monster target )
   {
      if( a == null )
         return;

      System.out.println( "\n" + this.name + " used " + a.getName( ) + "!" );

      int damage = 0;
      if( a.getPower( ) != 0 )
         damage = calcDamage( a, target );

      target.takeDamage( damage );

      a.runEffect( target ); //AbstractMethodError
     a.decrementUsesLeft( );
      this.setAttackChoice( null );
   }


I understand the error. SwordSlash is being demoted to just an Attack, and a.runEffect( ) is trying to use the runEffect( ) method in Attack which is abstract, hence the error. The question is what now? I don't want to write a different attack( ) method for each subclass of Attack.
8  Game Development / Newbie & Debugging Questions / Re: Acceleration of an object on: 2011-02-14 06:18:35
Thank you everybody, those were helpful replies. requestFocus( ) and adding the keyListener to the ImagePanel were what I needed to get it running.
9  Game Development / Newbie & Debugging Questions / Acceleration of an object on: 2011-02-07 11:12:14
Hello, I'm a programming student and one of my exercise examples was to animate a car to drive across the screen. Done. Then I wanted to get creative and have the user hold a key to accelerate the car, and then when they let go, have the car slow to a stop. Sadly, I cannot get this to work. Incorporating the KeyListener into the Timer has proven too challenging for me. I would appreciate any help.

I understand that many of these practices aren't the best. I SHOULD have built a Car class, but the exercise called for drawing Graphics class shapes to make car, and I've been building on that... This could also work supposedly with an Image object.

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  
/* Nicholas Pickering
 * 2/7/2011 1:25 AM
 *
 * RacingCar.java
 *
 */


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RacingCarFrame extends JFrame
{
   public RacingCarFrame( )
   {
      //panel to draw car onto.
     ImagePanel car = new ImagePanel( );
      add( car );

      //KeyListener requires an object to accept the KeyEvents... I guess
     //I chose a textfield.
     JTextField textfield = new JTextField( );
      textfield.addKeyListener( car.getKeyListener( ) );
      textfield.setFocusable( true );
      add( textfield, BorderLayout.SOUTH );
   }

   public static void main( String [] args )
   {
      RacingCarFrame frame = new RacingCarFrame( );
      frame.setTitle( "RacingCar" );
      frame.setSize( 400, 400 );
      frame.setLocationRelativeTo( null );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.setVisible( true );
   }
}

class ImagePanel extends JPanel
{
   //timer for animation
  private Timer timer = new Timer( 10, new TimerListener( ) );

   //car's position
  int carX = 20;
   int carY = 40;

   //car's velocity;
  int carSpeed = 0;

   //car's dimensions
  int carBodyWidth = 50;
   int carBodyHeight = 10;
   int carHoodWidth = 10;
   int carHoodHeight = 10;
   int carTireHeight = 10;
   int carTireWidth = 10;

   //render frame
  public void paintComponent( Graphics g )
   {
      super.paintComponent( g );

      timer.start( );

      g.setColor( Color.GREEN );
      g.fillRect( carX, carY - 10, carBodyWidth, carBodyHeight );

      g.setColor( Color.GREEN.darker( ) );
      g.fillRect( carX + 20, carY - 20, carHoodWidth, carHoodHeight );

      g.setColor( Color.BLACK );
      g.fillOval( carX + 10, carY, carTireWidth, carTireHeight );
      g.fillOval( carX + 30, carY, carTireWidth, carTireHeight );
   }

   //causes car to move across screen without user interaction
  //carSpeed is currently set to 0, so it doesn't move.
  //setting carSpeed higher will causes it to drive
  //at a consistent speed across the frame.
  private class TimerListener implements ActionListener
   {
      public void actionPerformed( ActionEvent ae )
      {
         carX += carSpeed;
         if( carX >= getWidth( ) )
            carX = 20;
         repaint( );
      }
   }

   //listens for keys and increases car's speed when SPACE is pressed
  private class KeyHandler implements KeyListener
   {
      public void keyPressed( KeyEvent ke )
      {
         if( ke.getID( ) == KeyEvent.VK_SPACE )
         {
            carSpeed++;
            repaint( );
         }
         else
         {
            if( carSpeed > 0 )
               carSpeed--;
            repaint( );
         }
      }

      public void keyReleased( KeyEvent ke )
      {      }
      public void keyTyped( KeyEvent ke )
      {      }
   }

   //returns a KeyHandler object to the jFrame to accept key events
  public KeyHandler getKeyListener( )
   {
      return new KeyHandler( );
   }
}


By the way this code is infernally small to read in preview. And typing at the bottom of the post after the code was posted makes the window focus bounce all over haphazardly...
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (36 views)
2014-07-23 21:16:32

Riven (24 views)
2014-07-23 21:07:15

Riven (26 views)
2014-07-23 20:56:16

ctomni231 (56 views)
2014-07-18 06:55:21

Zero Volt (48 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!