0

我正在尝试使用这个非常简单的“游戏”并为其提供三个保存/加载插槽。按照一个单独的教程,我可以使它与单个保存槽一起工作,但是一旦我尝试添加更多,它会给我以下错误消息。

1046:找不到类型或不是编译时常量:save2。

1046:找不到类型或不是编译时常量:save3。

我是 actionscript 3 的新手,所以我确定我很新手,但我已经尝试了很长时间,但似乎无法解决。整个事情由已经放置在场景中的按钮控制。我很感激我能得到的任何帮助。

编码:

import flash.net.SharedObject;

var saveDataObject:SharedObject;
var currentScore:Number = 0

init();

function init():void{

btnAdd.addEventListener(MouseEvent.CLICK, addScore);

btnSave1.addEventListener(MouseEvent.CLICK, save1);
btnSave1.addEventListener(MouseEvent.CLICK, saveData);

btnSave2.addEventListener(MouseEvent.CLICK, save2);
btnSave2.addEventListener(MouseEvent.CLICK, saveData);

btnSave3.addEventListener(MouseEvent.CLICK, save3);
btnSave3.addEventListener(MouseEvent.CLICK, saveData);

btnLoad1.addEventListener(MouseEvent.CLICK, save1);
btnLoad1.addEventListener(MouseEvent.CLICK, loadData);

btnLoad2.addEventListener(MouseEvent.CLICK, save2);
btnLoad2.addEventListener(MouseEvent.CLICK, loadData);

btnLoad3.addEventListener(MouseEvent.CLICK, save3);
btnLoad3.addEventListener(MouseEvent.CLICK, loadData);

}

function save1(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile1");
}

function save2(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile2");
}

function save3(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile3");
}

function addScore(e:MouseEvent):void{
currentScore += 1;
updateScoreText();
}

function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}

function loadData(e:MouseEvent):void{
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}

function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
4

1 回答 1

1

我试过你的代码,它就像一个魅力......

无论如何,我制作了一个不使用太多函数和事件的更简单的版本。
这是它的纯 AS3 版本(只需将其保存为 Test.as3 并在 Flash 中用作 Document Class),但您可以复制Test()方法的内容并粘贴到操作框架中。

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.net.SharedObject;
    import flash.text.TextField;

    public class Test extends Sprite
    {
        public function Test()
        {
            /***** START: Faking buttons and field *****/
            var txtScore:TextField = new TextField();
            addChild(txtScore);

            var btnAdd:Sprite = new Sprite();

            var btnSave1:Sprite = new Sprite();
            var btnSave2:Sprite = new Sprite();
            var btnSave3:Sprite = new Sprite();

            var btnLoad1:Sprite = new Sprite();
            var btnLoad2:Sprite = new Sprite();
            var btnLoad3:Sprite = new Sprite();

            var items:Array = [btnAdd, null, btnSave1, btnSave2, btnSave3, null, btnLoad1, btnLoad2, btnLoad3];

            for (var i:int = 0; i < items.length; ++i)
            {
                var item:Sprite = items[i];

                if (item)
                {
                    item.graphics.beginFill(Math.random() * 0xFFFFFF);
                    item.graphics.drawRect(0, 0, 100, 25);
                    item.graphics.endFill();

                    item.x = 25;
                    item.y = i * 30 + 25;

                    addChild(item);
                }
            }
            /***** END: Faking buttons and field *****/

            var saveDataObject:SharedObject;
            var currentScore:Number = 0

            btnAdd.addEventListener(MouseEvent.CLICK, addScore);

            btnSave1.addEventListener(MouseEvent.CLICK, save);
            btnSave2.addEventListener(MouseEvent.CLICK, save);
            btnSave3.addEventListener(MouseEvent.CLICK, save);

            btnLoad1.addEventListener(MouseEvent.CLICK, load);
            btnLoad2.addEventListener(MouseEvent.CLICK, load);
            btnLoad3.addEventListener(MouseEvent.CLICK, load);

            function getLocal(target:Object):String
            {
                if (target == btnSave1 || target == btnLoad1)
                {
                    return "savefile1";
                }
                else if (target == btnSave3 || target == btnLoad2)
                {
                    return "savefile2";
                }
                else if (target == btnSave2 || target == btnLoad3)
                {
                    return "savefile3";
                }
            }

            function save(e:MouseEvent):void
            {
                var local:String = getLocal(e.target);
                saveDataObject = SharedObject.getLocal(local);
                saveDataObject.data.savedScore = currentScore;
                trace("Data Saved!");
                saveDataObject.flush();
                trace(saveDataObject.size);
            }

            function load(e:MouseEvent):void
            {
                var local:String = getLocal(e.target);
                saveDataObject = SharedObject.getLocal(local);
                currentScore = saveDataObject.data.savedScore;
                updateScoreText();
                trace("Data Loaded!");
            }

            function addScore(e:MouseEvent):void
            {
                currentScore += 1;
                updateScoreText();
            }

            function updateScoreText():void
            {
                txtScore.text = ("Score: " + currentScore);
                trace("Score text updated");
            }
        }
    }
}
于 2013-05-19T22:21:16.133 回答