Skip to main content
Known Participant
January 11, 2013
Answered

Loading previously Saved objects into separate FLA's

  • January 11, 2013
  • 1 reply
  • 1970 views

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!

This topic has been closed for replies.
Correct answer sinious

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

1 reply

sinious
siniousCorrect answer
Legend
January 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). 

Known Participant
January 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!

sinious
Legend
January 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?