Badfitz66 February 2016

Adding background at certain part

I'm updating my Text Adventure engine and want to add a background for each part of the story. The problem at the moment is making the backgrounds appear for their respective part.

public var parts:Object = 
{

    "0":
        {
            "text":"You're name is David, a agoraphobic 24 year old. It's 12:32 AM, you were woken abruptly by glass smashing inside your house.",
            "choices":
                {
                    "response1":
                        {
                            "text":"Investigate",
                            "nextPart":"1"
                        },
                    "response2":
                        {
                            "text":"Ignore it. You need your beauty sleep.",
                            "nextPart":"2"
                        }

                },
        },

What would I add so I can make a background appear at each part.

Here's the backgrounds I have currently (defined after parts)

[Embed(source = "../Backgrounds/old-bedroom.png")]
public var Background1:Class
public var Background1PNG:BitmapData 
public var BG1:Bitmap = new Background1
public var bR:Sprite = new Sprite();

[Embed(source = "../Backgrounds/img010.jpg")]
public var Background2:Class
public var Background2PNG:BitmapData 
public var BG2:Bitmap = new Background2
public var lR:Sprite = new Sprite();

Answers


Aaron February 2016

Since you are embedding your background images and your "parts" data structure, you can simply refer to your background classes in your parts object:

"0": {
    "text": "...",
    "choices": { ... },
    "background": Background1
}

Then in your code where you the update the game to some "part", display the background referenced by that part:

var background:Bitmap;
function updateBackground(part:Object):void {
    if (background) {
        // remove old background
        removeChild(background);
    }
    // add new background
    background = new part.background();
    addChild(background);
}

Post Status

Asked in February 2016
Viewed 1,231 times
Voted 13
Answered 1 times

Search




Leave an answer