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

Loading previously Saved objects into separate FLA's

New Here ,
Jan 11, 2013 Jan 11, 2013

Hey everyone, I had a similar discussion about a similar problem I was having. But this time, I need to load previously saved object(s) into a separate FLA when the game is launched. Here's a little run down (for those of you who don't already know). Within the customize screen I have color selection and gear to add to the character. The color saves and loads when reopened or launched into the game. The gear saves and loads when reopened but NOT launched into game. I have a class for the slider which is attached to character in the Customize FLA as well as the Game FLA. (Separate FLA's).

The class looks like this:

package

{

          import flash.display.MovieClip;

          import fl.motion.AdjustColor;

          import flash.filters.ColorMatrixFilter;

          import flash.net.SharedObject;

          public class Dino extends MovieClip

          {

                    private var color:AdjustColor = new AdjustColor();

                    private var filter:ColorMatrixFilter;

                    private var so:SharedObject;

                    public function Dino():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];

                    }

          }

}

This saves and loads the adjusted color of the Character through separate FLA's.

The code for the Customize FLA is as follows:

import flash.events.KeyboardEvent;

var Head:Loader = new Loader();

var RightLeg:Loader = new Loader();

var LeftLeg:Loader = new Loader();

var Head2:Loader = new Loader();

var RightLeg2:Loader = new Loader();

var LeftLeg2:Loader = new Loader();

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

if(so.data.gear1){

loadPic1(null);

}

if(so.data.gear2){

loadPic2(null);

}

Add.addEventListener(MouseEvent.CLICK, loadPic1);   

Remove.addEventListener(MouseEvent.CLICK, loadPic2);    

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

          Head2.unload();

          RightLeg2.unload();

          LeftLeg2.unload();

          so.data.gear1=true;

          so.data.gear2=false;

          so.flush();

}

function loadPic2(MouseEvent):void {

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

    Head2.load(headRequest);

          Head2.y = -150;

    Sliders.Dino.Head.addChild(Head2); 

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

    RightLeg2.load(rightRequest);

    Sliders.Dino.RightLeg.addChild(RightLeg2);

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

    LeftLeg2.load(leftRequest);

    Sliders.Dino.LeftLeg.addChild(LeftLeg2);

          Head.unload();

          RightLeg.unload();

          LeftLeg.unload();

          so.data.gear1=false;

          so.data.gear2=true;

          so.flush();

}

Accept.addEventListener(MouseEvent.CLICK, AcceptImage);

PlayBTN.addEventListener(MouseEvent.CLICK, PlayGame);

function AcceptImage(MouseEvent)

{

          MovieClip(stage.getChildAt(0)).loadThisScreen("Menu.swf");

}

function PlayGame(MouseEvent)

{

          MovieClip(stage.getChildAt(0)).loadThisScreen("Level Select.swf");

}

The menu is split up into separate FLA's and it uses a preloader to navigate from one SWF to another. Customize and Game are separate FLA's.

The code for the Game FLA looks like this:

import flash.events.Event;

import flash.events.KeyboardEvent;

var music:Sound = new Sound();

var musicChannel:SoundChannel = new SoundChannel();

var lastPosition:Number = 0;

music.load(new URLRequest("Bongo.mp3"));

musicChannel = music.play();

stage.frameRate = 0;

var spd:Number = 1;

var vel:Number = 0;

var Animation:String;

var stop_where;

var gravity:Number = .9;

var ay = 0;

var jumpCounter:Number = 0;

var worldSpeed:Number = 10;

var canJump:Boolean = false;

var jumping:Boolean = false;

var isRunning:Boolean = false;

var score:int = 0;

          stop();

          Dino.gotoAndStop("running");

 

 

          ClickToPlay.addEventListener(MouseEvent.CLICK, gamestart);

 

          function gamestart(event:MouseEvent):void

{

          stage.frameRate = 60;

          ClickToPlay.visible = false;

}

//Randomly Generated Platform stuff!!

var showMcNum:Number = 0;

var movieList:Array = [LevelOne];

var mc:MovieClip = getRandomMovie();

function getRandomMovie():MovieClip

{

    return new movieList[Math.floor(Math.random()*movieList.length)];

}

addEventListener(MouseEvent.CLICK, Click);

function Click(event:MouseEvent):void

{

          trace("Added")

   

    addChild(mc);

    mc.x = 4800;

    mc.y = 380;

 

 

 

}

 

 

          //End Randomly Generated Platform Stuff!!

function main(e:Event):void

