all groups > flash actionscript > october 2005 >
You're in the

flash actionscript

group:

MovieClip inside Class problem


MovieClip inside Class problem Devendran
10/15/2005 12:12:00 PM
flash actionscript:
Hi,
I have created one class file named myClass
It contains
How can i acces the class variable in side the onrelease function
The objects are created by user, so how can i access the variable.

/************************************************************/


class myClass extends MovieClip{
var myVar_str:String;
function myClass(){
myVar_str = " Trial Text";
}
function duplicate(){
for(var i=0;i<10;i++){
_root.attachMovie("myMC","newMc_mc"+i,i);
_root["newMc_mc"].onRelease= function(){
//Here I would like to Access the class variable myVar_str
//How can i access it, I don't know the Object name created to this class
//Because its a dynamic created object by user.
}
}
}
}
Re: MovieClip inside Class problem LuigiL
10/15/2005 12:24:17 PM
Use a local reference to the current class as attached.



class myClass extends MovieClip{
var myVar_str:String;
function myClass(){
myVar_str = " Trial Text";
}
function duplicate(){
var thisObj:myClass=this; //local reference to the current class
for(var i=0;i<10;i++){
_root.attachMovie("myMC","newMc_mc"+i,i);
_root["newMc_mc"].onRelease= function(){
var testvar=thisObj.myVar_str;
//Here I would like to Access the class
variable myVar_str
//How can i access it, I don't know the Object
name created to this class
//Because its a dynamic created object by user.
}
}
}
}
Re: MovieClip inside Class problem 2m
10/15/2005 12:26:53 PM
As I generally favour compositon over inheritance I'm not very expirienced with
your way doing it here, but in my understanding you should just be able to
acces it as this.myVar_str.
Inside the onRelease handler "this" is the movieClip that triggered the event,
and as your class (myClass :)) extends the clip myVar_str should be a property
of the clip that is clicked.
Re: MovieClip inside Class problem LuigiL
10/15/2005 12:52:40 PM
Re: MovieClip inside Class problem LuigiL
10/15/2005 12:54:30 PM
And rewrite your class file like attached (your code won't work).

Re: MovieClip inside Class problem 2m
10/16/2005 12:00:00 AM
LuigiL:
I tried, and it worked. There was a small mistake in the code, that I
corrected (the bold part):

class myClass extends MovieClip
{
var myVar_str : String;
function myClass ()
{
myVar_str = " Trial Text";
}
function duplicate ()
{
for (var i = 0; i < 10; i ++)
{
_root.attachMovie ("myMC", "newMc_mc" + i, i);
_root ["newMc_mc"+i].onRelease = function ()
{
//Here I would like to Access the class variable myVar_str
//How can i access it, I don't know the Object name created to this class
//Because its a dynamic created object by user.
trace(this.myVar_str);
}
}
}
}

and as th clip in the library with linkage identifier myMC had also been in
the AS Class myClass everything worked as I expected.

I'd say it's your time to try if you like, or I'll post a zip if you want.

Markus
Re: MovieClip inside Class problem LuigiL
10/16/2005 12:00:00 AM
[quoted text, click to view]
the AS Class myClass everything worked as I expected.

No need to try, you're correct. The difference is making the myMC a member of
the class myClass in the library. Then your reference to 'this' works. I didn't
read the last lines in your (previous) post.
Re: MovieClip inside Class problem 2m
10/16/2005 10:38:48 AM
Hi LuigiL,

I was quite sure (and am still) that with the this keyword inside an onRelease
handler you would always refer to the movieClip over which the mouse is
relaesed.

This is also stated in the Helpfiles: Learning ActionScript 2.0 in Flash >
Handling Events > Scope of the this keyword

In the case we are discussing (and under the assumption that the attached
clips are members of the class we are looking into) the class and the clip
should be the same, but as don't work with classes that extend MovieClip
usualy, I'm not really sure about that.
Re: MovieClip inside Class problem LuigiL
10/16/2005 1:05:40 PM
AddThis Social Bookmark Button