1
votes

I am wondering if it is possible to get the data that is stored in a shape/graphics object in flash using actionscript 3?

In my project I would like to be able to draw a shape and then read all the points in that shape into my script. The reason for that is that I need go generate lines from those points that I later can use to check if my characters velocity intersects with any of them.

4
Perhaps you could expand on exactly what you're trying to achieve? What sort of shapes you drawing? Ultimately do you just need to do a hit-test between your character and the shapes?heavilyinvolved

4 Answers

6
votes

You can read all parts of Shape.

New feature added to Flash Player 11.6 and AIR 3.6:

flash.display.Grapics.readGraphicsData()

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html#readGraphicsData%28%29

Example:

var s :Shape = new Shape();
s.graphics.lineStyle(2, 0xFF0000);
s.graphics.drawCircle(0, 0, 50)

var gd:Vector.<IGraphicsData> = s.graphics.readGraphicsData(false);

var copy_of_s :Shape = new Shape();
copy_of_s.graphics.drawGraphicsData(gd);

addChild(copy_of_s);

To use the new version, you have to update playerglobal.swc

http://www.adobe.com/support/flashplayer/downloads.html

2
votes

You cannot read the shape info once it's drawn. But if you are drawing it, you can store the info at the time of drawing itself and use it later.

0
votes

OK, looks like its not possible then, to bad.

I am doing a 2d topdown racing game, and I wanted to generate lines along the walls of the track and check the players velocity against thouse lines. That way I would be able to implement some basic collision response by reflection the players velocity around the normal of the line that it collides with and make it bounce off walls. Does anyone have any good ideas about how to get the same type of collision behavior without the actual lines?

Is it possible to overload the graphics object in flash somehow so that when I draw something it is recorded? Or does the flash IDE not use the Graphics drawing api?

Regards

0
votes

You can not instantiate or subclass Graphics class. But you can use you own custom graphics class.

public class CustomGraphics extends Object{
  private static const CLEAR = -1;
  private static const MOVETO = 0;
  private static const LINETO  = 1;
  ...
  ...
  private var _graphics:Graphics;
  private var _actions:Array;
  public function CustomGraphics(g:Graphics):void {
    _graphics = g;
    _actions = new Array();
  }
  private function recordAction(obj:Object):void {
    if (obj.action == -1) {
      _actions = null;
      _actions = new Array();
      return;
    }
    _actions.push(obj);
  }
  public function moveTo(x:number, y:Number):void {
    g.moveTo(x, y);
    recordAction({action:MOVETO, X:x, Y:y});
  }
  ...
  ...
  public function get actions():Array {
    return _actions;
  }
}
var cg:CustomGraphics = new CustomGraphics(someDisplacyObject.graphics); cg.moveTo(0, 0); cg.drawRect(0, 0, 10,, 200); ... a:Array = cg.actions;