{

     if(Ground.x < -2200)

          mc.x -= worldSpeed;

          scoreBox.text = score.toString();

 

 

          Ground.x -= worldSpeed;

 

//Ground Movement in main loop

//GP means Ground Piece

          vel += spd;

          vel *= gravity;

 

          Dino.y += vel;

   

switch(Animation){

 

    case "Idle":

          Dino.gotoAndStop("running");

          break;

  case "Down":

 

   Dino.gotoAndStop("rolling")

 

  break;

 

}

}

//Key down code

stage.addEventListener(Event.ENTER_FRAME, main);

stage.addEventListener(KeyboardEvent.KEY_DOWN, movement);

function movement(e:KeyboardEvent):void{

 

          if(e.keyCode == Keyboard.SPACE && canJump) {

      

        jump();

                    //Animation = "Up";

                    canJump = false;

    }

 

if(e.keyCode == Keyboard.DOWN && Dino.currentLabel!="rolling"){

  

             Animation = "Down";

  

}

if(e.keyCode == Keyboard.UP){

//Animation = "Up";

jump();

 

}

  trace (Animation);

}

Jump.addEventListener(MouseEvent.CLICK, GetUp);

function GetUp(event:MouseEvent):void

{

           //Animation = "Up";

          jump();

}

Roll.addEventListener(MouseEvent.CLICK, GetWet);

function GetWet(event:MouseEvent):void

{

 

          roll();

}

// Key up code

stage.addEventListener(KeyboardEvent.KEY_UP, stopMovement);

function stopMovement(e:KeyboardEvent):void{

          //Dino.gotoAndStop("running");

 

 

 

 

if(e.keyCode == Keyboard.DOWN){

  //Animation = "Down";

}

if(e.keyCode == Keyboard.UP){

Animation = "Idle";

}

  trace (Animation);

 

}

function jump():void {

   if ( canJump == true){

   trace("jump");

          canJump = false;

            vel = -20;

            jumping = true;

          Animation = "Idle";

   }

 

}

function roll():void {

   if (Dino.currentLabel!="rolling"){

   

          Animation = "Down";

 

   }

 

}

Go.addEventListener(MouseEvent.CLICK, StartPlaying);

function StartPlaying(event:MouseEvent):void

{

          Pause.visible = true;

          Go.visible = false;

          stage.frameRate = 60;

          function playAll(displayObject:DisplayObject):void

          {

                    if(displayObject is MovieClip)

                    {

                              var Dino:MovieClip = displayObject as MovieClip;

                              Dino.play();

 

                              for(var i:uint =0; i < Dino.numChildren; i++)

                              {

                                        playAll(Dino.getChildAt(i));

                              }

                    }

          }

          playAll(Dino);

}

Pause.addEventListener(MouseEvent.CLICK, StopPlaying);

function StopPlaying(event:MouseEvent):void

{

          Pause.visible = false;

          Go.visible = true;

          stage.frameRate = 0;

 

          function stopAll(displayObject:DisplayObject):void

          {

                    if(displayObject is MovieClip)

                    {

                              var Dino:MovieClip = displayObject as MovieClip;

                              Dino.stop();

 

                              for(var i:uint =0; i < Dino.numChildren; i++)

                              {

                                        stopAll(Dino.getChildAt(i));

                              }

                    }

          }

          stopAll(Dino);

}

SoundOn.addEventListener(MouseEvent.CLICK, MusicOn);

function MusicOn(event:MouseEvent):void

{

          SoundOff.visible = true;

          SoundOn.visible = false;

          lastPosition = musicChannel.position;

          musicChannel.stop();

}

SoundOff.addEventListener(MouseEvent.CLICK, MusicOff);

function MusicOff(event:MouseEvent):void

{

          SoundOff.visible = false;

          SoundOn.visible = true;

          musicChannel = music.play(lastPosition);

 

}

Retry.addEventListener(MouseEvent.CLICK, Reset);

function Reset(event:MouseEvent):void

{

                    stage.removeEventListener(KeyboardEvent.KEY_DOWN, movement);

                    stage.removeEventListener(KeyboardEvent.KEY_UP, stopMovement);

                    stage.removeEventListener(Event.ENTER_FRAME, main)

                    MovieClip(stage.getChildAt(0)).loadThisScreen("Menu.swf");

}

This all works perfectly fine and executes the way I want it to. It loads in the color from the Class in both the Customize and Game. The gear loads in the Customize but NOT the game.

All I need is to get it load in-game and I have been trying to get this to work for weeks. I have tried everything. Anybody out there know what I need to add in order for the gear to be loaded into my game and not just my Customize screen?! Any help is greatly appreciate. Thank you all in advanced!

TOPICS
ActionScript
1.8K
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

LEGEND , Jan 11, 2013 Jan 11, 2013

Can we hack this down a bit to just the relevant code? Since a Loader can be in a different context and SO's work a bit similar to cookies, you might be hitting a sandbox issue. Have a look at the LoaderContext and make sure the main game and the loaded modules are all in the same context so the SO is available to them all. If the path is "/", it should be, but this may help.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/LoaderContext.html

