Basically I have this code :

for(int i = 0; i < tnt.size; i++){

        TNT t = tnt.get(i);

        if(cl.isLeft()){ t.getBody().setLinearVelocity(6,0); }

        if(cl.isRight()){t.getBody().setLinearVelocity(-6,0); } 

        if(t.getBody().getPosition().x > 1888/PPM){t.getBody().setLinearVelocity(-6,0);     }

        if(t.getBody().getPosition().x < 31/PPM){t.getBody().setLinearVelocity(6,0);}



    }

What happens is if I have 2 objects in this Array, when something happens to object #1 it gets a change in how it should act, but object #2 gets the same change as object #1. I want them to act independently, so how would I do that?

Here is a GIF of what happens: https://gfycat.com/HelplessEmotionalCornsnake When the object(TNT) touches a wall it should change direction, but here they both change direction when only one should.

1

There are 1 answers

3
AnnoSiedler On BEST ANSWER

Your Code seems to be right. I don't think your mistake is in the code, you posted.

Please show us your TNT-class- and the class getBody() is referring. Do you use any static Methods/Variables? If for example setLinearVelocity() or the variable of your movement direction is static, this behaviour is normal, because static Methods/Variables are the same for all instances.

Are you sure, the two body-objets of both TNT classes are different? Check if tnt.get(0).getBody() != tnt.get(1).getBody().

Update: Forget everything:

You are calling "cl.isVänster()". What does this code mean? cl shouldn't change while running your for-loop, so cl.isVänster() and cl.isHöger() also returns the same value for every tnt-object.