Blog posts

  • When MouseEnabled fails

    June 16, 2010 | Tags: Actionscript, AS3, Troubleshooting, Flash
    Some problems in Flash can be a real pain sometimes. For instance in the following case.

    Having a sprite that includes another sprite, but only the parent sprite should be able to recieve mouse events. The child sprite would only be used to be painted on in the drawLine function which is called on a regular basis. A simplified version would look something like this:
    public class Tag extends Sprite{
    private var _canvas:Sprite;

    public function Tag() {
    _canvas = new Sprite();
    _canvas.mouseEnabled = false;

    addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);

    private function onMouseOver(e:MouseEvent):void {
    // Do stuff

    private function drawLine():void{
    var targetX:Number = 120;
    var targetY:Number = 10;;, 0x888888, 0.75);, 0);, targetY);
    Looks pretty straight forward? _canvas.mouseEnabled is set to false, and that should remove the MouseEvent.MOUSE_OVER from being triggered when the mouse is over the child, right? WRONG!

    Eventhough _canvas.mouseEnabled is disabled it will fire the event as the graphics of _canvas still resides in the parent sprite. The way to solve this is to check which object triggered the event by changing the onMouseOver function to:
      private function onMouseOver(e:MouseEvent):void {
    if ( != this)
    // Do stuff
    Is the target the main sprite? If not bail out of the event handler. Another approach would be to use the hitRect attribute of the main sprite.


    June 14, 2010 | Tags: Actionscript, AS3, Flash
    I thought that the functionality of ROLL_OUT was about the same as MOUSE_OUT. And in many aspects they are. But the big difference is how they handle children:

    MouseEvent.MOUSE_OUT will be raised if a child object recieves MOUSE_OVER.

    MouseEvent.ROLL_OUT will only be raised if the clip that the event is attached to and all it's children looses MOUSE_OVER.

    So, if you use MouseEvent.MOUSE_OUT the event will fire when focusing on a child object. Something that usually isn't what you want. If that's the case, change to MouseEvent.ROLL_OUT and you're all set to go! :)

  • Struct vs Class: By value or reference

    June 07, 2010 | Tags: C#
    public struct Car {
    public int Gears;
    public class Car {
    public int Gears;
    ..looks almost the same. But there's a huge different how they are handled. Structs are always copied as values when assigned to another variable in contrast to classes that copied as a reference to the original object.

    If you run the following code:
    Car car1 = new Car();
    car1.Gears = 4;
    Car car1copy = car1;
    car1copy.Gears = 5;
    ..and Car is a struct, then car1.Gears still would be 4 eventhough the copies Gears was changed. If, instead, Car is a class car1copy would be a reference to car1 thus resulting in car1.Gears also becoming 5.

  • Generating PDF from .Net

    May 26, 2010 | Tags: PDF, ASP.NET, C#
    There are several neat libraries for .Net to create PDF files on the fly. Some rely on you doing all the content by code, others like FO.NET uses XSD to accomplish this.

    Here's a hello world example for FO.NET, first the content:
    <?xml version="1.0" encoding="utf-8"?>
    <fo:root xmlns:fo="">

    <fo:simple-page-master master-name="simple"
    <fo:region-body margin-top="3cm"/>
    <fo:region-before extent="3cm"/>
    <fo:region-after extent="1.5cm"/>

    <fo:page-sequence master-reference="simple">
    <fo:flow flow-name="xsl-region-body">
    <fo:block font-size="18pt" color="black" text-align="center">
    Hello, World!


    And the C# code to generate the PDF from the XSD above:

    using System.IO;
    using Fonet;

    namespace FonetExample {
    class HelloWorld {
    static void Main(string[] args) {
    FonetDriver driver = FonetDriver.Make();
    driver.Render("", "hello.pdf");

    You'll find the open source FO.NET library and documentation how to use it over at Codeplex.

  • Get height of TextBlock in WPF

    May 21, 2010 | Tags: WPF, XAML, CodeProject, C#, Snippet
    Sometimes it's quite nice to get the height of a textblock at the same time it's created. In WPF however there's a couple of events that handle the layout functions when an element is rendered, and before these have been fired, you won't get proper values from the DesiredSize property. The good news is that these can be triggered by the Arrange function. You call the Arrange function with a Rect (rectangle) representing the column width and max height (example below use 1000px, but you might need more for longer texts) of your text.

    TextBlock t = new TextBlock();
    int columnWidth = 310;
    t.FontFamily = new FontFamily("Arial");
    t.FontSize = 20;
    t.Text = "Here goes the text..";
    t.Width = columnWidth;
    t.TextWrapping = TextWrapping.Wrap;
    t.Arrange(new Rect(0,0,columnWidth,1000));
    int textHeight = (int)t.DesiredSize.Height;