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

2017/3/24

How do I change the Layer?

11870 11870

2017/3/24

#
Hello everyone, I am closing up on my first significant game, and there are a few glitches to fix here and there. One of them the character's layer. I want him to appear in front of certain objects and behind others. Now I know that normally you change in Inspect Object or something but there's a catch. My game is in the same format as Minish Cap and Undertale, by which I mean that it's supposed to look 3d. So the objects with a lower y axis (higher up on the screen) are the ones that need to be farthest back. However, since my player is moving around, his y axis changes. So my idea for how to do this would be to set the layer to the y coordinate, but like I said, this is my first game. Can anybody help?
danpost danpost

2017/3/26

#
There is no easy fix for this issue -- and no documented way to solve it. There may be a way to accomplish it, still; however, it may also come at a cost -- lagging. That should not be too much of a problem if you do not have many actors in the world; but, with more and more actors, the more probable and pronounced the lag. It involves removing and adding actors into the world in the correct order (back to front). Obviously doing this every act cycles would be quite taxing on the CPU.
11870 11870

2017/3/26

#
Would there be a way to write it in the form of setPaintOrder(object with smallest y axis, object with largest y axis)? As for the lag, I only need to evaluate it between the Player and the objects he interacts with. So maybe this could be called in a collider?
danpost danpost

2017/3/26

#
11870 wrote...
Would there be a way to write it in the form of setPaintOrder(object with smallest y axis, object with largest y axis)?
Using 'setPaintOrder' only paints by Actor types, not by the value of a field within these actors.
As for the lag, I only need to evaluate it between the Player and the objects he interacts with. So maybe this could be called in a collider?
It is possible to manage this in this way; but I might question whether that would be better as far as lag is concerned. All objects in front of the player, before AND after the move, must be checked and for any actor that needs repainted, any and all of its intersecting objects in front of them must be repainted. The entire list of who needs repainted must be compiled first, then sorted, then repainted in proper order. It is this compiling of the list that would concern me as far as lag.
Super_Hippo Super_Hippo

2017/3/26

#
I just wrote that and I didn't test it, but in theory, it could work. (But as danpost pointed out, it could result in a lot of lag (even though he probably could find a more efficient code).)
public void act()
{
    ArrayList<Actor> actorListOriginal = new ArrayList(0);
    ArrayList<Actor> actorListOrdered = new ArrayList(0);
    ArrayList<Integer> yPosList = new ArrayList(0);
    for (Object obj : getObjects(null))
    {
        Actor a = (Actor) obj;
        actorListOriginal.add(a);
        yPosList.add(a.getY());
    }
    
    while (!actorListOriginal.isEmpty())
    {
        int y=100000, index=-1;
        for (int i=0; i<yPosList.size(); i++)
        {
            if (yPosList[i] < y)
            {
                index = i;
                y = yPosList[i];
            }
        }
        actorListOrdered.add(actorListOriginal.get(index));
        actorListOriginal.remove(index);
        yPosList.remove(index);
    }
    
    for (int i=0; i<actorListOrdered.size(); i++)
    {
        int x=actorListOrdered.get(i).getX(), y=actorListOrdered.get(i).getY();
        removeObject(actorListOrdered.get(i));
        addObject(actorListOrdered.get(i), x, y);
    }
}
11870 11870

2017/3/27

#
Thanks! I will do this.
Super_Hippo Super_Hippo

2017/3/27

#
The
yPosList[i]
in lines 18 and 21 should be
yPosList.get(i)
.
11870 11870

2017/3/27

#
Thanks! I got it to work.
You need to login to post a reply.