Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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: Class and array on: 2011-01-18 20:57:24
Yeah I was Tongue

Sorry! Was just staring at one website which didn't help me enough.

Google is your friend ^^

It's starting to work as I want it to now Smiley


2  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 20:38:05
hmm need help with that too Tongue

so how do I create a vector? Defining and initialization.

and how do I add elements to the vector in a loop?
3  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 20:21:00
hmm i think a vector is more suitable for what I want so i'm gonna change everything to a vector now.
4  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 19:26:24
Just one last question!

How do I delete an element from my array?
1  
myArray[i].splice(i,i);



splice() doesn't seem to work because i'm using a custom array.
5  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 18:35:31
*sigh* I had another project as main project which ran when i compiled...

Thanks for everyones help! Smiley
6  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 17:14:22
Ok, I can't seem to find the problem.
I'll show you all my code I hvae so far.
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  
/**
 * @author Satchel
 */



package testlife;
//import java.util.Random;
//import java.util.Timer;
import java.util.*;

class Person {
        int life = 100;
        boolean alive = true;
        }


public class Main {
   
    public static int rounds = 0;
    public static int delay = 100; //milliseconds
   
    public static Random generator = new Random();  

    public static int startPop = 10;    
    private static Person myArray[];

    public static void main(String[] args) {
        // TODO code application logic here
       

        System.out.println("alive : " + startPop);
                       
        for (int i = 0; i<startPop; i++){
            myArray[i] = new Person();  
        }

        System.out.println("pop: "+myArray.length);

        //System.out.println(generator.nextInt(10)); //Generates a random nr between 0 - 10

        while(myArray.length != 0){
            for(int i=0; i<myArray.length; i++){
                if(myArray[i].alive == true){
                    //System.out.println("HP = "+monster.life);
                    System.out.println("Population = "+i);
                    System.out.println("Round = "+rounds);
                    rounds = rounds +1;
                    myArray[i].life = myArray[i].life - 1;

                    System.out.println("");

                    //Search for food
                    int nr = generator.nextInt(10);
                    if(nr > 6){
                        myArray[i].life = myArray[i].life + 2;
                    }

                    //Mate

                    //Dead or alive
                    if(myArray[i].life <= 0){
                        myArray[i].alive = false;
                        //monster.splice(i,i);
                    }

                    //Slows down the process to make it more readable
                    try {
                        Thread.sleep(delay);
                    }
                    catch (Exception e) {}

                }
            }
           
       
        }
    }

}



All I get is just a successful run when I run the code.
7  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 14:01:17
Great that seems to work!

But now i'm wondering how to get information from each element.
when I try to print out a variable like this:
1  
System.out.println("population: "+myArray.length);


It doesn't give me any numbers. It doesn't even print out the word within "".

8  Game Development / Newbie & Debugging Questions / Re: Class and array on: 2011-01-18 13:23:15
How do you fill the array at the moment?

Mike

Well that is one of the thing i'm not sure if i'm doing right.

Now i'm doing a for loop which each turn creates a new object and use the push function to add it to the array:
1  
2  
3  
4  
for (int i = 0; i<10; i++){
            Person person = new Person();
            myArray.push(person);
        }


But the compiler says "Cannot find symbol" at the push function
9  Game Development / Newbie & Debugging Questions / Class and array on: 2011-01-17 23:15:34
So what I am trying to do is loop through an array filled with variables which are defined by my own class.

The class itself contains variables like integers and booleans, and I want to add other stuff later.

1  
2  
3  
4  
class Person {
        int life = 100;
        boolean alive = true;
    }


and then I want to create an array which i'm going to fill with variables from my class.

1  
static Person myArray[];


Now here i'm starting to get problems.

I've tried to fill the array with variables from my clsas through a loop but without success.

And I'm guessing that's why I cant loop through my array like this:

1  
2  
3  
4  
for(int i=0; i<myArray.length; i++){
                if(myArray[i].alive == true){
                    ...
                }


Grateful for help!
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.

Elsealabs (15 views)
2014-12-28 10:39:27

CopyableCougar4 (19 views)
2014-12-28 02:10:29

BurntPizza (24 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!