16
Ask a Question / [Solved] Why does this code work?
« on: June 06, 2018, 05:18:32 pm »
I am aware my question may sound odd, but it's 3 in the morning and I can't find an explanation online.
I made a class MyText in freeform mode and I made it extend a SceneScript.
I did it like that because I wanted it to act as a behavior too.
Then what I did, was give it a 'addWhenDrawingListener' to the constructor like this:
This actually drew me "Hello World" on the screen.
From my understanding, the whole code which is not from Behaviours for a scene (on update, on draw, when created, etc) are actually inside a 'default' behavior of the scene. Please correct me if I'm wrong here.
So, actually, what I did was I created a new behavior inside a behavior.
How does Stencyl know to use the same 'g : G' to draw the text?
I could create an on-draw block for my scene, the Stencyl automatically calls 'addBehavior(this on-draw block)' at initialization.
In that case it makes sense. But if I add a behavior "manually", how does it know to get the screen and draw on the same screen as the scene does?
Sorry if it sounds confusing.
Thanks
I made a class MyText in freeform mode and I made it extend a SceneScript.
I did it like that because I wanted it to act as a behavior too.
Then what I did, was give it a 'addWhenDrawingListener' to the constructor like this:
Code: [Select]
addWhenDrawingListener(null, function(g:G, x:Float, y:Float, list:Array<Dynamic>):Void {
g.drawString("Hello world");
});
Then I added a new MyText object in an arbitrary-code (and called new MyText()) inside a newly created scene.This actually drew me "Hello World" on the screen.
From my understanding, the whole code which is not from Behaviours for a scene (on update, on draw, when created, etc) are actually inside a 'default' behavior of the scene. Please correct me if I'm wrong here.
So, actually, what I did was I created a new behavior inside a behavior.
How does Stencyl know to use the same 'g : G' to draw the text?
I could create an on-draw block for my scene, the Stencyl automatically calls 'addBehavior(this on-draw block)' at initialization.
In that case it makes sense. But if I add a behavior "manually", how does it know to get the screen and draw on the same screen as the scene does?
Sorry if it sounds confusing.
Thanks