Some domain documentati

...
Translate
LEGEND ,
Jan 11, 2013 Jan 11, 2013

Can we hack this down a bit to just the relevant code? Since a Loader can be in a different context and SO's work a bit similar to cookies, you might be hitting a sandbox issue. Have a look at the LoaderContext and make sure the main game and the loaded modules are all in the same context so the SO is available to them all. If the path is "/", it should be, but this may help.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/LoaderContext.html

Some domain documentation on contexts and their differences:

http://help.adobe.com/en_US/as3/dev/WSd75bf4610ec9e22f43855da312214da1d8f-8000.html

Good reading but start with pointing out where you're failing to receive the SO in the main game as mentioned because in my extremely brief scan of the code I didn't even see the main game attempt to access the SO at all. It'd be wise to use a single model anyhow, which holds all the apps data  (via custom event or exposing public properties), so it's all managed and accessed from a single place (similar to a registry/singleton). 

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 11, 2013 Jan 11, 2013

That's the thing, I didn't try loading in the SO in my game cause I don't know how. I don't know whether or not I need to put it in the Game FLA and or in the class attached to the character. I don't know what I need and/or were to put it, which is why I'm coming to you professionals for help!

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
LEGEND ,
Jan 11, 2013 Jan 11, 2013

You'd load it in your main game the same way you load it in your other classes, var so:SharedObject=SharedObject.getLocal("myStuff","/");.

Shared objects that are saved locally via getLocal/flush act similar to cookies. They write a limited amount of data to the local disk for later usage.

Have you done this and had issues accessing it?

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 11, 2013 Jan 11, 2013

Correct. I use that code and it doesn't do anything. I have the customize screen add the gear to specific body parts when clicking a button. It saves it and loads it when reopened, it just doesn't load into the main game and that's what's I really need. I feel so close but yet so far 😞

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
LEGEND ,
Jan 11, 2013 Jan 11, 2013

What does your var 'so' trace as? After you define it in the main game, add a breakpoint under it, run the debugger (CTRL+SHIFT+ENTER on win) and examine the 'so' variable for its contents or any raised exceptions.

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 11, 2013 Jan 11, 2013

So I just tried that and it has nothing for the var of 'so'. Now I'm confused! Do I add anything to the class that save and loads the characters color into the main game or add it to the game fla? I'm so confused 😞

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
LEGEND ,
Jan 11, 2013 Jan 11, 2013

Here's a quick sample of a loaded SWF (Config.swf) creating/saving a SO value while a Game class (Game.swf) loads the config.

Make a new AS3 doc called Game.fla and in frame 1:

import flash.net.SharedObject;

import flash.display.Loader;

import flash.events.Event;

// load Config which sets SO

var config:Loader = new Loader();

config.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

config.load(new URLRequest('Config.swf'));

 

function onComplete(e:Event):void

{

          // add listener for "SOSaved" Event

          config.content.addEventListener("SOSaved", soSaved);

 

          // once added, fires off save

          addChild(config);

}

function soSaved(e:Event):void

{

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

          trace("myVar, saved from Config.swf: " + so.data.myVar);

 

          // change it

          so.data.myVar = 456;

          so.flush();

 

          // ask Config to broadcast myVar

          config.content.dispatchEvent(new Event("PleaseBroadcast"));

}

Make a second new AS3 doc "Config.fla" in the same folder. Add this to frame 1:

import flash.net.SharedObject;

import flash.events.Event;

addEventListener(Event.ADDED_TO_STAGE, onAdded);

addEventListener("PleaseBroadcast", onBroadcast);

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

function onAdded(e:Event):void

{

          trace("Config.swf setting myVar to 123");

          so.data.myVar = "123";

          so.flush();

 

          dispatchEvent(new Event("SOSaved"));

}

function onBroadcast(e:Event):void

{

          trace("Config myVar: " + so.data.myVar);

}

Game loads Config in a Loader (note no actual error checking is done, just for ease). Once Config.swf is loaded it adds it to stage.

The adding to the stage fires off an Event.ADDED_TO_STAGE event in Config which makes it write a SO. After that it dispatches an event back to Game to let it know the SO is saved.

Game receives the event that the SO is saved and traces the data. It should be what Config set it to, 123.

Game then sets the data to 456 and dispatches an event back to Config when done.

Config gets the event which runs a function to trace the new value of the SO. It traces:

Config.swf setting myVar to 123

myVar, saved from Config.swf: 123

Config myVar: 456

