views:

296

answers:

2

Can i set a function when creating an object like i can with variables?

Given i have a container class and a CustomButton class:

function doSomething():Void{}

var button:CustomButton = CustomButton{
   posX : 50;
   posY = 100;
   onMouseClicked: doSomething;

}

Short story: i need the main container object to handle mouse events that occurs in objects placed in the containers.

+1  A: 

If I've understood your requirement correctly, I think it can be achieved with some syntax changes. Obviously you can extend Button if you need a custom version:

function doSomething():Void{
    println("clicked");
}

var button:Button = Button{
    text: "Click Me"
    translateX: 50;
    translateY: 100;
    action: doSomething
}
Stage {
    title : "ButtonTest"
    scene: Scene {
        width: 200
        height: 200
        content: [ button ]
    }
}
Matthew Hegarty
+1  A: 

The easiest change for you is to change:

function doSomething():Void{}

to

function doSomething(e:MouseEvent):Void{}

The action property is nice but I'm sure you want some custom rollover effect or something using onMouseEntered etc.

Eric Wendelin
A combination of these things worked out. I did try out sending the MouseEvent as a parameter, but that didn't work and i couldn't find any documentation on the syntax of passing functions. var towerButton: GenericButton = GenericButton{ onMouseClicked: doSomething }This will correctly call doSomething(e:MouseEvent).
Vargen
Oh you want to pass a function to another? function doSomething(function(MouseEvent):otherfunc):Void { ... }
Eric Wendelin