PDA

View Full Version : Sprite Problem



cesco
October 14th, 2009, 01:06 PM
Hi!

I'm trying to program a little tail that should follow a dot moving around the scene.
For this purpose I've meda a class in AS3 but I can't get it to work. The idea is that the tail will receive a position and draw a line from the last received position to the new one.

I've got this code but I can't make it work:


package {
import flash.geom.Vector3D;
import flash.geom.Point;
import flash.display.Sprite;
import flash.display.Graphics;
public class Tail extends Sprite{

private var lastPos:Vector3D;
private var lines:Vector.<Sprite>;
private var maxLength:int;

public function Tail(maxLength:int, initialPos:Vector3D) : void {

lines = new Vector.<Sprite>();
this.maxLength = maxLength;
this.lastPos = initialPos;

}
public function push(newPos : Vector3D) : void {
var sprite = new Sprite();

sprite.graphics.lineStyle(1,0xFF0000);

var ps:Point;
ps = sprite.local3DToGlobal(lastPos);
sprite.graphics.moveTo(ps.x, ps.y);

ps = sprite.local3DToGlobal(newPos);
sprite.graphics.lineTo(ps.x, ps.y);

addChild(sprite);

lines.push(sprite);
if (lines.length > maxLength) removeChild(lines.shift());
}

}
}

And it is called from a fla with this code:


import flash.geom.Vector3D;

var v:Vector3D = new Vector3D(0,0,0);
var t:Tail = new Tail(5,v);
v = new Vector3D(50,80,0);
t.push(v);
v = new Vector3D(150,300,0);
t.push(v);
v = new Vector3D(200,80,0);
t.push(v);

this.addChild(t);

Any help would be much apreciated. Thanks!!

cesco
October 14th, 2009, 03:41 PM
Ok, debuging I've found that the problem is in the local3DToGlobal function which always return point (0, 0). Any ideas?

efos
October 14th, 2009, 04:23 PM
Adobe's Local3DToGlobalExample.as does the same thing...

cesco
October 14th, 2009, 04:35 PM
Well it's similar but I can't get mine to work. Their example is a MovieClip, so it's not instanciated by any class. There must be some problem with this method if you call it from inside a class which is not a movieclip and doesn't share the main stage.

Does anybody have an idea of how this works?

efos
October 14th, 2009, 04:40 PM
Googled a bit; tested your code, works. I guess it's a hierarchy thing.

t must be on the stage before you call local3DToGlobal.

Then call this.local3DToGlobal (or just local3DToGlobal ) instead of sprite.local3DToGlobal in push().

cesco
October 14th, 2009, 05:04 PM
Thank you very very much :)


Googled a bit; tested your code, works. I guess it's a hierarchy thing.

t must be on the stage before you call local3DToGlobal.

Then call this.local3DToGlobal (or just local3DToGlobal ) instead of sprite.local3DToGlobal in push().