forum > Squares

  • Hello World!

    I am currently trying to make a square with haxe, but I am encountering a problem. I made a file called Square.hx, and here is the code I put inside it:

    import flash.geom.Rectangle;
    
    class Square {
    
        function new() {
            flash.geom.Rectangle.new(0, 0, 50, 50);
        }
    
        static function main() {
    
        }
    }

    But when I compile it, it give me an error suggesting that the "new" part of the code is wrong. I don't get what I did wrong here if this is what it said in the API. If anyone knows what I'm doing wrong, please reply bellow.

    Thanks!

  • flash.geom.Rectangle is a mathematical concept. You cannot draw a Rectangle just like you cannot draw a square root.
    You can read about it in the docs:
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Rectangle.html

    ''Note: The Rectangle class does not define a rectangular Shape display object. To draw a rectangular Shape object onscreen, use the drawRect() method of the Graphics class.''

    If you want a visible rectangular or square shaped object on your screen, you need to draw it onto the graphics property of a display object. For example:

    import flash.display.Sprite;
    
    class Main extends Sprite
    {
        public function new()
        {
            super();
            
            var square = new Sprite();
            square.graphics.lineStyle(2, 0xFF0000);
            square.graphics.beginFill(0x0000FF, 1);
            square.graphics.drawRect(0, 0, 50, 50);
            square.x = 100;
            square.y = 100;
            this.addChild(square);
        }
        
        public static function main()
        {
            flash.Lib.current.addChild(new Main());
        }
    }

    The script above will create a 50x50 blue square with a red border and puts it at position (100,100).

    Or if you want to create a separate class for your square:

    import flash.display.Sprite;
    
    class Square extends Sprite{
    
        public function new() 
        {
            super();
            this.graphics.beginFill(0x0000FF, 1);
            this.graphics.drawRect(0, 0, 50, 50);
        }
    }
    
    class Main extends Sprite
    {
        public function new()
        {
            super();
            var square = new Square();
            addChild(square);
        }
        static function main() 
        {
            flash.Lib.current.addChild(new Main());
        }
    }

    You can find all drawing commands here:
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html#methodSummary

    Jan

  • I don't know, when I try compiling what you tell me it gives me an error saying that it cannot find "flash.display.Sprite". Here's the code:

    import flash.display.Sprite;
    
    class Square extends Sprite {
    
        private function new() {
            super();
            var r = new Sprite();
    
            r.graphics.lineStyle(2, 0xFF0000);
            r.graphics.beginFill(0x0000FF, 1);
            r.graphics.drawRect(0, 0, 50, 50);
            r.x = 100;
            r.y = 100;
            this.addChild(square);
        }
    
        private static function main() {
            flash.Lib.current.addChild(new Square());
        }
    }

    and here's what I put into the command line:
    $ haxe -main Square -swf Square.swf
    ./Square.hx:1: characters 0-28 : Class not found : flash.display.Sprite

    Is there something I did wrong here?

  • Hi Nicolas,

    I just tried the code and for me it worked, but I had to change the filename to Main.hx and use

    haxe -main Main -swf Square.swf
    Another thing I might suggest if this still doesn't work is perhaps you could install Haxe NME from www.haxenme.org and try that as they provide scripts for setting up libraries for various platforms. It's handy if like me you're using Linux.

    Good luck!

    Julian

  • ./Square.hx:1: characters 0-28 : Class not found : flash.display.Sprite

    It seems your installation is not correctly setup. The compiler cannot find the classes in the std folder
    Read about it here:
    http://haxe.org/doc/build/haxe_ubuntu_build#environment-variables

    Jan

  • OK, I reinstalled haxe with using the website download instead of the Debian repositories, but I still have problems with the cpp libraries, even though I installed them and I see it installed on my computer. When I try to compile the "Hello World" program, it give me the following error:

    $ haxe -main Hello -cpp Hello.cpp
    haxelib run hxcpp Build.xml haxe -Dcpp -Dhaxe_209 -Dsys -Dtrue
    Library hxcpp is not installed
    Error : Build failed

    On another note, when I reinstalled the haxe libraries, I was able to compile the file Square.hx with no problems except I had to change the code a little:
    import flash.display.Sprite;
    
    class Square extends Sprite {
    
        private function new() {
            super();
            var r = new Sprite();
    
            r.graphics.lineStyle(2, 0xFF0000);
            r.graphics.beginFill(0x0000FF, 1);
            r.graphics.drawRect(0, 0, 50, 50);
            r.x = 100;
            r.y = 100;
            this.addChild(this);
        }
    
        private static function main() {
            flash.Lib.current.addChild(new Square());
        }
    }

    Now, it compiles, but when I run the program, it doesn't show the square. I have no idea what the problem is, anyone have any suggestions?

    Thanks!

  • OK, forget about the C libraries, I figured that out. But I am still having problems with the flash.

  • The way to compile the example:

    1) Save the code in a file named "Square.hx"
    2) Create another file named "compile.hxml" and put this inside

    -swf output.swf
    -main Square
    -swf-version 10
    -swf-header 200:200:30:ffffff

    3) In the command shell (Bash, I suppose):
    haxe compile.hxml

    Explanation:
    "compile.hxml" is a file that contains all the flags that you have to pass to the compiler. So, instead of doing the compilation in this way: haxe -swf outfile.swf -main Square... You can do it this way: haxe compile.hxml.
    The first line tells the compiler that the output file will be a SWF (Flash) file called "output.swf"
    The second line tells the compiler that the main entry function (The "static function main ()" in the main file) is located in the Square class, so it searches for a "Square.hx" file in the actual directory.
    Next line, "-swf-version", tells the compiler that our target Flash Player version is 10 or up.
    Finally, "-swf-header" tells the compiler that the output file should have 200 pixels wide, 200 pixels tall, 30 frames per second and a white background.

    If the example don't works using this, as Jan said you don't have a correct setup, so reinstall all.

    You are looking for something graphical, not the barebone Haxe compiler, so check NME that will fits your needs, as Julian said. NME is a Haxe library that has the "magic" for making a program meant to be compiled to Flash be compiled, almost flawlessly, to Linux, Mac, Windows, iPhone, Android, WebOS, HTML5, whatever... Try it!

    And if you want more help from this point, don't hesitate to contact me by this way so I can answer your questions in spanish ;) (translating to english too, I know the rules of language in the forum)

  • no problems except I had to change the code a little:

     this.addChild(this);

    I have no idea what the problem is, anyone have any suggestions?

    The problem is the line you changed. It should be

    this.addChild(r);

    In your old example you did:

    var r = new Sprite();
    this.addChild(square);
    but square does not exist.

    Tip: When you are given a working example, don't break it by changing things. First try it like it is presented to you. ;)

    Jan

  • OK, thanks Jan, as for your comment Maximiliano, I compiled it as you said, but it game me the same result it did before when I compiled the program, no square. I think I have a problem with my haxe flash compiler.

  • Did you try the basic haxe flash example? There are some more at haxe.org/byexample, start with code others have made then when you know it's working you can start getting creative.

  • Yes, Justin, I tried compiling the very basic "Hello World" program and it didn't, but it worked when I compiled it as a cpp or as neko. I seem to have a problem with the flash compiler.

< Prev | Page 1 / 1 | Next >