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: 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 ) { int i = 6; System.out.println( "intital primitive int: " + i ); changeInt( i ); System.out.println( "after method call: " + i );
Integer wrapperI = new Integer( 12 ); System.out.println( "\ninital Wrapper value: " + wrapperI ); changeInteger( wrapperI ); System.out.println( "after method call: " + wrapperI );
AtomicInteger ai = new AtomicInteger( 24 ); System.out.println( "\ninitial Atomic value: " + ai ); changeAtomicInteger( ai ); System.out.println( "after method call: " + ai );
int i2 = 30; System.out.println( "\ncorrect method primitive: " + i2 ); i2 = changeInt( i2 ); 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. 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 ); 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.
|
|
|
|
|
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
|
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class RacingCarFrame extends JFrame { public RacingCarFrame( ) { ImagePanel car = new ImagePanel( ); add( car );
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 { private Timer timer = new Timer( 10, new TimerListener( ) );
int carX = 20; int carY = 40;
int carSpeed = 0;
int carBodyWidth = 50; int carBodyHeight = 10; int carHoodWidth = 10; int carHoodHeight = 10; int carTireHeight = 10; int carTireWidth = 10;
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 ); }
private class TimerListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { carX += carSpeed; if( carX >= getWidth( ) ) carX = 20; repaint( ); } }
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 ) { } }
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...
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|