Openfl.display.Bitmap alpha property crashes

New to OpenFL and Haxe.

I created a new empty OpenFL project with FlashDevelop, so I’m using pretty much the basic template.
Using a class called “Scene” that extends openfl.display.Sprite, and adds an event listener to call a function update() for every ENTER_FRAME.

Whenever I try to set or read a bitmap’s alpha property from within that update(), program crashes (cpp) or freezes (flash).

Yet, if I set it outside of that function it works. However, I need to do it from within update() because I’m working on a “fade” effect.

I’m completely new to these two technologies so I must be missing something, however I haven’t found any specific case like this through Google. Any help appreciated.

hello there, I am also new to openfl and haxe, but I’ll try to help.
My guess would be your instance of bitmap is null when you call the function update.

but first, I’ll help you to have the good habits.

what does the output window of flash Develop says when it crashes?
I’ll take from one of my project what Flash develop says when a variable is null at some point.

if it says something like:

Invalid field access : set_alpha
Called from Cases.hx line 45
Called from Cases.hx line 21

this three lines gives an idea of what cause the error, and on which lines it was called.

If you have something like that then you have to make sure that your variable is given a value before setting or reading its properties.
hope I could help.

Well I’ll be damned:

First, the build doesn’t throw anything like you say, it actually compiles and launches just fine. It’s at the main window when it freezes or crashes.

Second, I have quite a mess of code already as you might imagine, as in classes in their respective files, a project package structure, etc. so posting here my code would be quite messy.

Third, so I go and start an empty project with -just- the piece I’m having problem with in order to post it here, and guess what, NOW it works as well as it should (doesn’t hang).

import flash.display.Sprite;
import flash.Lib;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.Assets;

enum FadeStates

class Main extends Sprite
var inited:Bool;

var fadeStep:FadeStates;
var fadeRate:Float;
var myBitmap:Bitmap;


function resize(e) 
    if (!inited) init();
    // else (resize or orientation change)

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

    // (your code here)
    // Stage:
    // stage.stageWidth x stage.stageHeight @ stage.dpiScale
    // Assets:
    // nme.Assets.getBitmapData("img/assetname.jpg");
    addEventListener(Event.ENTER_FRAME, update);
    var bitData = Assets.getBitmapData("img/hello.PNG");
    myBitmap = new Bitmap(bitData);
    myBitmap.x = (stage.stageWidth - myBitmap.width) / 2;
    myBitmap.y = (stage.stageHeight - myBitmap.height) / 2;
    myBitmap.alpha = 0.0;
    fadeStep = FADE_IN;
    fadeRate = 0.025;

private function carryOn():Void
    fadeStep = FADE_OUT;

private function update(eventReceived:Event):Void
        case FADE_IN:
            (myBitmap.alpha >= 0.999) ? fadeStep = WAIT : myBitmap.alpha += fadeRate;
        case WAIT:
            haxe.Timer.delay(carryOn, 4000);
        case FADE_OUT:
            (myBitmap.alpha <= 0) ? fadeStep = END : myBitmap.alpha -= fadeRate;
        case END:


/* SETUP */

public function new() 
    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

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());


My project code isn’t really different, the only thing I did not replicate is setting up the stage background color and a couple of things that extend the Sprite class to have some methods of its own in order to unload/load itself to facilitate me a “scenario/scenes” workflow.

What could interfere with accessing Bitmap.alpha? I’ve set up a dozen debug traces around and it hangs riiiiight before I do bitmap.alpha += newAlphaFloat, as if it didn’t even get past that line.

Got it now. This is what I had wrong (in analogy):

See the class-wide var myBitmap? and how within init() I say again myBitmap = new Bitmap(bitData);?

Well, in my other non-working code, I repeated var before myBitmap within init(), which I guess made a total mess and changed its scope. Had to re-read everything N times to catch it myself because neither the build output nor FlashDevelop gave me any warning.

ok happy you could sort it out.

But it is weird that flash develop output tag didn’t tell you there was something wrong at some line…
Usually, I debug for neko, because for it is fast for native(well not as fast as flash) and it tells me where I did wrong.

well we can also work on your non-working code and see what is wrong with it.

What I described is what was wrong with my actual project. I described it in analogy to the snippet. Actually you can reproduce it too using the snippet above.:

myBitmap = new Bitmap(bitData);

var myBitmap = new Bitmap(bitData);

Even though it is a re-definition it compiles just fine.

Yep, you can have a local variable (or function parameter) with the same name as an instance or static variable. This is true of many if not most C-like languages.

Here’s how you can disambiguate.