Haxe and AS3

112 views
Skip to first unread message

jordan tomlinson

unread,
Apr 29, 2014, 9:36:24 PM4/29/14
to haxe...@googlegroups.com
Hello, im familiar with AS3 but coming to haxe has raised a few confusions and such. Im used to having Adobe Flash's graphical editor so i think this is whats causing me confusion. 

1. Lib.current.addEventListener(MouseEvent.CLICK, onClick); 

private function onClick(_)
{
    trace("Click!");
}

Doing this does nothing, however, if i change (Lib.current) to (Lib.current.stage), it works. This is driving me nuts can anyone exaplin please?

2. When drawing Sprites, Movieclips etc do i draw them to Lib.current or Lib.current.stage?

Any help would be appreciated, thanks!


Joshua Granick

unread,
Apr 30, 2014, 3:51:41 AM4/30/14
to haxe...@googlegroups.com
Are you using Haxe, or something like OpenFL?

OpenFL might give the closest experience to what you expect from AS3, with a "document class" that has "stage" predefined, you can add a listener to this class as normal, and move on.

Using Haxe directly, "Lib.current" is the "actual" document class. This is the entry point for the application, it's an empty MovieClip. You can add things to it (which is generally recommended) or you can add things to the stage instead. If you add directly to the stage, listening to Lib.current will never mouse events, because is inside the empty clip.

I hope this helps! Please feel free to post any more questions, or let me know if this is still not clear.

Have a great day!


PS, here are a couple code samples that may help:


In OpenFL...


package;

import flash.display.Sprite;
import flash.events.MouseEvent;

class Main extends Sprite {
    
    public function new () {
        
        super ();
        
        graphics.beginFill (0xFF0000);
        graphics.drawRect (0, 0, 100, 100);
        
        stage.addEventListener (MouseEvent.CLICK, onClick);
        
    }
    
    private function onClick (event:MouseEvent):Void {
        
        trace ("Click");

    }
    
}



In traditional Haxe/Flash...



package;

import flash.display.Sprite;
import flash.events.MouseEvent;

class Main extends Sprite {
    
    public function new () {
        
        super ();
        
        graphics.beginFill (0xFF0000);
        graphics.drawRect (0, 0, 100, 100);
        
        Lib.current.stage.addEventListener (MouseEvent.CLICK, onClick);
        
    }
    
    private function onClick (event:MouseEvent):Void {
        
        trace ("Click");

    }
    
    private static function main ():Void {
        
        Lib.current.addChild (new Main ();
        
    }
    
}



...and in AS3 (if I'm not too rusty)



package {

    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Main extends Sprite {
    
        public function Main () {
        
            super ();
        
            graphics.beginFill (0xFF0000);
            graphics.drawRect (0, 0, 100, 100);
            
            stage.addEventListener (MouseEvent.CLICK, onClick);
            
        }
        
        private function onClick (event:MouseEvent):void {
            
            trace ("Click");
    
        }
        
    }
    
}
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
Using Opera's mail client: http://www.opera.com/mail/

Nicolas Cannasse

unread,
Apr 30, 2014, 4:04:34 AM4/30/14
to haxe...@googlegroups.com
Le 30/04/2014 03:36, jordan tomlinson a écrit :
> Hello, im familiar with AS3 but coming to haxe has raised a few
> confusions and such. Im used to having Adobe Flash's graphical editor so
> i think this is whats causing me confusion.
>
> 1. Lib.current.addEventListener(MouseEvent.CLICK, onClick);
>
> private function onClick(_)
> {
> trace("Click!");
> }
>
> Doing this does nothing, however, if i change (Lib.current) to
> (Lib.current.stage), it works. This is driving me nuts can anyone
> exaplin please?

This is perfectly normal and will work the same in AS3. If your
"current" does not have any content then the clickable zone is empty.

Best,
Nicolas

jordan tomlinson

unread,
Apr 30, 2014, 12:01:42 PM4/30/14
to haxe...@googlegroups.com
Okay thanks for the replies! I think i get it now.

Lib.current = Flash Document class and this is a blank MovieClip, also this is what i should be drawing to?

Lib.current.stage <- im still confused at this then, if we have the above^ then what is the stage? If im drawing to the Lib.current what is the need of stage? 

Sorry if i sound dumb Haxe has turned a few things in AS3 on its head for me

Joshua Granick

unread,
Apr 30, 2014, 2:51:12 PM4/30/14
to haxe...@googlegroups.com
In Flash, there's always a stage, so it really goes like this:


Stage
|--> Lib.current (MovieClip)


So if you add to Lib.current, you can listen to clicks from Lib.current

Stage
|--> Lib.current (MovieClip)
|--> Main (usually a Sprite)


Otherwise you'll want to listen either to your Main class (or whatever you call it) or the stage, since you'll be bypassing Lib.current (its a sibling, not a parent)

Stage
|--> Lib.current
|--> Main


Anything that's already on the stage has a "stage" reference. If its off the display list its null, otherwise its defined. Since Lib.current is the Flash document class (when targeting Flash, this isn't the case for other OpenFL targets) it is already on the stage at startup, thus why Lib.current.stage is defined.

Hope this helps!

jordan tomlinson

unread,
Apr 30, 2014, 2:57:09 PM4/30/14
to haxe...@googlegroups.com
Ahhh i get it now! Thankyou so much
Reply all
Reply to author
Forward
0 new messages