it's a bit difficult to explain about this, I'm working on a slightly complicated game using score system made by me, I use the []
operator to call variable names like in this post Calling a variable given its string name
the goal is that I can change the SharedObject data as desired, first I create a variable data1, data2 etc...
var data1 = 0;
userData.data.Game1Score = data1;
var data2 = 0;
userData.data.Game1Score = data2;
// data3, data4, etc.
I create a variable so that I can change the data I want to use
var data:string;
on button i change data variable
// function to the Game
function BtnGame1(event:MouseEvent):void {
data = "data1";
}
function BtnGame2(event:MouseEvent):void {
data = "data2";
}
so that the variable data becomes :
trace(data);
// Output:
// data1
// or data2
i only want to have 1 page of score result, i changed data1 and data2 using []
operator
//Button Function to Page Level
function NextoPageLevel(event:MouseEvent):void {
this[data] = 3 //example if score 3
updateScore(); // function for Update SharedObject Data
}
then to update ShareObject I created a function :
function updateScore():void {
userData.data.Game1Score = data1;
userData.data.Game2Score = data2;
}
and the result is working!, SharedObject data is modified and saved, sorry if there is a mistake in English, I only use a translator, for more details I have a .fla file that can be downloaded here SetupScore.fla
if I make a lot of level games, there will definitely be a lot of syntax that I make, so I make it simple, but the code doesn't work
this["userData.data.Game1Score"];
OR
var data = "userData.data.Game1Score";
this[data] = 3;
I want the flow like this