Frage

I don't think my syntax is actually bad here. Or is it? This is my first stab at OpenFL.

The Haxe is not compiling correctly? Am I missing a compiler directive? Do I actually have a syntax error in this function? Syntax checker in Flashdevelop says no.

Here's the command:

Running process: C:\Program Files (x86)\FlashDevelop\Tools\fdbuild\fdbuild.exe "C:\dev\Haxe\TestOpenFL\OpenFLTest.hxproj" -ipc 2e4ace78-45b9-4868-a2dd-cf2c35265f44 -version "3.0.0" -compiler "C:\HaxeToolkit\haxe" -library "C:\Program Files (x86)\FlashDevelop\Library" -target "flash"

src/Main.hx:32: characters 16-17 : Unexpected ; Build halted with errors (haxelib.exe).

function init() 
{
    if (inited) return;
    inited = true;

    //           \/ says this semicolon is unexpected. wtf 
    for (var i = 0; i < 200; i ++)
    {

        var bmd = new BitmapData( 100, 100, true, 0xff0000ff);
        var bmp = new Bitmap( bmd);

        addChild(bmp);

        bitmaps.push( bmp );            
    }

    addEventListener( Event.ENTER_FRAME, onEnterFrame );
}

Here's the whole script. I can't for the life of me figure out why it would error there. If I comment out just the loop, it compiles just fine.

class Main extends Sprite 
{
var inited:Bool;    
var bitmaps:Array<Bitmap>;

/* ENTRY POINT */   
function resize(e) 
{
    if (!inited) init();
    // else (resize or orientation change)
}

function init() 
{

    bitmaps = new Array();

    if (inited) return;
    inited = true;

    for (var i = 0; i < 200 ; i ++)
    {
        // Assets:
        var bmd = new BitmapData( 100, 100, true, 0xff0000ff);
        var bmp = new Bitmap( bmd);

        addChild(bmp);

        bitmaps.push( bmp );            
    }

    addEventListener( Event.ENTER_FRAME, onEnterFrame );
}

private function onEnterFrame(e:Event):Void 
{
}




/* SETUP */

public function new() 
{
    super();
    addEventListener(Event.ADDED_TO_STAGE, added);
}

function added(e) 
{
    removeEventListener(Event.ADDED_TO_STAGE, added);
    stage.addEventListener(Event.RESIZE, resize);
    #if ios
    haxe.Timer.delay(init, 100); // iOS 6
    #else
    init();
    #end
}

public static function main() 
{
    // static entry point
    Lib.current.stage.align = flash.display.StageAlign.TOP_LEFT;
    Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
    Lib.current.addChild(new Main());
}

}

War es hilfreich?

Lösung

First thing: have a look at the Haxe syntax page, it will help you for a lot of your futur problems.

Next, the for in Haxe is a bit tricky, it's kind of a foreach, so you use it like this:

for(myElem in elements){
    // loop here
}

Where elements implements Itarable (like an Array, a GenericStack or a Map). But if you want to increment a variable, you can create an Iterable by using the operator .... So, to take your code as an example:

for(i in 0...200){
    // loop here
}

Here, i will take as a value all the int between 0 and 200 (excluded).

Andere Tipps

Complete noob problem. Dove right in instead of reading the doc (which is the best way to learn, IMHO).

You can't use loops in Haxe. You have to use iterators.

for (i in 0...200)
{
         //do stuff
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top