AS 2.0 Classes
- Started
- Last post
- 11 Responses
- daveFelton
So I have a class, and one of the initial properties is
private var myShape:MovieClip;
and later in the constructor
myShape = path.createEmptyMovieClip(...
works splendidly. then later, i have a method applying an action.
...
myShape.onRelease = function() {
trace(myShape);
}
...and its undefined. if i trace any of the initial properties, they come up undefined. can i not access those from insde a clip whose actions were set in a class method? i hope this makes sense.
i'm new to classes, so my terminology may be off. sorry!
- UndoUndo0
that looks like a scope issue, your not passing anything to the function or specificying where myShape is so its is undefined.
- fugged0
try:
myShape.onRelease = function() {
trace(this);
}within the scope of the function, "this" will refer to the object myShape.
- daveFelton0
yeah, i was trying to access a different property than myShape, actually...i just used that in the example to make it simpler. in reality i'm trying
myShape.onRelease = function() {
trace(objectId);
}but that is being set where myShape is, and I can trace it outside of the action, just not from inside the onRelease.
maybe i should just do:
myShape.objectId = id;
when i assign myShape a value, then later
trace(this.objectId);
should work, yeah?
- UndoUndo0
if objectID is within the class whenere the button function is you could try
myShape.onRelease = function() {
trace(parent.objectId);
}
- daveFelton0
_parnet didnt work either.
but i just did myShape.id = id; and later it lets me trace(this.id)...so all is well.
thanks much for the insight.
- UndoUndo0
dave, it might be also worth yr while reading up on 'get' and 'set' methods for classes. these will help you with what yr doing
- daveFelton0
yeah i will certainly look into those. thanks for the tip.
- fugged0
Where is objectId being defined, and is it a member of the class, or just a local variable?
- caseyc0
Know that AS2 is just a friendlier way of writing AS1. When you publish to swf the flash compiler goes through all the as2 and the resulting bytecode is just as1, old school prototype stuff.
Basically what happens is that you can write in AS2:
myVar
Which is then converted in the swf to:
this.myVar
And the compiler doesnt touch your onRelease function so it is just looking for "myVar" when in reailty it needs "this._parent.myVar" to work.
This is what things like mx.utils.Delegate are for, but if you dont want to use that the shortcut looks something like:
var self = this;
myButton.onRelease = function() {
trace(self.myVar);
}Cheers,
- kinetic0
use this._parent
you can also do shit like create vars on init called classPath
then in the event handler or whatever it is that cant obtain data from somewhere else, just make a new var called P_classPath (private class path) and refernece vars from it
so something created in the class would be P_classPath.variableName
- UndoUndo0
another helpful tip is using the _global identifer.
ie.
_global.myVarthis means that myVar is accessible in any scope in yr flash movie!