1
\$\begingroup\$

I want to be able to have a different background image at each level of my game. Presently I have only one. There is no layout folder shown in Android Studio and all info I have searched shows how to do it with the layout xml.

I have found this code in GameLayer.java of my game. Actually I am reskinning the game so everything is already there, I just need to know how to have different bg images now. Code:

// background
CCSprite bg = new CCSprite("game/game_bg.png");
bg.setPosition(G.displayCenter());
addChild(bg);
\$\endgroup\$
1
\$\begingroup\$

This is actually pretty straight forward. Instead of loading up an image from a static path, you would want to store up (the implementation depends on how you store your levels) a path to the correct background for the level along the other level data. As you are loading and creating your game objects, you would load the path to the background image and set that to be rendered behind the other objects. I'll leave the detailed implementation to you, as it is very trivial and depends on the way you store your level data.

In pseudo-code you the implementation would look something like the following. First you would add the background data to the level file. This example adds it to an XML level description file:

<Level>
    <Background src="backgrounds/forest.png"/>
    <Objects>...</Objects>
</Level>

You would then load the level data as usual, and while doing that, find the background filepath, and load the instead.

for (XmlElement element : elements)
{
    if (element.Tag == "Background")
    {
        backgroundImage = new Image(element.Attributes.get("src"));
    }
    ...
}
\$\endgroup\$
  • \$\begingroup\$ It's not that straight forward for me still. I know now that I am calling one static image but I do not know how to call different images with each level change. There is a gamelayer.java and a levelmap.java in which I feel like it can be done and maybe but I don't know where to put that code and which code to add. Game was made using cocos2d as there are files for it too. I've searched the internet but this is not so clearly explained or maybe I am just not getting it. I need more more guidance.... \$\endgroup\$ – Mohamed Aadhil Jun 25 '16 at 17:53
  • \$\begingroup\$ This really should be enough. What part exactly are you having trouble with? I cannot tell you anything game speficic on those files. I feel like you are asking for a full working intergration. \$\endgroup\$ – Tyyppi_77 Jun 25 '16 at 17:54
  • \$\begingroup\$ My bad. I should have chosen a source code which had all the features that I wanted in the first place. The first one I chose just didn't have the bells and whistles that I needed. I will try with others and learn from them again. Thanks though Tyyppi_77 :) \$\endgroup\$ – Mohamed Aadhil Jun 26 '16 at 6:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.