Hey,
I am currently working on a small sidescrolling Jump 'n' Run Game and have just added a horizontally scrolling "camera".
However recently I have run into some problems with this "camera"
The whole project already got a bit out of hand and complicated (atleast in my opinion). Therefore I will try to keep everything as simple as possible.
To make the camera work I created a Arraylist, to wich all the Actors are added with there corresponding X- and Y-coordinates.
A method checks in every frame for every Object if they are in the camera or not.
If they are not they will be removed.
If they are in the camera they will be removed and replaced at the position that is in the Arraylist
Therefore I have to change the X- and Y-coordinates if I have (for example) a moving Actor.
I already did that for my main player. However I only was able to because I made sure that he is in position "0" in this list before hand.
Now I want to add some basic enemies that walk in a constant speed, so their position in the Arraylist needs to be changed too. So I need to find out in what position in the list they are.
However the way a friend of mine showed me does not work.
(
WorldActor is the Object the world is created in and contains the list.
getIndexOfObject() is a method in WorldActor
obj is the name of the Arraylist
)
In this case the game crashes, because it thinks index is -1....
Every single idea is much appreciated.
Tritos
int index = getWorld().getObjects(WorldActor.class).get(0).getIndexOfObject(this);
public int getIndexOfObject (Object o) { return obj.indexOf(o); }