Blog posts


  • Error #2130 Unable to flush sharedObject

    November 20, 2009 | Tags: Actionscript, AS3, Troubleshooting, Flash

    This annoying error was shown recently in a Flash project I was working on. At first I thought it was a sandbox problem, but Googling around didn’t help. Some links said it was a Flash 10 problem, but I had been succesfully used almost the exact same code in another project.

    This is a shortened version of the code:

    var name:String = "/demosite/";
    var path:String = "/";
    var paramname:String = "test";
    var value:String = "myValue";

    // Open SharedObject
    var _so:SharedObject = SharedObject.getLocal(name, path);

    // Create object
    var o = new Object();
    o.value = value;
    o.timestamp = new Date();

    _so.data[paramname] = o;
    _so.flush(); // Flush to disc

    _so.close();

    Can you spot what I did wrong? If not, I’ll tell you. It’s the name that causes the “Error #2130 Unable to flush sharedObject” message. Or more exact, the “/” in the name. Replacing it with:

    var name:String = "demosite";

    ..solved the problem. I really like good descriptions in error messages ;)


  • Get correct week number in .NET

    November 20, 2009 | Tags: Morgan, ASP.NET, C#, Snippet
    A nice piece of code submitted by Morgan Johansson for getting the correct week number for your region (in this sample code Sweden ("se-SE"):
    public int GetWeekNo(DateTime date) {
    System.Globalization.CultureInfo cult_info = System.Globalization.CultureInfo.CreateSpecificCulture("se-SE");
    System.Globalization.Calendar cal = cult_info.Calendar;
    int weekNo = cal.GetWeekOfYear(date, cult_info.DateTimeFormat.CalendarWeekRule, cult_info.DateTimeFormat.FirstDayOfWeek);
    return weekNo;
    }

  • Extend string with own methods

    November 12, 2009 | Tags: ASP.NET, C#, Nils, Snippet
    Here's a nice little sample submitted by Nils Forsman on how to extend the String object.
    public static class StringExtension
    {
    public static String AddSlashAfter(this String input)
    {
    return String.Concat(input, @"\");
    }
    public static String AddSlashBefore(this String input)
    {
    return String.Concat(@"\", input);
    }

    public static String Left(this String input, Int32 length)
    {
    if (length > input.Length)
    {
    return input;
    }
    return input.Substring(0, length);
    }

    public static String Right(this String input, Int32 length)
    {
    if (length > input.Length)
    {
    return input;
    }
    Int32 StartPosition = input.Length - length;
    return input.S
    return input.Substring(StartPosition, length);
    }

    public static String Mid(this String input, Int32 start, Int32 length)
    {
    if (start > input.Length)
    {
    return String.Empty;
    }
    if ((start + length) > input.Length)
    {
    return input.Substring(start);
    }
    return input.Substring(start, length);
    }
    }

    You'll then able to access your new methods on any string object, for example filePath.AddSlashAfter()

  • Your own event in Flash AS3

    November 11, 2009 | Tags: AS3, Flash, Snippet
    Want to create an event of your own that can be raised and handled like the build in Flash? Well then you've landed on the right page. So here we go!

    First create your new event class, which extends flash.events.Event:
    package {
    import flash.events.Event;

    public class MyEvent extends Event {
    // Types of events that could be casted:
    public static const HUNGRY:String = "hungry";
    public static const HAPPY:String = "happy";
    public static const SAD:String = "sad";

    public function MyEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false){
    super(type, bubbles,cancelable);
    }
    }
    }

    Now you can setup event listeners for your new event:
    // more code above ..

    addEventListener(MyEvent.HUNGRY, goEatSome);

    // more code below ...


    public function goEatSome(e:MyEvent) {
    // more code ...
    }

    And finally, we need to dispatch the event:
    dispatchEvent(new MyEvent(MyEvent.HUNGRY));

    Not that hard :)

    If you want to pass more variables with your event just add them to your event class and add them to the constructor, like this:
    package {
    import flash.events.Event;

    public class MyEvent extends Event {
    // Types of events that could be casted:
    public static const HUNGRY:String = "hungry";
    public static const HAPPY:String = "happy";
    public static const SAD:String = "sad";
    public var linesOfCodeWritten:int;

    public function MyEvent(linesOfCode:int, type:String, bubbles:Boolean = false, cancelable:Boolean = false){
    super(type, bubbles,cancelable);
    linesOfCodeWritten = linesOfCode;
    }
    }
    }

    And you read your variable with e.linesOfCodeWritten in your event handler.

  • Change WMODE with jQuery

    November 10, 2009 | Tags: Javascript, Flash, jQuery, Snippet
    Ever needed to change the WMODE of a Flash object at a runtime? This problem occured in a CMS I'm working with. All embedded Flash objects get WMODE Window by default, meaning they would block all other HTML-elements. It wasn't possible to change them on the server side, so only client side javascript remained as a possible solution. Changing the EMBED-tag in FireFox is really easy, one row needed. However Internet Explorer required some extra work. Apperantly EMBED-element was resistant against setting WMODE through attributes.

    <script type="text/javascript">

    // FireFox is easy to handle
    $("embed").attr("wmode", "opaque");

    // IE requires more work
    $(document).ready(function() {
    var embedTag;
    $("embed").each(function(i) {
    embedTag = $(this).attr("outerHTML");
    if ((embedTag != null) && (embedTag.length > 0)) {
    embedTag = embedTag.replace(/embed /gi, "embed wmode=\"opaque\" ");
    $(this).attr("outerHTML", embedTag);
    }
    });
    });

    </script>

    A short note about the IE specific code. I take no notice of existning WMODE-attributes. You might want to strip existing WMODES first before appending the new one to the EMBED-tag.

    EDIT:
    Okay, thing ain't always as easy as the seem to. A page in FireFox with lots of graphics would require another approach (only change is that the EMBED-tag is wrapped by a DIV inside the for-loop (originally for IE)):

    <script type="text/javascript">

    // FireFox
    $j("embed").attr("wmode", "opaque");

    $j(document).ready(function() {
    // IE
    var embedTag;
    $j("embed").each(function(i) {
    embedTag = $j(this).attr("outerHTML");
    if ((embedTag != null) && (embedTag.length > 0)) {
    embedTag = embedTag.replace(/embed /gi, "embed wmode=\"opaque\" ");
    $j(this).attr("outerHTML", embedTag);
    }
    // This "else" was added
    else {
    $j(this).wrap("<div></div>");
    }
    });
    });

    </script>