Skip to main content
Anonymous507
Known Participant
August 23, 2011
Question

how to store multiple data's in an object

  • August 23, 2011
  • 2 replies
  • 710 views

i am preparing a game, the game has four bet spots. i want to save the user bet details. i like to save the user bet details in an object, so i can access it whenever i need.


I have done same thing in AS 2.0. my object will look like

player[0].betSpot1.amount="100"

player[0].betSpot2.amount="200"

i am not sure to do this in AS 3.0. any help pls

Thanks in advance

This topic has been closed for replies.

2 replies

relaxatraja
Inspiring
August 23, 2011
var myAssocArray:Object = {fname:"John", lname:"Public"};
trace(myAssocArray.fname);     // John
trace(myAssocArray["lname"]);  // Public
Anonymous507
Known Participant
August 23, 2011

i want this for dynamic not static

Ned Murphy
Legend
August 23, 2011

Have you tried to do it the same way yet?  It is not clear from your code what you have supporting it, but Objects can still be used in AS3 as well as Arrays, so what you currently use might be a cut and paste from AS2 to AS3.

Anonymous507
Known Participant
August 23, 2011

below is my code

i have four bet spot button in a stage. whenever i click the btn i need to save them in an object with its betspot name and id


import flash.events.MouseEvent;

var gameDetails:Object = new Object
var player


betSpot.mouseChildren = true
betSpot.addEventListener(MouseEvent.CLICK, onBtnClick);

function onBtnClick(e:MouseEvent){
var btn = e.target.name
var spot = String(btn).split("_")[0]
var spotId =String(btn).split("_")[1]
gameDetails.player[spotId].betSpot = spot
gameDetails.player[spotId].betSpotId = spotId
}

Ned Murphy
Legend
August 23, 2011

What you show has errors even for AS2.  The most significant one being that your buttons are not being named to give you what your code is after.  Tr the following and rename your buttons to fit the intentions of the code...

var gameDetails:Object = new Object();
gameDetails.player = new Array(new Object());

betSpot_0.mouseChildren = true;
betSpot_0.addEventListener(MouseEvent.CLICK, onBtnClick);

function onBtnClick(e:MouseEvent){
    var btn = e.currentTarget.name;
    var spot = String(btn).split("_")[0];
    var spotId =String(btn).split("_")[1];
    gameDetails.player[spotId].betSpot = spot;
    gameDetails.player[spotId].betSpotId = spotId;
}

edit:  after rethinking what I offered, I believe it is also off the mark, but I also think there is likely a much simpler data structure you can use for whatever purpose this data is going to serve.  If you can explain what the data needs to provide, it might be possible to help you define a simpler structure.  A bit of what you showed originally, an array of objects, would seem much more suitable.