0

I want to spawn my item (Sword1) in random places. When i spawn it, first of all it only creates 1 of it, then it moves randomly everywhere. Should I create and array for the object? How?

    public class Sword1 {

        public static TextureRegion sprite;

        public static int x;
        public static int y;
        public static int size;

        public static boolean created;
        public Sword1(){
            created=true;
            Random r = new Random();
            x = (r.nextInt(5)+1)*GameRender.tilesize;
            y = (r.nextInt(5)+1)*GameRender.tilesize;
            size = GameRender.tilesize;
            sprite = AssetLoader.s1;
            createMe();
        }

        public static void createMe() { 
            GameRender.batch.draw(sprite, x, y, size, size);    
        }

   }

I render it in batch:

while(number<4){
    number++;
    Items.createSwords();
}   

I also tried to use Items class that would hold all the Items when there would be more

public class Items {


    public Items(){}

    public static void createSwords() {
        Object sword = (Sword1) new Sword1();
    }
}
dawez
  • 2,714
  • 2
  • 29
  • 50

1 Answers1

0

You can clean up and rename a bit the Sword1 class [I also changed the static variables to private] otherwise they will be shared across the various class instances see: Static Variables — What Are They?.

public class Sword {

    private TextureRegion sprite;

    private int x;
    private int y;
    private int size;

    public Sword() {
        Random r = new Random();
        x = (r.nextInt(5)+1)*GameRender.tilesize;
        y = (r.nextInt(5)+1)*GameRender.tilesize;
        size = GameRender.tilesize;
        sprite = AssetLoader.s1;        
    }

    public void createMe() { 
        GameRender.batch.draw(sprite, x, y, size, size);    
    }
}

Then to have multiple swords you can use and an ArrayList of Swords the class that is using the Sword object GameRender as given in comment:

    private List<Sword> swords = new ArrayList<Sword>();

for more info on Array see the Oracle documentation

Now you would have a List of Swords objects. This list will be used to store the newly create Sword objects and to render them later.

create swords

//create 10 swords
for (int i = 0; i<10 ; i++ ) {
    swords.add(new Sword());
}

render swords , call the create method in each object

for (Sword sword : swords) {
    sword.createMe();
}

for example a minimal example in the GameRender Class

public class GameRender() { 
    private List<Sword> swords = new ArrayList<Sword>();

    public GameRender(){
        // create the swords
        for (int i = 0; i<10 ; i++ ) {
            swords.add(new Sword());
    }

    // render the swords
    public void render() {
        for (Sword sword : swords) {
            sword.createMe();
        }

    }

}
dawez
  • 2,714
  • 2
  • 29
  • 50
  • it doesnt work and I get tons of errors because my render is in other class GameRender – ill feedyou Oct 25 '14 at 19:12
  • You would have then to pass the SpriteBatch to the sword createMe() method. – dawez Oct 25 '14 at 19:14
  • I dont get it. still errors. Where do i put the List?(and even if i put it somewhere it wants to be ArrayList not list) where to put foreach if there is update/render loop only in other class? – ill feedyou Oct 25 '14 at 19:26
  • This is messed up. Is there other ways to do it? first of all you removed the static but when i call it in other class (foreach loop) it needs to be static, second - in for each it doenst know what Sword sword : is it doesnt know what swords is. then in the Items() class it doesnt know what swords.add is because there is nothing ellse in it... Please tell me that this is not the only way to do stuff like this. – ill feedyou Oct 25 '14 at 19:48
  • When you have a static variable in a class **ALL** its instances will have the **SAME** values in these variables[that is the plan of static variables. So somehow you have to avoid them if you want to have unique values for different objects. The call in the foreach is calling the methos on the **sword object** no on the **Sword class**. What is the purpose of the Items class?If that one is only holding the reference to the Sword object, it can just removed. If you want to keep and use it, you would have to create the sword(s) objects in that class and have a method to render the swords – dawez Oct 25 '14 at 19:55
  • Whoops, I edited again the post, I was wrongly using the Items class. – dawez Oct 25 '14 at 20:04
  • I did EVERYTHING like in edited post and still : http://puu.sh/cqkMQ/b11a66bbc8.png there is really something else that should be done here. – ill feedyou Oct 25 '14 at 20:12
  • Doh, too much php for me... I used **foreach** but actually the right one is **for**, update the post. – dawez Oct 25 '14 at 20:16
  • Works like a charm now. One day i will find you and hug you! #nohomo Dont know how i will do collisions with them but ill figure it out. Are you ok if i write to you tomorrow mb if i wont do it? – ill feedyou Oct 25 '14 at 20:20