Line 1 comes from Config where it sets the SO. Line 2 comes from Game which traces the SO's value. Line 3 comes from Config again after Game sets it. You can see Config and Game are setting the same data object successfully.

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 11, 2013 Jan 11, 2013

Understandable. But what I don't get is, why does the color carry over and not the gear? The gear gets added to the image when a button is clicked. I don't know what I'm missing and where I'm missing it to make it load the previously saved gear. Like I said, the adjusted color loads perfectly fine (except on mobile). The gear loads what was previously saved when going back to the customize screen. What do I need to make it load in the game? Do I put it in a class? I don't know!

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
LEGEND ,
Jan 11, 2013 Jan 11, 2013

Mine isn't in a class, just frame scripts. Extending the example above, I have no issue saving what you're saving, Boolean values in the SO:

Config changes:

function onAdded(e:Event):void

{

          trace("Config.swf setting myVar to 123, gear1 to false, gear2 to true");

          so.data.myVar = "123";

          so.data.gear1 = false;

          so.data.gear2 = true;

          so.flush();

 

          dispatchEvent(new Event("SOSaved"));

}

function onBroadcast(e:Event):void

{

          trace("Config myVar: " + so.data.myVar + ", gear1: " + so.data.gear1 + ", gear2: " + so.data.gear2);

}

Game changes:

function soSaved(e:Event):void

{

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

          trace("Game myVar: " + so.data.myVar + ", gear1: " + so.data.gear1 + ", gear2: " + so.data.gear2);

 

          // change it

          so.data.myVar = 456;

          so.data.gear1 = true;

          so.data.gear2 = false;

          so.flush();

 

          // ask Config to broadcast myVar

          config.content.dispatchEvent(new Event("PleaseBroadcast"));

}

Changed trace:

Config.swf setting myVar to 123, gear1 to false, gear2 to true

Game myVar: 123, gear1: false, gear2: true

Config myVar: 456, gear1: true, gear2: false

All 3 vars are being updated properly. It's just a frame script (try it), no need for a class to make it work.

Can you show the relevant line of code where you're unable to get the data you want.

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 11, 2013 Jan 11, 2013

Well, the reason why I'm using a class is because I'm using a slider to adjust the hue of the character in the customize screen which gets saved and then loaded when the game gets launched. That's the only class I have. I just don't know how to load the gear that's saved when the game is launched and not just in the customize screen. How do I access it?

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
LEGEND ,
Jan 11, 2013 Jan 11, 2013

All I see is a Dino class. The changes are getting a little too long to keep posting so I'll just post a CS4 saved down sample. I added a Dino class that just adds more to the daisy chain.

Example Source

After Config receives the event from Game to broadcast what its SO is I instantiate the Dino class inside Config and add it to the display list.

e.g. Config.fla

function onBroadcast(e:Event):void

{

          trace("Config myVar: " + so.data.myVar + ", gear1: " + so.data.gear1 + ", gear2: " + so.data.gear2 + ", now loading Dino");

          var d:Dino = new Dino();

          // listen for dino to dispatch

          d.addEventListener("SOClassSave", onDino);

          addChild(d);

}

Once added the Dino class grabs the SO, traces the value, changes it and dispatches a "SOClassSave" Event back to Config. Config receives the event and prints out what the SO currently is after Dino class changes it.

Example trace:

Config.swf setting myVar to 123, gear1 to false, gear2 to true

Game myVar: 123, gear1: false, gear2: true

Config myVar: 456, gear1: true, gear2: false, now loading Dino

Dino, added to stage, myVar: 456, gear1: true, gear2: false

Config, onDino, myVar: 789, gear1: false, gear2: true, now changing and dispatching back to Dino

Dino, dinoFinish,  myVar: 999, gear1: true, gear2: false

I can't think of any more ways to use the SO . That's SWF(frame script)<->Loader<->SWF(frame script)<->Class.. I have no issues with it, the SO is changed accurately every time.

Are you sure you're in the same sandbox on all your files? I'm using access network files only in publish on Game.fla and Config.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
New Here ,
Jan 11, 2013 Jan 11, 2013

This all looks so easy to understand but then when I think about how to incorporate it into my game, I get so confused. Is there anyway I can send you my files so you can run it and see what I'm talking about? I would upload them here but I don't feel 100% safe doing so. You definitely seem extremely experienced and I would love it if you could test my game for me, if possible. Yes, there is only one class. It's for the Dino characters adjusted hue which gets changed by 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
LEGEND ,
Jan 11, 2013 Jan 11, 2013

If it's not to complicated I can take a quick look. Upload it to a filesharing service like Dropbox and PM me a link if you don't want to post it here.

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 11, 2013 Jan 11, 2013
LATEST

Alright, I just PMed you the link. It seems like a really basic thing to accomplish, I just can't figure it out! I hope you can help!

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