在某个部分添加背景

时间:2016-02-08 16:43:13

标签: arrays actionscript-3

我正在更新我的Text Adventure引擎,并希望为故事的每个部分添加背景。目前的问题是让背景出现在各自的角色中。

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"
                        }

                },
        },

我会添加什么,以便我可以在每个部分显示背景。

这是我目前的背景(在部分之后定义)

[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();

1 个答案:

答案 0 :(得分:1)

因为您正在嵌入您的背景图片和您的部分"数据结构,您只需在parts对象中引用您的背景类:

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

然后在您的代码中,您将游戏更新为部分"部分",显示该部分引用的背景:

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