Stencyl 3.4.0 is now out. Get it now!

Drawing when control was pressed

nikopol

  • Posts: 26
Hi,

I am making a behaviour which basically opens an inventory (black rectangle for now).
When I try to draw something by using the 'draw' block, it always gives a red warning (see attachment), unless i put it under the 'when drawing' block.
But then if I use 'draw' under the 'when drawing', when I test the game, all my layers disappear.
I know this is not a huge problem but I couldn't even find any real explanation on how drawing works.

Thanks in advance if you have a link to somewhere, or the patience to explain yourself !

JeffreyDriver

  • Posts: 900
Drawing blocks need to be put under drawing events, they don't work elsewhere.

I'd approach it like this:

When Inventory is pressed
If showInv = true
set showInv to false
Otherwise If showInv = false
set showInv to true


When Drawing
If showInv = true
Draw rectangle...

nikopol

  • Posts: 26
I tried that. When I test the game, all the layers of the scene disappear and I get this error message in the log viewer :
Level:     ERROR
When:      2017-03-19 14:07:24:153
From:      stencyl.sw.app.filewatcher.ExternalAppLauncher

[Flash] Universal#uncaughtErrorHandler(59): Error #1034: Type Coercion failed: cannot convert com.stencyl.models.scene.layers::BackgroundLayer@10f2e7eb9 to com.stencyl.models.scene.Layer.

Ps: no rectangle drawn...

VoidShard

  • Posts: 80
Drawing is done per frame, so you really have to put it into a when drawing section. The Stencyl compiler won't let you put it anywhere else, even if that event is only called when it's drawing.

To make it work, you need another boolean (like JeffreyDriver suggested) and in your When Drawing section:
if <showInv> and <InvOpen>
    Draw rectangle.


Also,
I'd just use a [set <showinv> to not(<showinv>)]
that will switch it between true and false.

I think you might also want to set some styles for your box, because the engine doesn't know what stroke width, color, or anything else that you want.

EDIT: Was a bit late, your error was caused because you didn't set the styles.

nikopol

  • Posts: 26
I still have all my layers disappearing, and the log viewer showing me hundreds of time :

Level:     ERROR
When:      2017-03-19 15:09:17:758
From:      stencyl.sw.app.filewatcher.ExternalAppLauncher

[Flash] Universal#uncaughtErrorHandler(59): Error #1034: Type Coercion failed: cannot convert com.stencyl.models.scene.layers::BackgroundLayer@1428a1d71 to com.stencyl.models.scene.Layer.


nikopol

  • Posts: 26
PS: the 'inventory is pressed' block is not activated.

CmdrWhitey13

  • Posts: 444
Could switch it to trigger upon release.

nikopol

  • Posts: 26
The code I'm writing is in an Actor behaviour, could that be the problem ?

greatanthony

  • Posts: 111


....give a try on placing your blocks in the "SCENE" in which you need.....