Actionscript/Shared Object issue

  • Started
  • Last post
  • 4 Responses
  • CyBrain

    I was wondering if anyone could decipher the problem I'm having trying to establish a Shared Object

    I have 36 features that may or not be set as favorites. I'd like to save an array of booleans (isFavoriteArray_so.data.f_array... to save the favorites for future use when returning to the page.

    http://pastebin.com/f65a66388

    When I test the movie I trace favTotal = NaN and isFavoriteArray_so.data.f_array traces out false 36 times.

    If I try this conditional
    if (isFavoriteArray_so.data.f_array == false) { I still get the same results as before.

    If I try this conditional
    if (isFavoriteArray_so.data.f_array == NaN) { I get Warning: 1098: Illogical comparison with NaN. This statement always evaluates to false. which doesn't seem very fair since it's tracing to NaN anyway.

    In any of these cases, none of the code inside the if statement is executed. My first question is what should I do to test if the shared object exists?

  • thatblokemike0

    you can check the folder where shared objects are stored... not sure if your on mac or pc, but google it and find where that is, somewhere deep in your computer.

    or call SharedObject.getLocal( COOKIE_NAME, "/" );

    I just modded a class I have that does similar to suit your needs... altho I admit untested in this case as just opened my class and stripped it down, it should provide a way to get your prob sussed and may work off the bat.

    package com.yoursite.utils
    {
    import flash.events.EventDispatcher;
    import flash.net.SharedObject;

    /**
    * @author mike
    *
    * simple class for getting setting or clearing the users data
    *
    */
    public class UserSharedObject extends EventDispatcher
    {

    /**
    * the name of the sharedobject that we're placing on the users machine
    */
    public static const COOKIE_NAME:String = "mywebpage.mywebsite.com";

    /**
    * Stores the arrays
    * @param myBoolean - a boolean to store in the array
    * @param reference - a hash for storage
    */
    public static function setData( myBoolean:Boolean, reference:String ) : void
    {
    var _so:SharedObject = getUserData();

    var items:Array = _so.data.items;

    try
    {
    // check if the reference already exists and replace it
    for( var i:int=0; items.length; i++ )
    {
    if(items[i].reference == reference)
    {
    _so.data.items[i] = { reference:reference, myBoolean:myBoolean };
    _so.flush();
    return;
    }
    }

    }catch( e:Error )
    {
    trace( "ERROR: there was no exisiting references" );
    }

    _so.data.items = [];
    _so.data.items.push( { reference:reference, myBoolean:myBoolean } );
    _so.flush();
    }

    /**
    * gets a specific boolean
    */
    public static function getDataByReference( reference:String ):Boolean
    {
    var _so:SharedObject = getUserData();

    var items:Array = _so.data.items;

    try
    {
    for( var i:int=0; i<items.length; i++ )
    {
    if( reference == items[i].reference )
    {
    return items[i].myBoolean;
    }
    }
    }catch( e:Error )
    {
    trace("ERROR: shared object didnt have a array length");
    }

    return false;
    }

    /**
    * Returns the entire shared object
    */
    public static function getUserData() : SharedObject
    {
    return SharedObject.getLocal( COOKIE_NAME, "/" );
    }

    /**
    * clears the users shared object
    */
    public static function clearUserData() : void
    {
    var _so:SharedObject = SharedObject.getLocal( COOKIE_NAME, "/" );
    _so.clear();
    }

    }
    }

    • I have to admit that is a bit over my head. I'll head to the gym and come back with a fresh look. Thanks for the help.CyBrain
  • acescence0

    I'm not sure I understand what you're doing here, you're checking if isFavoriteArray_so.data.f_array is undefined, then trying to read its contents if it is?

  • instrmntl0

    for the NaN error, do:

    if( isNaN( isFavoriteArray_so.data.f_array ) )
    {

    }

  • instrmntl0

    dunno if this helps but:
    http://pastie.org/791453