View Full Version : How do I acces a var from the main time line in to a movieclip?
nbautista
May 19th, 2009, 10:59 AM
I'm making a quiz-type animation, since I'm new with ActionScript I had developed my own way to make this work.
I want to add like a meter that shows one out three position depending on the answer given, I had made this animation as a movieclip but now I need it to react according to the answer, so it changes on every question to do that I need to access the variables I'm using in the main time line, how can I access the the main time line variables from the movie clip?
The movie clip (meter) is basically the background and the questions will change and advance on top of that movie clip so it'll change with each answer until you get the final result.
My FLA file is too big to upload it to the forum but I can email it to whom want to help me, THANK YOU!!!
efos
May 19th, 2009, 11:35 AM
The easiest way to do this is to push those variable into your movie clip, rather than have it reach out for them.
write a function on your meter that can receive the input and does what it needs to like:
//inside mcMeter
function foo(v):void{
switch(v){
case 1:
doThis();
break;
case 2:
doThat();
break;
default:
tryAgain();
}
}then when your user selects something just feed it to mcMeter.foo(answer)
nbautista
May 19th, 2009, 12:39 PM
I kinda of make it work using:
var meterVar = MovieClip(this.root).userAnswer; , but I have different problems know I'm getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at divameter2_fla::MovieClip_1/frame1() and the meter movieclip only change from the original position to the second and stays there even if the first answer supouse to sent it to the third or four position. here is the code for the main and movie clip:
Main:
I have following code in separate frames, I know is probably not the best way to do it but I'm new with AS.
Frame 1:
stop();
messageBox.text = "";
var userAnswer:int;
var rbg:Object = rbA.group;
var finalScore:int;
btnCheck.addEventListener(MouseEvent.CLICK, nextBtn);
function nextBtn(evt:MouseEvent):void {
userAnswer = int(rbg.selectedData);
if(userAnswer == 0) {messageBox.text = "Please select an option";
}else{
finalScore = userAnswer;
gotoAndPlay(2);
}
}Frame 2:
stop();
messageBox.text = "";
var rbbg:Object = rbbA.group;
btnCheckTwo.addEventListener(MouseEvent.CLICK, nextBtnTwo);
function nextBtnTwo (evt:MouseEvent):void {
userAnswer = int(rbbg.selectedData);
if(userAnswer == 0) {messageBox.text = "Please select an option";
}else{
finalScore = finalScore + userAnswer;
gotoAndPlay(3);
}
}Frame 3:
stop();
messageBox.text = "";
var rbcg:Object = rbcA.group;
btnCheckThree.addEventListener(MouseEvent.CLICK, nextBtnThree);
function nextBtnThree (evt:MouseEvent):void {
userAnswer = int(rbcg.selectedData);
if(userAnswer == 0) {messageBox.text = "Please select an option";
}else{
finalScore = finalScore + userAnswer;
gotoAndPlay(4);
}
}Frame 4:
stop();
messageBox.text = "";
var rbdg:Object = rbdA.group;
btnCheckFour.addEventListener(MouseEvent.CLICK, nextBtnFour);
function nextBtnFour (evt:MouseEvent):void {
userAnswer = int(rbdg.selectedData);
if(userAnswer == 0) {messageBox.text = "Please select an option";
}else{
finalScore = finalScore + userAnswer;
gotoAndPlay(5);
}
}Frame 5:
stop();
messageBox.text = "";
var rbeg:Object = rbeA.group;
btnCheckFinish.addEventListener(MouseEvent.CLICK, finishBtn);
function finishBtn (evt:MouseEvent):void {
userAnswer = int(rbdg.selectedData);
if(userAnswer == 0) {messageBox.text = "Please select an option";
}else{
finalScore = finalScore + userAnswer;
if (finalScore > 4) gotoAndPlay(6);
if (finalScore > 9) gotoAndPlay(7);
if (finalScore > 13) gotoAndPlay(8);
}
}The las theree frames are the 3 different results and the code they have is only a
stop();The following is the movieclip code:
stop();
var meterVar = MovieClip(this.root).userAnswer;
var btnCheck = MovieClip(this.root).btnCheck;
var btnCheckTwo = MovieClip(this.root).btnCheckTwo;
var btnCheckThree = MovieClip(this.root).btnCheckThree;
var btnCheckFour = MovieClip(this.root).btnCheckFour;
btnCheck.addEventListener(MouseEvent.CLICK, nextBtn);
function nextBtn (evt:MouseEvent):void {
if (meterVar == 1) {gotoAndPlay(2);
if (meterVar == 2) gotoAndPlay(3);
if (meterVar == 3) gotoAndPlay(4);
}else{ gotoAndPlay (1);
}
}
btnCheckTwo.addEventListener(MouseEvent.CLICK, nextBtnTwo);
function nextBtnTwo (evt:MouseEvent):void {
if (meterVar == 1) {gotoAndPlay(2);
if (meterVar == 2) gotoAndPlay(3);
if (meterVar == 3) gotoAndPlay(4);
}else{ gotoAndPlay (1);
}
}
btnCheckThree.addEventListener(MouseEvent.CLICK, nextBtnThree);
function nextBtnThree (evt:MouseEvent):void {
if (meterVar == 1) {gotoAndPlay(2);
if (meterVar == 2) gotoAndPlay(3);
if (meterVar == 3) gotoAndPlay(4);
}else{ gotoAndPlay (1);
}
}
btnCheckFour.addEventListener(MouseEvent.CLICK, nextBtnFour);
function nextBtnFour (evt:MouseEvent):void {
if (meterVar == 1) {gotoAndPlay(2);
if (meterVar == 2) gotoAndPlay(3);
if (meterVar == 3) gotoAndPlay(4);
}else{ gotoAndPlay (1);
}
}This is a link to the almost working project: http://www.touchsuite.com//salon/testing/htmltest/divameter.html
THANK YOU!!
efos
May 19th, 2009, 03:41 PM
That error you're getting may be the tip of the iceberg, if I had to guess I'd say this line:
var rbg:Object = rbA.group;
is triggering it. What is rbA? Is it a movieclip with a 'group' property? Use MovieClip(rbA).group if so.
stop();
var meterVar = MovieClip(this.root).userAnswer;
var btnCheck = MovieClip(this.root).btnCheck;
var btnCheckTwo = MovieClip(this.root).btnCheckTwo;
var btnCheckThree = MovieClip(this.root).btnCheckThree;
var btnCheckFour = MovieClip(this.root).btnCheckFour;
Try not to use root; as root changes depending on where the swf is embedded; but parent will always be the next level up.
So lets change that:
var pRef = MovieClip(parent)
var btnCheck = MovieClip(pRef.btnCheck);
var btnCheckTwo = MovieClip(pRef.btnCheckTwo);
var btnCheckThree = MovieClip(pRef.btnCheckThree);
var btnCheckFour = MovieClip(pRef.btnCheckFour);
Note I dropped your meterVar reference. You referenced the value once; then you changed the parent value without updating the reference to it. So instead, reference the parent value directly with pRef.userAnswer
So change meterVar to pRef.userAnswer in the rest of the code.
ben36
May 20th, 2009, 04:26 AM
Hi nbautista,
Didn't have time to read the entire thread, but from your first post it sounds like this link may be of reference to you:
http://kb2.adobe.com/cps/140/tn_14087.html
EDIT: I'm dumb... thought I was in the AS2 forum. The link is probably of little use.
Let me know if that helps.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.