This site requires JavaScript, please enable it in your browser!
Greenfoot back
nicknl
nicknl wrote ...

2014/10/29

Set a rotation to a arraylist item

nicknl nicknl

2014/10/29

#
Hello all, We have an assignment for school and we need some help. One restriction is that we have to use an arraylist. We have the following arraylist: ArrayList<Tankkogel> kogelLijst = new ArrayList<Tankkogel>(); // Tankkogel means Tank bullet in English. The arraylist is already working and we can fire some bullets from the tank. We are firing the bullets with the following code: getWorld().addObject (kogelLijst.get(0), getX(), getY()); Ok, our problem: When we are firing some bullets, they all have the same rotation. We have a constructor in the class Tankkogel: public Tankkogel(int rot) { //Rotatie meegeven aan de kogel hoe de tank staat setRotation(rot); } We can use the setRotation function, but how can we give the parameter "rot" to the constructor with the following code: getWorld().addObject (kogelLijst.get(0), getX(), getY()); Thank you very much for your interest and we hope we can find the solution in this topic. Kind regards, Nick and Mikey (HHS)
erdelf erdelf

2014/10/29

#
well, u need to create the Objects in the list somehow before that, just write it there. something like this
kogelLijst.get(0) = new Tankkogel(50);
danpost danpost

2014/10/29

#
nicknl wrote...
We can use the setRotation function, but how can we give the parameter "rot" to the constructor with the following code:
getWorld().addObject (kogelLijst.get(0), getX(), getY());
There is no way to execute the constructor for an object that has already been created. However, you can just add another line with the line above to set the rotation:
kogelLijst.get(0).setRotation(getRotation());
getWorld().addObject(kogelLijst.get(0), getX(), getY());
nicknl nicknl

2014/11/25

#
Hi Erdelf, Thank you very much for your reaction. I understand your code and the way how you will fix this problem. We have to use the set function (http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - Replaces the element at the specified position in this list with the specified element.) Because of you we came to this solution. However, we have the following error code if we use this code: Kind regards, Nick
You need to login to post a reply.