-2

I have a class Sword that is instantiated to create several objects in my game and hold them in an Array structure. How do I find the attributes each object of Sword class from another class ?

How my spawned swords look

This code allocate and generate the Sword(s) Renderer class:

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

Then in public Renderer() method I do:

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

In the render() they are displayed on the screen:

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

And here is my Sword class:

 public class Sword {
    private TextureRegion sprite;
    private static int xx;
    private static int yy;
    private int x;
    private int y;
    private int size;
    private Random r;
    public Sword() {
        r = new Random();
        x = (r.nextInt(10))*GameRender.tilesize;
        y = (r.nextInt(10))*GameRender.tilesize;
        size = GameRender.tilesize;
        sprite =getSprite();        
    }

    private TextureRegion getSprite() {
        int id = r.nextInt(2);
        System.out.println(id);
        if(id==1){
            sprite=AssetLoader.s1;
        }else sprite=AssetLoader.s2;
        return sprite;
    }

    public void createMeShape(){
        GameRender.shapeRenderer.rect(x, y, size, size);
    }

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

    public static void Update() { }
}

So how do i find the value of x, y from the Render class (I cant use static because it will only find one swords position) for collision detection or how i check if swords x,y is same like playerx, playery (ints from GameRenderer class)?

dawez
  • 2,714
  • 2
  • 29
  • 50
  • You would need to add a getter method. See the following questions in SO [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) and [How do getters and setters work?](http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – dawez Oct 26 '14 at 19:48

1 Answers1

1

Add a getter in your Sword class.

// Returns the sword x location
public int getLocX()
{
    return this.x;
}

There's many ways you can go about this, i.e. using a Vector instead. The implementation is up to you.

On a side note, I would highly recommend you use Box2D for collision detection in libgdx.

user1234
  • 689
  • 1
  • 8
  • 22
  • i cant use this because when i use somewhere getLockX() it needs to be static. I guess i need to use Voctors. – ill feedyou Oct 26 '14 at 19:23
  • 1
    Why the **getLocX()** needs to be static? Have a read at what a static variable/method is: [Java – static variable with example](http://beginnersbook.com/2013/05/static-variable/) and [Java: when to use static methods](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods). – dawez Oct 26 '14 at 19:32
  • Nvm. The vectors worked perfectly, thankyou i didnt even thought about vectors before this – ill feedyou Oct 26 '14 at 20:30