views:

229

answers:

3

Hallo,

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.

Regards,

+1  A: 

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.

Amarghosh
A: 

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

Ole
A: 

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;
  }
}
Now whenever you want to draw something then you can use CustomGraphics. var cg:CustomGraphics = new CustomGraphics(someDisplacyObject.graphics); cg.moveTo(0, 0); cg.drawRect(0, 0, 10,, 200); ... a:Array = cg.actions;

bhups
Thank you, but that will only work if I do the drawing using scripts. In order to do that I need to know where the points are. The problem is how it easily put down a lot of points that I can access (get the position) in the script.
Ole Kristian