Name based skins

Hello there,

I’m currently working on a 2d sandbox survival type game and I’m trying to implement a system that changes the player sprite based on the name you input. I’ve tried messing around with many different methods to do this, but none of them seem to be working. The current;y code I have for getting the string input is this:

public static String getUserName() {
	    if (username.getText() != null) {
	      return Main.player.username = username.getText();
	    }
	    return "";
	    }
	}

And here is the part that displays the name above the player and displays the sprite:

if (username != null) {
			if (Main.dir == movementSpeed) {
				g.drawImage(image);
			} else {
				g.drawImage(image);
			}
			g.setColor(Color.WHITE);
			g.setFont(Main.size8);
			g.drawString(username, (int)x - (int)Main.sX - username.length() * 3 / 2, (int)y - (int)Main.sY);

		} else {
			System.out.println("null");
		}

Thank you for any possible feedback and help :slight_smile:
*Should be noted that I am not using any libraries

I’m curious. What exactly is the issue? Are you just trying to fish for some inspiration and think of ways to associate a name with a Sprite? Or is there actually something going wrong in those snippets?

Hi,

Sorry, I don’t really get where your problem is or rather it looks like you don’t do what you intend to at all ???
For example you do the same thing in your if and else block.

Anyways, I guess what you are trying to do can be achieved with something like this:


if (username != null) {
    
    switch(username) {
        case "superman":
            g.drawImage(supermanImage);
            break;
        case "batman":
            g.drawImage(batmanImage);
            break;
        default:
            g.drawImage(noCostumeImage)
    }

    g.setColor(Color.WHITE);
    g.setFont(Main.size8);
    g.drawString(username, (int)x - (int)Main.sX - username.length() * 3 / 2, (int)y - (int)Main.sY);

} else {
    System.out.println("null");
}

Thank you so much! That did work! I never though of doing it that way. And in response to your questions. I knew what I was doing somewhat. I knew what I wanted to do. I was just having problem implementing it. But thank you both :slight_smile: