Skip to main content
Known Participant
January 7, 2013
Answered

Adding Objects to an Image then Saving and Loading it upon closing and re-opening

  • January 7, 2013
  • 1 reply
  • 2113 views

Hey everyone, as some of you may or may not know, I am currently in the process of making an Android and iOS game, using Flash and AS3. Within the menu you can customize what your character looks like by adjusting it with a color slider and adding gear to the character by selecting the gear you want. When clicking on a certain piece of gear to add to your character, it places 3 pieces of of gear onto your character: Head, Right Leg, and Left Leg, respectively. I do so by using the code below:

var Head:Loader = new Loader();

var RightLeg:Loader = new Loader();

var LeftLeg:Loader = new Loader();

Add.addEventListener(MouseEvent.CLICK, loadPic1);   

Remove.addEventListener(MouseEvent.CLICK, removePic1);   

function loadPic1(MouseEvent):void {

    var headRequest:URLRequest=new URLRequest("TestHead.png");

    Head.load(headRequest);

          Head.y = -150;

    Sliders.Dino.Head.addChild(Head); 

          var rightRequest:URLRequest=new URLRequest("TestRightLeg.png");

    RightLeg.load(rightRequest);

    Sliders.Dino.RightLeg.addChild(RightLeg);

          var leftRequest:URLRequest=new URLRequest("TestLeftLeg.png");

    LeftLeg.load(leftRequest);

    Sliders.Dino.LeftLeg.addChild(LeftLeg);

}

function removePic1(MouseEvent):void {

    Head.unload();

RightLeg.unload();

LeftLeg.unload();

}

This all works fine for test purposes. Is there anyway to add the gear directly to the image, save it at it's current state, then when reopened (or loaded into the game) it loads the previously saved image with gear on it?

Thanks in advance!

This topic has been closed for replies.
Correct answer kglad

I have this for the customize screen:

var Head:Loader = new Loader();

var RightLeg:Loader = new Loader();

var LeftLeg:Loader = new Loader();

Add.addEventListener(MouseEvent.CLICK, loadPic1);  

Remove.addEventListener(MouseEvent.CLICK, removePic1);  

function loadPic1(MouseEvent):void {

    var headRequest:URLRequest=new URLRequest("TestHead.png");

    Head.load(headRequest);

          Head.y = -150;

    Sliders.Dino.Head.addChild(Head);

          var rightRequest:URLRequest=new URLRequest("TestRightLeg.png");

    RightLeg.load(rightRequest);

    Sliders.Dino.RightLeg.addChild(RightLeg);

          var leftRequest:URLRequest=new URLRequest("TestLeftLeg.png");

    LeftLeg.load(leftRequest);

    Sliders.Dino.LeftLeg.addChild(LeftLeg);

}

function removePic1(MouseEvent):void {

    Head.unload();

RightLeg.unload();

LeftLeg.unload();

}

Which is on its own frame in its own FLA. Thats for adding it to the image and its specific parts.

And I have a separate class which has:

package

{

          import flash.display.MovieClip;

          import fl.motion.AdjustColor;

          import flash.filters.ColorMatrixFilter;

          import flash.net.SharedObject;

          public class DinoRoll extends MovieClip

          {

                    private var color:AdjustColor = new AdjustColor();

                    private var filter:ColorMatrixFilter;

                    private var so:SharedObject;

                    public function DinoRoll():void

                    {

                              color.brightness = 0;

                              color.contrast = 0;

                              color.saturation = 0;

                              so = SharedObject.getLocal("myStuff","/");

                              if (so.data.hue)

                              {

                                        color.hue = so.data.hue;

                              }

                              else

                              {

                                        color.hue = 0;

                              }

                              so.flush();

                              update();

                    }

                    private final function update():void

                    {

                              filter = new ColorMatrixFilter(color.CalculateFinalFlatArray());

                              this.filters = [filter];

                    }

          }

}

Would I add anything to the class and something different to the fla? If so, what?! I'm confusedddd


:

var Head:Loader = new Loader();

var RightLeg:Loader = new Loader();

var LeftLeg:Loader = new Loader();

var so:SharedObject=SharedObject.getLocal("myStuff","/");

// the way your code is setup, if you add one body part, you've added all three parts

if(so.data.head){

loadPic1(null);

}


Add.addEventListener(MouseEvent.CLICK, loadPic1);  

Remove.addEventListener(MouseEvent.CLICK, removePic1);  


function loadPic1(MouseEvent):void {

    var headRequest:URLRequest=new URLRequest("TestHead.png");

    Head.load(headRequest);

          Head.y = -150;

    Sliders.Dino.Head.addChild(Head);

          var rightRequest:URLRequest=new URLRequest("TestRightLeg.png");

    RightLeg.load(rightRequest);

    Sliders.Dino.RightLeg.addChild(RightLeg);

          var leftRequest:URLRequest=new URLRequest("TestLeftLeg.png");

    LeftLeg.load(leftRequest);

    Sliders.Dino.LeftLeg.addChild(LeftLeg);

so.data.head=true;

so.flush();

}

function removePic1(MouseEvent):void {

    Head.unload();

RightLeg.unload();

LeftLeg.unload();

so.data.head=false;

so.flush();

}

1 reply

kglad
Community Expert
Community Expert
January 7, 2013

you can save the data (eg, so.data.rightLeg) needed to inform flash what should be added to the character and then use code to interpret those data, eg, if(so.data.rightLeg), and add whatever to the character.

Known Participant
January 7, 2013

That's what I thought. I knew I had to use shared objects but wasn't too sure. I'm using a class to control the hue slider (which you helped me out with) that uses a shared object. I know I need one or in this case, probably multiple, but I don't necessarily know how to incorporate it. Which is why I came here as a last resort!

kglad
Community Expert
Community Expert
January 7, 2013

you only need one sharedobject.  you can add and retrieve all sorts of data.

just use a boolean to indicate a body part should be added:

// store data indicating a right left should be added

so.data.rightLeft=true;

so.flush()

after reading your so, use:

if(so.data.rightLeg){

// add the right leg to your dino

}