-
Senior Member
this._parent
I am reading the book, Actionscript for FLash MX- Definitive Guide by Colin Moock and I just finished making a quiz. Here is the code some of the code I used, and where my question is.
choice1_btn.onRelease = function () {
q2answer = 1;
gotoAndStop("quizEnd");
};
//Code executed when button 2 is pressed.
choice2_btn.onRelease = function () {
q2answer = 2;
gotoAndStop("quizEnd");
};
//Code executed when button 3 is pressed.
choice3_btn.onRelease = function () {
q2answer = 3;
gotoAndStop("quizEnd");
};
The quiz works fine with this code. In the book though it tells me to put
choice3_btn.onRelease = function () {
this._parent.q2answer = 3;
this._parent.gotoAndStop("quizEnd");
};
but with this code, when I press the buttons, they don't move to the next part. Can anyone tell me why this is and maybe explain to me what the "this._parent" does.
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
Senior Member
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
Senior Member
Your code will work, as long it is attached to a frame in the timeline of the movie you are trying to manipulate.
However, looking at your code, Colin Moock's code should also work.
An explanation of 'this' and '_parent' follows.
what is _parent?
For any symbol x, x._parent refers to the symbol that the symbol x is contained within. So for example, if you create a movieclip called sheep, and then inside sheep you create a movieclip called charley, then charley._parent is sheep.
what is this?
For any event handler in a frame-based script, this refers to the item that the event handler belongs to. So in your example,
code:
choice3_btn.onRelease = function () {
this._parent.q2answer = 3;
this._parent.gotoAndStop("quizEnd");
};
this refers to choice3_btn and this._parent refers to whatever choice3_btn is attached to (presumeably _root), but possibly some other movieclip.
Now, if you don't explicitly use a pathname to refer to something, as you chose not to do in your code, then flash assumes that you are referring to the enclosing movieclip - whatever owns the timeline that the script is attached to
(commonly, but not always, _root).
This is why your script is working, since your script is probably attached to a frame in the timeline of the movie you are trying to manipulate - so the references are defaulting to that.
I'm not sure why the Moock technique is not working for you, however. If you post a .fla, I might be able to figure out why.
Hope this helps.
- Jim
-
Senior Member
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
Senior Member
Thanks. Your button scripts (which are scripted Moock-style) seem to be working fine on my end (that is, it progresses thru the quiz and scores it correctly).
If I were scripting this, I would leave out the this._parent stuff because it's really not needed, but it does seem to be working on my end. I'm wondering if maybe you have an old version of the Flash player lurking about...
-
Senior Member
maybe, how would I find out?
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
Senior Member
nevermind, for some odd reason when I typed back in the this._parent.whatever it works. Before when I clicked on teh buttons it just didn't move. Maybe I didn't type it in the right way last time. Oh well, thanks
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|