Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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: Array game. on: 2011-12-03 13:37:46
1  
 Hunter Hunter = new Hunter(11,11,"H");


I did not examine everything in detail but what looked wrong was the piece above.

Can you name the variable same as the class? If you can, then it is at least bad style.
I would prefer something like
1  
Hunter hunter = new Hunter(...);


Quote
The  Route1 Class is being used for...

I see a route1() and paint() method. Maybe you mixed up the terms?

I don't completely understand what you are doing. You say that both methods are called every time the user presses one of the movement keys.
I have now looked a bit more at your code. It seems that you are creating new objects at each cycle of your game loop. And you will always give them the same starting coordinates.

If you want to save the state of player and hunters over the loop, then you should initialize your player once, create the object and use the reference to this one object without throwing it away.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void route1(){
    Board board = new Board();
    Hunter Hunter = new Hunter(11,11,"H");
    Scanner in = new Scanner(System.in);
    Random number = new Random(2);
    int random = number.nextInt(2);
     if(random ==1)
        Hunter.x = Hunter.x -1;
    else
       Hunter.y = Hunter.y -1;
    }

A variable has a certain scope. If you create a variable within a method (like "Hunter" here. use hunter as name, not Hunter. Hunter is your class), it will be in your program's memory until the method ends.
This is what you do:
Player does something. Hunters have to be updated
  • create a hunter object at a position
  • move the newly created hunter into a random direction
  • forget that the hunter ever existed (you don't remember the reference to it)

What you should do:
Your Hunter-class has only a constructor
1  
2  
3  
4  
5  
public Hunter(int x, int y, String name){
    this.x = x;
    this.y = y;
    this.name = name;
}

You should have a move() method and access to your coordinates as well.
Then you could tell the hunter to move. Then you would not create the hunter
within the route1() method but outside your game loop in your Main class. Then,
when it comes to updating the hunters' positions, you do something like this:
1  
2  
3  
for (int i = 0; i < numberofhunters; i++) {
    hunter[i].move();
}


The move() method would then contain all the code to generate the new coordinates
of the hunter. It would store the coordinates within the object and would not forget
them.

There are a lot of strange things in your code. I would suggest doing some Java tutorials
first to get familiar with its basic concepts before doing a bigger project. Learn about
classes, objects and methods. Learn about the scope of variables and their visibility.

A small one class example does not provide as many chances to create bugs and will
improve the understanding. Once you have done this, go back to your project, find the
mistakes and strange things, start new and finish your project.
2  Game Development / Newbie & Debugging Questions / Re: Array game. on: 2011-12-01 06:44:46
Hi Arom,

I just had a look at your other thread where you had already pasted some of your code. Like ra4king said, I also don't like to download archives of unknown source.

If your code still looks like this, then I would say that you are missing a game loop. The main method draws the map and then there are some conditions which will end after being executed once.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public static void main(String[] args) {    
      ....
      for (int r = 0; r < a2.length; r++) {
      ... }
      System.out.println("Input your move");
      move = in.nextLine();
      if (move.equalsIgnoreCase("w")) {
         // move up
        // repaint
        // check for collision
        // check for health
     } elseif {.... } // no loop here. Main will end and the game will be over.
}


I think there should be a look around your input and conditions. And, of course, you would have to replace the comments by something that is actually doing stuff. If you have done so already, then please post the relevant piece of code.
3  Game Development / Newbie & Debugging Questions / Re: Getting Clicked Block on: 2011-11-29 20:32:13
Int() returns a double?  And people wonder why I stay away from Microsoft  Cheesy

What? I didn't read that. So -8.2 float is converted to -9 double?!?! I'm confused... Luckily Java behaves differently.
4  Game Development / Newbie & Debugging Questions / Re: Getting Clicked Block on: 2011-11-29 18:53:43
I don't understand, are you saying I'm right or wrong?  PureBasic did this.
It only makes sense to me, 'cause 1.99999 is better represented as 2 than 1.

Edit:  Ok, I was wrong about the .5 rounding up, it's .6 and higher that round up.  But still, using Int(number) in basic languages drop the decimal point entirely which is why I posted the code above with (int) in it.

You are right that 1.99999 is much nearer to 2 than to 1. But I was used to all decimal places being truncated when assigning a floating value to an int variable. After I had read your post, I read on MSDN that this not the case in Basic (at least VBA). The article seems to say that -8.2 is rounded to -9. I'm still a bit confused about that...
5  Game Development / Newbie & Debugging Questions / Re: Randomly Generated Terrain on: 2011-11-29 18:43:47
FTFY.
p.s. your code looks pretty normal; good for you for being open and sharing it along with your thoughts.
Thanks, still not used to forum tags.
6  Game Development / Newbie & Debugging Questions / Re: Getting Clicked Block on: 2011-11-29 07:03:28
@Ra4king, I still have to get used to that.  In some languages (mostly basic dialects), when setting the value of 1.5 to an integer it would round up to 2.

Wow, I just wanted to say "that sounds like BS, no sane language would do this" but then I googled a bit and it seems that I am right (about the language). VBA does this http://msdn.microsoft.com/en-us/library/Aa188474 and the article says:

Quote from: MSDN
Int(-8.2)

Using the Int function, on the other hand, yields "-9":
Which makes sense, although in a very strange way. If the digit right of the dot is below five, then go to the next number below. If the number is negative, this would result in an unexpected decrease.

Well, one never stops learning... and wondering....
7  Game Development / Newbie & Debugging Questions / Re: Randomly Generated Terrain on: 2011-11-28 20:19:34
I have generated some random terrain a few days ago and posted a blog entry about it including my (bad) code.

After I had read about Perlin Noise, I wanted to try to generate a map for a game I plan to do.

Because I thought it would be hard to fill the terrain below the line created by the perlin noise, I just used an image of terrain and shifted parts of its pixels downwards to create valleys. Probably not the best approach but it works.
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.

Nickropheliac (14 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!