Question

I'm new to haxe/openfl and am trying to simply put a button on a screen. The code below gives me a white screen, no button or reaction to a screen press via trace. Can someone tell me what I'm doing wrong?

package;

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.Lib;
import openfl.Assets;
import flash.display.Sprite;


class Main extends Sprite {

public function new () {

 super ();
var sprite = new Sprite();
var bitmapData = Assets.getBitmapData ("images/button.png");
var bitmap = new Bitmap ( bitmapData );
sprite.addChild( bitmap );

// you may need to draw the hitarea

sprite.graphics.beginFill(0xff,0);    //transparent
sprite.graphics.lineStyle( 0,0xff, 0); //transparent

// may need to wait for image to load before using width, height - add
// check if required?

sprite.graphics.drawRect( 0, 0, bitmap.width, bitmap.height );
sprite.graphics.endFill();

   sprite.addEventListener( MouseEvent.CLICK, function(e: MouseEvent){
   trace('clicked');} );        

}   
}
Was it helpful?

Solution

The button is not visible because you didn't add the sprite containing the button to the stage.

You should add it with something like Lib.stage.addChild(sprite)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top