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

2019/6/6

Character selection for more than one player

fabiososo fabiososo

2019/6/6

#
Hi all, I'm working on a racing game (the scenario should be in my profile), and it's a player versus player game. I want to add different cars, which will have different speeds/abilities, as well as have different cosmetics. This I can manage myself, as well as a character selection screen, but I want to know how I would be able to instantiate the new vehicles into the main race track. Since WASD controls player 1, and the arrow keys control player 2, I don't know how to assign the cars to the certain controls. Any help is appreciated! (If the source code would be useful for you to have, or if I didn't explain well enough, please let me know)
Super_Hippo Super_Hippo

2019/6/6

#
I think something like this should work:
//in Car
private String[] movementKeys;

public Car(String[] keys)
{
    movementKeys = keys;
}

public void act()
{
    for (int i=0; i<4; i++)
    {
        if (Greenfoot.isKeyDown(movementKeys[i])
        {
            switch (i)
            {
                case 0:
                //accelerate
                break;
                
                case 1:
                //break
                break;
                
                case 2:
                //turn left
                break;
                
                case 3:
                //turn right
                break;
            }
        }
    }
}
And then, when creating the Cars:
new Car(new String[] {"w", "s", "a", "d"});
new Car(new String[] {"up", "down", "left", "right"});
You need to login to post a reply.