Stage.color dosen't work?


#1

npm openfl

I tried to change stage color , but it seems like not work.

class App extends Sprite
{
	constructor()
	{
		super();

		this.addEventListener(Event.ADDED_TO_STAGE, (e: Event) =>
		{
			this.stage.color = 0xff00ff;
		});
	}
}

What should I do?


#2
this.stage.color = 0xff00ff;
//Change to try like this
stage.color = 0xff00ff;

#3

Could you see if this works?

this.stage.color = 0xff00ff;
this.stage.invalidate ();

I think we need to mark the stage render as dirty when changing it’s color. If this works, I’ll fix it in the core. Thank you :wink:


#4

Sorry, it’s my misstake.
I did not get callback from ‘addedToStage’ event.
In the code below, it works well~

class App extends Sprite
{
	constructor()
	{
		super();

        if (this.stage)
		{
			this.on_added(null);
		}
		else
		{
            // auto bind 'this' to event handle? good ~~
			this.addEventListener(Event.ADDED_TO_STAGE, this.on_added);
		}
	}

    private on_added(e: Event)
	{
		this.stage.color = 0xff00ff;
        // result: #ff00ff, it works~
		console.log('on stage added, color:', '#' + this.stage.color.toString(16));
        // resylt: 0
		console.log('on stage added, fps:', this.stage.frameRate);
        // result: false
		console.log('stage equal:', globalStage == this.stage);
	}
}

var globalStage = new Stage(550, 400, 0xFFFFF, App);
document.body.appendChild(globalStage.element);

But I have new questions :grinning:
1.Does frameRate of stage keep 0 ?
2.Does ‘goobalStage’ not equal to ‘this.stage’ ?


#5

这两种效果一样的,是我之前代码的问题,不过最开始 this.stage 获取的是null,我以为一定会有事件才对,结果后来又能获取到 this.stage 了,看来还是和原生的flash写法保持一致吧。

怎么,左眼兄弟觉得是haxe openfl好用还是ts openfl好用呢?:sunglasses:


#6

Okay, thanks. I’ll look into dispatching ADDED_TO_STAGE

I believe in Flash, your document class is already added to the Stage when it is created, but you do also get an extra ADDED_TO_STAGE event, in case you were listening for it. We might not be dispatching this extra event.

Another approach would be to create the stage, then stage.addChild:

var stage = new Stage (550, 400);
stage.addChild (new App ());

…this would dispatch ADDED_TO_STAGE and stage would start null.

I believe our frameRate defaults to requestAnimationFrame. Is that the frequency you are getting, or is it slower?

Your globalStage reference should be the same as app.stage, since that’s where it’s added :slight_smile:

EDIT: I’ve updated the dev builds, so now ADDED_TO_STAGE will be dispatched for the initial document class as well :slight_smile: Thanks!


#7

目前用的是Haxe-openfl,主要是因为能支持C++,这是不错的选择。


#8

Got it, thank you very much~

Indeed, globalStage equal to app.stage.

setTimeout(() =>
{
	// result: true
	console.log('stage equal:', globalStage == this.stage);
}, 100);

:+1: