[Question] How to draw shapes on the screen?

SadiQ

  • Posts: 1788
I'm messing with creating an extension, but I would need to be able to draw some basic shapes on the screen (squares, lines) for debugging purposes. Any tips on what I should do? I don't want to use images or anything fancy, and it will only need to work in flash. I will also not need to create blocks for that(in case it makes things easier to explain).
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

captaincomic

  • *
  • Posts: 6108
You could use the same code as Stencyl's drawing blocks  (look at their code preview). They use the class http://static.stencyl.com/api/31/types/com/stencyl/graphics/G.html.

Or you could use the graphics class directly http://static.stencyl.com/api/31/types/nme/display/Graphics.html.

SadiQ

  • Posts: 1788
I tried to copy the code from the code preview but the results weren't the ones I expected.
This is what I have so far:
Code: [Select]
import com.stencyl.graphics.G;
import com.stencyl.utils.Utils;



class DrawingTest
{
public static var g:com.stencyl.graphics.G;

public static function draw()
{
g.fillColor = Utils.getColorRGB(255,200,0);
g.fillRect(20, 20, 20, 20);
//com.stencyl.Engine.engine.root.addChild(g); //gives the following error: com.stencyl.graphics.G should be flash.display.DisplayObject

}

}
Yet I get the following error:
Code: [Select]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Pathfinder$/draw()[C:\Users\Luci\AppData\Roaming\Stencyl\stencylworks\engine-extensions/drawingtest/DrawingTest.hx:12]
at MethodInfo-3780()[Source/scripts/Design_1_1_s.hx:88]
at com.stencyl::Engine$/invokeListeners2()[D:\Program Files (x86)\Stencyl\plaf\haxe\lib/stencyl/1,00/com/stencyl/Engine.hx:4020]
at com.stencyl::Engine/update()[D:\Program Files (x86)\Stencyl\plaf\haxe\lib/stencyl/1,00/com/stencyl/Engine.hx:2441]
at com.stencyl::Engine/postUpdate()[D:\Program Files (x86)\Stencyl\plaf\haxe\lib/stencyl/1,00/com/stencyl/Engine.hx:2732]
at com.stencyl::Engine/onUpdate()[D:\Program Files (x86)\Stencyl\plaf\haxe\lib/stencyl/1,00/com/stencyl/Engine.hx:2725]

Something tells me my code needs to be more complex?

EDIT: pasted the imports and the proper error message I get.

« Last Edit: July 10, 2014, 12:49:00 pm by SadiQ »
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

captaincomic

  • *
  • Posts: 6108
You need to get a reference to "g", which is an instance of the G class. Do you want to call the code from a behavior? In that case you could pass the g object from the behavior to your method.
 
Code: [Select]
public static function draw(g:G)
{
g.fillColor = Utils.getColorRGB(255,200,0);
g.fillRect(20, 20, 20, 20);
//com.stencyl.Engine.engine.root.addChild(g); //gives the following error: com.stencyl.graphics.G should be flash.display.DisplayObject

}
and then called from a behavior with "draw(g);"

SadiQ

  • Posts: 1788
And
You need to get a reference to "g", which is an instance of the G class. Do you want to call the code from a behavior?

I wish I knew how I would get that reference :(
I made it work with the code you posted if I call the function from inside the Drawing event (since I can send "g" to that static function I created), but is there another way? (I'd like to learn more if possible).
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

rob1221

  • *
  • Posts: 9459
Set g to a public variable inside the drawing event and then you should be able to refer to g from anywhere using that public variable.