actionscript 3 - AS3: Issues with multiple save/load slots -
i'm trying take simple "game" , give 3 save/load slots. following separate tutorial can make work single save slot once try adding more, gives me following error message.
1046:type not found or not compile-time constant: save2.
1046:type not found or not compile-time constant: save3.
i new actionscript 3 i'm sure i'm being newbish have tried figure out quite time can't seem to. whole thing controlled buttons placed on scene. appreciate can get.
the code:
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"); }
i tried code , works charm...
anyways, i've made simpler version doesn't use many functions , events.
here pure as3 version of (just save test.as3 , use document class in flash), can copy content of test()
method , paste in action frame.
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 , 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]; (var i:int = 0; < 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 = * 30 + 25; addchild(item); } } /***** end: faking buttons , 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"); } } } }
Comments
Post a Comment