View Full Version : custom properties to a sprite
werehunt
September 18th, 2009, 03:53 AM
since I cant create a custom property on a sprite if extend a sprite, should be able to make a custom property then or do I need to use a movieClip?
senocular
September 18th, 2009, 10:31 AM
Yes. The entire point of extending classes is to give them new properties or functionality. If you need a custom property on a Sprite, create a class that extends Sprite, define the property there, then use that class in place of the spot where before you needed the Sprite with the custom property
Krilnon
September 18th, 2009, 10:44 AM
The entire point of extending classes is to give them not properties or functionality.
Maybe you meant new instead of not?
senocular
September 18th, 2009, 10:57 AM
Maybe you meant new instead of not?
oops! Fixed; thanks!
werehunt
September 21st, 2009, 06:59 AM
like so?
package {
import flash.display.Sprite;
public class CustomSprite extends Sprite {
private var _case:String;
public function CustomSprite( ) {
}
public function getCase():String{
return _case;
}
public function setCase(value:String):void{
_case = value;
}
}
}
var cs:CustomSprite = new CustomSprite();
trace(cs.getCase());
cs.setCase("V");
trace(cs.getCase());
dail
September 21st, 2009, 07:17 AM
Yep.
senocular
September 21st, 2009, 08:37 AM
Note that ActionScript supports accessors with getters and setters
http://livedocs.adobe.com/specs/actionscript/3/as3_specification92.html
jloa
September 21st, 2009, 09:20 AM
Am, why not use setter/getter methods?
public function set case(value:String):void
{
_case = value;
}
public function get case():String
{
return _case;
}
Usage:
var cs:CustomSprite = new CustomSprite();
trace(cs.case);
cs.case = "V";
trace(cs.case);
werehunt
September 21st, 2009, 09:35 AM
I know about getters and setters but I prefer this way, it feels more natural to me.
using setters it looks like you are setting a variable, but actually you are calling a method.
and then you think, this is a public variable?
shouldnt it be private?
and then you realize, oh its a setter...
using explicit way tells me immediately that I am calling a method.
and even lots of times when we say this for example:
mc.x = 100;
we are using a setter, its not a public variable, i still prefer the other way...
qncenterlab13
September 21st, 2009, 09:40 AM
thanks for a great ideas you sahre..thanks
qncenterlab13
September 21st, 2009, 09:41 AM
since I cant create a custom property on a sprite if extend a sprite, should be able to make a custom property then or do I need to use a movieClip?
Thanks for sharing
QnLabs (http://www.qncenter.com)
jloa
September 21st, 2009, 09:46 AM
I know about getters and setters but I prefer this way, it feels more natural to me.
Ough, ok then. Just trying 2 help. ^_^
U c, setter/getter method r very useful. I don't know if it's really important 2 know whether it's a var or a method, but using setter/getter methods can for example update the display state of your class (btw the "width/height" properties of the DisplayObjects are methods actually, as they call the redraw() method after setting new value).
So, if u do not have to update smth in yr class - use public vars; if u have to - use getter/setters
senocular
September 21st, 2009, 10:10 AM
Ough, ok then. Just trying 2 help. ^_^
U c, setter/getter method r very useful. I don't know if it's really important 2 know whether it's a var or a method, but using setter/getter methods can for example update the display state of your class (btw the "width/height" properties of the DisplayObjects are methods actually, as they call the redraw() method after setting new value).
So, if u do not have to update smth in yr class - use public vars; if u have to - use getter/setters
That's more of an argument between getter/setter properties and class member variables. It doesn't apply to methods. Methods offer all of the advantages as getter/setters, if not more. They mostly only differ in syntax when used.
Krilnon
September 21st, 2009, 11:48 AM
Methods offer all of the advantages as getter/setters, if not more. They mostly only differ in syntax when used.
Unless you count the syntax of accessors as an advantage. :sure:
senocular
September 21st, 2009, 11:54 AM
Unless you count the syntax of accessors as an advantage. :sure:
no one would dare do such a thing! :trout:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.