Drawing to the screen - and making it stick

Epic428

  • Posts: 1118
Ah, OK you meant the drawing shapes. Yeah, then that really wouldn't help him. Worth a shot. :p
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Alexin

  • *
  • Posts: 3127
Lines are drawn into a Shape, which in turn is drawn into the screen. The difference is that the Shape "remembers" what was drawn into it, so you don't have to redraw everything, every frame.

The code maintains a list of points, which isn't really necessary unless the Shape is cleared. In this case, you'd have to redraw everything.

Code: [Select]
package scripts
{
import flash.events.*;
import flash.net.*;
import flash.filters.*;
import flash.display.BitmapData;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.geom.Point;

import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;

import stencyl.api.data.*;
import stencyl.api.engine.*;
import stencyl.api.engine.actor.*;
import stencyl.api.engine.behavior.*;
import stencyl.api.engine.bg.*;
import stencyl.api.engine.font.*;
import stencyl.api.engine.scene.*;
import stencyl.api.engine.sound.*;
import stencyl.api.engine.tile.*;
import stencyl.api.engine.utils.*;
import org.flixel.*;

public dynamic class Curves extends SceneScript
{
private var s:Shape;
private var points:Array;

override public function init():void
{
s = new Shape();
points = new Array();

s.graphics.lineStyle
(
1.0,
0x000000,
1.0, true,
LineScaleMode.NORMAL,
CapsStyle.ROUND,
JointStyle.ROUND
);

doesCustomDrawing();
}

override public function update():void
{
if(isMouseReleased())
{
points.push(new Point(getMouseReleasedX(), getMouseReleasedY()));

/*s.graphics.clear();
s.graphics.lineStyle
(
1.0,
0x000000,
1.0, true,
LineScaleMode.NORMAL,
CapsStyle.ROUND,
JointStyle.ROUND
);
s.graphics.moveTo(points[0].x, points[0].y);

for(var i:uint = 1; i < points.length; i++)
{
s.graphics.lineTo(points[i].x, points[i].y);
//s.graphics.curveTo(points[i].x, points[i].y, points[i - 1].x, points[i - 1].y);
}*/

if(points.length >= 2)
{
s.graphics.moveTo(points[points.length - 2].x, points[points.length - 2].y);
s.graphics.lineTo(points[points.length - 1].x, points[points.length - 1].y);
}
}
}

override public function draw(g:Graphics, x:Number, y:Number):void
{
if(points.length <= 0) return;

FlxG.buffer.draw(s);
}

override public function handleCollision(event:Collision):void
{
}

public function Curves(ignore:*, scene:GameState)
{
super(scene);
}
}
}
"Find the fun"
alexin@stencyl.com

sharpleaf

  • Posts: 90
Hey Alexin,

I made a new behavior in code mode and cop/pasted your code in. I then attached the behavior to a scene, saved and tried to run the scene but nothing happens.
I shows the building dialog, but the flash play never appears.
??

Alexin

  • *
  • Posts: 3127
When creating the Code Mode behavior, make sure both names are "Curves". Then, replace the entire content with the code above. Test and click around in the screen.
"Find the fun"
alexin@stencyl.com

sharpleaf

  • Posts: 90
I made the behaviors name and class name both Curves and it still did the same thing. I would press test scene (or test game) and it would do the Prepping/Creating SWF loading box and then nothing happens. No flash player or anything.
Any ideas? Can you maybe pst your example on Stencyl Forge so that I can look at it?

Epic428

  • Posts: 1118
I uploaded the behavior to forge called Curves - it is a scene behavior, it is possible that the behavior you made was done as an actor behavior, which I believe extends a different class altogether.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

sharpleaf

  • Posts: 90
I made sure it was a scene behavior, so I don't know what was up.

I just downloaded the behavior you uploaded and attached it to my scene. When I hit test I get the following error:

Quote
Behavior: Curves_4 at line 100
A super statement can be used only inside class instance constructors.
         super(scene);

Epic428

  • Posts: 1118
Interesting, I didn't notice any errors, and I copy and pasted exactly what Alexin Posted. (i just tested it now and I still don't see any errors)

In fact I was capable of drawing to the screen perfectly fine. It seemed to only recognize my mouse down and mouse up states so it only drew straight lines, but the lines remained on the screen.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Alexin

  • *
  • Posts: 3127
It seemed to only recognize my mouse down and mouse up states so it only drew straight lines, but the lines remained on the screen.
Actually, the code the draw curves is there but it won't work as you might expect. That's why it's commented out.

This was just a quick performance test.
"Find the fun"
alexin@stencyl.com

sharpleaf

  • Posts: 90
I think I needed to download an an update for it to work because it didn't work on one computer but today it does work on a different computer (on which I've installed an update).

Is there a way to get it do draw continuously? like, you drag your mouse around with the left-mouse button clicked and it drops small lines?

Alexin

  • *
  • Posts: 3127
Instead of watching for mouse presses or releases, you can simply run the code every frame, while the mouse is down and its location has changed.
"Find the fun"
alexin@stencyl.com

Luxon5

  • Posts: 12
I'm new to stencyl, but I know in game maker this would usually be done with surfaces. Basically, a surface is an image you hold in memory, and reference it using a variable name. Then, you draw to it just like drawing to the screen, except you use the "draw to" function to make the game draw to your surface when using the drawing commands, instead of to the screen (which is the default).

Then, you just draw the surface to the screen every step. Your surface is not erased and refreshed every step like the screen is, so the result is that everything you draw "stays" on the screen.

The advantage is there's no making long lists of points or coding needed.. you just draw the same way you always do, just to the surface instead of the screen. This is really useful for drawing games, but other games too (like for pausing the game, screenshots for save games, etc.)

I don't know if this is possible in stencyl yet or not, but I would sure like to find out.

Jon

  • *
  • Posts: 17524
Flash, Flixel (and in turn Stencyl) can make use of BitmapData, which is the same concept.

sharpleaf

  • Posts: 90
Jon,

Is BitmapData something you can access with blocks somehow?

If it's not then could it be?

Hectate

  • *
  • Posts: 4643
None of the existing blocks could access it. You could use code blocks in conjunction with the rest of the design mode blocks though, if you prefer to stay within design mode and not code mode.

:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.