I'm not entirely sure how to best explain what I'm doing. Maybe I'm going about this completely the wrong way. If any of you find that that is in fact the case, I would appreciate it if you told me how I can do this better.
I've started looking into game development about a week ago and came across a cross-platform framework called LibGDX. So I started working on a practice project which is an incremental game. I'm using the sprites supplied by a program called BannedStory. I want every part of the player's body to be customizable. That means that I'm using every sprite's animations separately.
This is a snippet of code to illustrate how I'm building the character right now.
float playerScreenWidth = HackerStory.WORLD_WIDTH; float playerScreenHeight = HackerStory.WORLD_HEIGHT; float playerPositionX = playerScreenWidth*0.3f; float playerPositionY = playerScreenHeight*0.7f; manager = new AssetManager(); textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheets/body/light/walking.atlas")); bodyAnimation = new Animation(0.25f , textureAtlas.getRegions()); // textureAtlas.getRegions() bodyAnimationActor = new AnimatedImage(bodyAnimation); bodyAnimationActor.setPosition(playerPositionX, playerPositionY); float bodyWidth = bodyAnimation.getKeyFrame(0).getRegionWidth(); float bodyHeight = bodyAnimation.getKeyFrame(0).getRegionHeight(); float bodyX = bodyAnimationActor.getX(); float bodyY = bodyAnimationActor.getY(); Group body = new Group(); body.addActor(bodyAnimationActor); Group head = new Group(); textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheets/head/light/walking.atlas")); headAnimation = new Animation(0.25f , textureAtlas.getRegions()); // textureAtlas.getRegions() headAnimationActor = new AnimatedImage(headAnimation); headAnimationActor.setPosition(bodyX - bodyWidth * 0.05f, bodyY + bodyHeight * 0.87f); float headWidth = headAnimation.getKeyFrame(0).getRegionHeight(); float headHeight = headAnimation.getKeyFrame(0).getRegionHeight(); float headX = headAnimationActor.getX(); float headY = headAnimationActor.getY(); head.addActor(headAnimationActor); textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheets/face/male/002/walking.atlas")); faceAnimation = new Animation(0.25f, textureAtlas.getRegions()); faceAnimationActor = new AnimatedImage(faceAnimation); faceAnimationActor.setPosition(headX+headWidth*0.25f, headY + headHeight*0.1f); head.addActor(faceAnimationActor); textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheets/hair/male/003/black/walking.atlas")); hairAnimation = new Animation(0.25f, textureAtlas.getRegions()); hairAnimationActor = new AnimatedImage(hairAnimation); hairAnimationActor.setPosition(/*headX+headWidth*0.085f*/headX+hairAnimation.getKeyFrame(0).getRegionWidth()*0.04f, headY + headHeight*0.39f); head.addActor(hairAnimationActor); Group player = new Group(); player.addActor(body); player.addActor(head);
As you can see, I am currently setting the X position for (for example the hair) by using its parent's X co-ordinate plus a fraction of its own width. However, this causes issues when the hair is different widths. What works for one hair sprite, won't work for another.
I am at a loss for where to go next. I also couldn't find anything on the subject in other questions, so again, I might be completely wrong in my approach to this.
Does anyone have any idea as to how I could solve this issue?