Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Jan 07, 2013 Jan 07, 2013

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!

TOPICS
ActionScript
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 08, 2013 Jan 08, 2013

:

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(hea

...
Translate
Community Expert ,
Jan 07, 2013 Jan 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 07, 2013 Jan 07, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2013 Jan 07, 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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 07, 2013 Jan 07, 2013

I wouldn't need to created a shared object per body part being added? And would this be included in the class that has the slider?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2013 Jan 07, 2013

no, one sharedobject is all you need.  that one sharedobject can store lots of data.

the sharedobject is a global object so you just need to access it where your body parts exist when you want to re-construct your dino.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 07, 2013 Jan 07, 2013

Alright, that makes total sense...

This is the class with the shared object in it:

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];

                    }

          }

}

How would I add it in there? That's what I'm confused about

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2013 Jan 07, 2013

you're shared object is available everywhere.

so, in the class where you add body parts and want to store that info, use:

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

so.data.rightLeg=true;

so.flush()

and, in a class that has access to those body parts use:

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

if(so.data.rightLeg){

// add a right leg

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 07, 2013 Jan 07, 2013

Hmmm, I think I understand that, but I am only using one class which is:

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];

                    }

          }

}

And that adjusts the hue of the character.

When clicking a button in the Customize screen, it adds gear to the Head, Right and Left leg respectively. I want it to save with whatever gear is there and then load it next time it is accessed or launched in game

That is on the main actions frame of that fla

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2013 Jan 07, 2013

where's the code for the customize screen?  that's where you use:

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

if(so.data.rightLeg){

// add a right leg

}

// and when your dino is being customized, you'll use the following when a right leg is added.

so.data.rightLeg=true;

so.flush();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 07, 2013 Jan 07, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2013 Jan 08, 2013

:

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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 08, 2013 Jan 08, 2013

Alright. I'm currently away from my computer right now but I'll add that in when I get back to it. Now, adding that code will save it and then load it again when relaunched?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2013 Jan 08, 2013

yes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 08, 2013 Jan 08, 2013

Also, when I launch the game, would that load it as well or would I need something else in the class with the slider?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2013 Jan 08, 2013

if the code in that class executes when your game starts, it will load the dino body parts (if they were saved previously).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 08, 2013 Jan 08, 2013

Alright, so it works in the menu!! Saves and loads the previously selected outfit when closing and/or reaccessing the customize screen. However, when I run the game, it doesn't carry over the outfits. It carrys over the hue, not the outfit. Do I need to add anything specific to the class in which the hue is saved in order for it to carry over the outfit? Also, if adding more than one outfit, would I add the other like this:

if(so.data.head1){

loadPic1(null);

}

if(so.data.head2){

loadPic2(null);

}

and keep them to null?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2013 Jan 08, 2013

when your Add button is clicked and you run the game, does the dino have those added body parts?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 08, 2013 Jan 08, 2013

No it does not. So I'm thinking I need to add something to the class with the hue that gets carried over but not sure as to what?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2013 Jan 08, 2013
LATEST

then this is a different topic.  you should start a new thread.

i don't know/remember your setup well-enough to tell you how to integrate the code you showed in this forum with the code in your game.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines