[SOLVED] Enter_frame listener in other class than main

Hi guys

I have problem with ENTER_FRAME EventListener.
If i use addEventListener(Event.ENTER_FRAME, Update) in main.hx everything
works fine, but if i want use this event listener in other file (and other class of course) this dont work.
I dont know where is problem because I’m newbie in Haxe and openfl.
I search in internet solution of this problem but i found nothing.

Cheers!

package;
import openfl.events.Event;
import openfl.Lib;
import openfl.Assets;
import flash.display.Bitmap;
import openfl.system.Capabilities;
import openfl.display.Sprite;
import openfl.events.MouseEvent;

class B_back extends Sprite
{
    private var screenDensity:Float;
    private    var back_button_pressed:Bool;
    var baseImagePath = "assets/mdpi";
    var bitmapData = Assets.getBitmapData ("assets/mdpi/ic_sysbar_back_dark_normal.png");


    
    
    
    private    function init(){
        back_button_pressed = false;    
                                            
        var bitmap = new Bitmap (bitmapData);
        addChild (bitmap);
        bitmap.width = ((Lib.application.window.width * 50) / 480);
        bitmap.height = ((Lib.application.window.width * 50) / 480) / ((50 / 50));
        bitmap.x = Lib.application.window.width-((Lib.application.window.width * 55) / 480);
        bitmap.y = Lib.application.window.height - ((Lib.application.window.width * 58) / 480) / ((58 / 58));
        
        this.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
        this.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
        this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
        this.addEventListener(MouseEvent.CLICK, mouseClick);
        addEventListener(Event.ENTER_FRAME, Update);
    }
    


        private function Update(e:Event) {
            
            var dpi = Capabilities.screenDPI;
        
            if (dpi < 200) {
                screenDensity = 1;
                baseImagePath = "assets/mdpi";
            } else if (dpi < 300) {
                screenDensity = 1.5;
                baseImagePath = "assets/mdpi";
            } else {
                screenDensity = 2;
                baseImagePath = "assets/hdpi";
            }
            

            if (back_button_pressed==false){
                bitmapData = Assets.getBitmapData (baseImagePath + "/ic_sysbar_back_dark_normal.png");
            }else{        
                bitmapData = Assets.getBitmapData (baseImagePath+ "/ic_sysbar_back_dark_pressed.png");
            }
        }
    
    
    
    
    
        private     function mouseClick(event: MouseEvent):Void {
            trace(back_button_pressed);
            back_button_pressed = true;
        }
        
        
        
        private function mouseDown(event: MouseEvent):Void{
            //    trace("mouse mouseDown");
    
        }
        
        private function mouseOver(event: MouseEvent):Void{
            //back_button_pressed = true;
    
            //trace("mouse over");
    
        }
        
        private function mouseOut(event: MouseEvent):Void {
            back_button_pressed = false;
            trace(back_button_pressed);
            }
    
            
        public function new() 
        {
            super();
            init();
        }
        
        
    }

Do you create an instance of B_back and add it to the stage (call addChild or addChildAt with the instance as a parameter)?

In Flash, ENTER_FRAME works on any DisplayObject, but if you are using OpenFL, you need to be added to the stage, since not all targets support weak event listeners

I dont understand where I must call addChild, I try in many places in code
but no of them work (or work partly). I checked a couple of codes for example and
I do not noticed this problem, e.g.
https://github.com/yupswing/openfl-preloader/blob/master/src/Preloader.hx .

Thanks for reply guys.

Your project might like this:

package;


import openfl.display.Sprite;


class Main extends Sprite {
    
    
    public function new () {
        
        super ();
        
        addChild (new B_back ());
        
    }
    
    
}
1 Like

Thanks guys, everything works fine!