1
\$\begingroup\$

I have several objects which are assigned a certain size and position, works fine. The only problem is that in certain special cases the item has to be positioned a bit to the left and be a bit smaller; I solved this problem as follows:

private void initSprites(){
    sprites = new HashMap<String, Sprite>();
    sprites.put("lock", new Sprite(new Texture(Gdx.files.internal("lock.png"))));
    sprites.put("pauseMenu", new Sprite(new Texture(Gdx.files.internal("pauseMenu.png"))));
    for(int x = 0; x < creators.size(); x++){
        Sprite tmp = new Sprite(new Texture(Gdx.files.internal("sprites/"+creators.get(x)+".png")));
        tmp.setSize(creators.get(x).equals("stick")?10f:20f, 20f);
        sprites.put(creators.get(x), tmp);
    }
}

and

private void addCreator(String name, Stage stage, int location){
    Sprite spr = sprites.get(name);//here
    Box2DObjectCreator act = new Box2DObjectCreator(locations[location]+(name.equals("stick")?4f:0f), items.get(name), spr, stage, definitions.get(name), camera);
    act.setBounds(act.getX(), act.getY(), spr.getWidth(), spr.getHeight());
    creatorObjects.add(act);
    table.addActor(act);
}

The code works obviously but there are going to be a couple of special cases and the only way I can check for those cases is comparing a string which seems a little ugly. Is there a cleaner way to do this besides something like

   private int method(string str)
            return (str.equals("specialCase")?10:20);

or a method that uses switches with all special cases?

That doesn't seem too efficient but seems like my only option if I'm going to have several special cases (which I will, something like 40 normals and 5-10 special cases).

\$\endgroup\$
  • 1
    \$\begingroup\$ Could you post your Sprite class? I suspect that the solution lies there. \$\endgroup\$ – 200_success Jan 12 '16 at 3:32
  • 2
    \$\begingroup\$ The Sprite class is the one from libGDX and it's here \$\endgroup\$ – Vacster Jan 12 '16 at 3:34
  • \$\begingroup\$ Wouldn't this be more suited for stackoverflow? it's not as much a review question as a programming question \$\endgroup\$ – Tschallacka Jan 12 '16 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.