PDA

View Full Version : Glue data to sprite


Aiwa
08-30-2006, 09:49 PM
package
{
import flash.display.Sprite;
import flash.text.*;
import flash.events.MouseEvent;

final public class main
{
// --- Class Propertie ----------------------------------------
private var conref:Sprite;
private var btn:Sprite;
// --- Class Constructor --------------------------------------
final public function main(param_con)
{
this.conref = param_con;
// --- Init ---
this.createBtn();
}
// --- Class Method: Create btn --------------------------------
final private function createBtn()
{
// --- Create btn ---
btn = new Sprite();
btn.vars = "Storing data"; // <-- How do I glue data to a sprite in AS3 and trace it later
var btn_txt:TextField = new TextField();
btn_txt.text = "press me";
btn_txt.selectable = false;
btn.addChild(btn_txt);
btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
conref.addChildAt(btn, 0);
}
// --- Class Method: Mouse over --------------------------------
final private function mouseDown(event:MouseEvent):void
{
trace(btn.vars);
}
}
}


(How do I glue data to a sprite in AS3 and trace it later)

I how do I solve this error sence in AS2 it where okey to do it but itīs a hole other beef whit AS3 or is it ?

:)


ReferenceError: Error #1056: Cannot create property vars on flash.display.Sprite.
at main/main::createBtn()
at main$iinit()
at Timeline0_86a764da85234e4a8b79f0be4754258/::frame1()

Tink
08-31-2006, 12:39 AM
You would need to extend Sprite and add 'vars' as a public var or a getter/setter. Then create an instance of your new class instead of a standard Sprite which doesn't have those properties by default.

Aiwa
09-01-2006, 11:04 AM
Tink could you post an example of what your talking about ? I understand how to make a public var and how to extend the class with sprite keyword but I donīt know what you mean by making an instance of the class in the class and how that couples with the btn and storing data on it (Glueing) ?

Best regards
Aiwa
:rolleyes:

Fall_X
09-01-2006, 11:26 AM
All he means is you need to make a class like this :


package {
public class DataSprite extends Sprite {
public var vars:Object;
public function DataSprite () {
// Constructor code if needed
}
}
}


Then, in your code, you currently have this :


btn = new Sprite();


So just change that to


btn = new DataSprite(); // instantiate the new class instead of Sprite


Note that the name DataSprite should probably be something that makes more sense. I called it that because you can add some data to it. Also, I called the property "vars" because you used that name, but I think it's a bad choice since it's only one var, so giving it a plural name is confusing. I would call it "tag" (which is used in .net to add a reference variable to some objects). I set it's datatype to object because you might want to store other things than strings.

Aiwa
09-08-2006, 03:42 PM
Thnx for the the response guys

Tried and it works now, nice to learn AS3...

:rolleyes: