Sorry to be a pain, but when I added the
parent.parent.checkSingle() it gives me an error.
1061: Call to a possibly undefined method checkSingle through a reference with
static type flash.display:DisplayObjectContainer.
I though my problem was the 'public var testMod:test1 = new test1();', thinking
the 'new test1()' was the problem. So I removed it and just set it to a local
variable.
public var testMod:test1; I was able to see the checkSingle
method in the auto hinting. But when I try to run it now, I get that the
testMod is null.
If I were to do the dispatchEvent route, I would have to attach it to the test1
module, right? this.addEventListener("answered", checkSingle);. How could I
get the variable passed from the components (questions) to the module's
checkSingle() method? There will be between 20-100 of these questions.
----------------------------------------
From: Michael Wills <[email protected]>
Sent: Monday, March 09, 2009 3:21 PM
To: [email protected]
Subject: Re: [flexcoders] Moving a viewStack through actionscript
Hi Wally,
sorry I missed the hierarchy there. You have checkSingle on the module
and not the Viewstack. In that case, you should be able to do
parent.parent.checkSingle and leave out the cast as test1. So
public var testMod:Object = parent.parent;
The last one failed because it was trying to convert a viewstack into a
test1 which of course isn't a viewstack. But I'm not sure about this
one because the parent.parent is a module. You could also forgo using
the testMod variable and in your click declaration instead of
click="testMod.checkSingle(group1.selectedValue)"
use
click="parent.parent.checkSingle(group1.selectedValue)" />
Hopefully that'll work for you.
If you have a little time to invest, and if you may be doing more Flex
apps in the future, even though this one is not going to be a full-on
huge app, I'd learn about dispatching custom events. It's not that
complicated but it takes some time to understand. It would save some of
the hassle you're trying to do of course at the expense of a new and
different kind of hassle. :-)
Michael
Wally Kolcz wrote:
Wow...its a 'simple' one time create
app.
All I want to do is when the user selects an answer on the
question1.mxml component, they can click a button, pass the value of
the radio button to the test1 module's checkSingle() method and
then move to the next question in the test1 module's stack.
When I changed the function call on the button of question1.mxml to
test1(parent).checkSingle(group1.selectedValue);
<mx:Button label="Next"
click="test1(parent).checkSingle(group1.selectedValue)"
/>
I get this error:
TypeError: Error #1034: Type Coercion failed: cannot convert
mx.containers::viewst...@6563ae1 to com.ipexpert.tests.test1.
at
com.ipexpert.questions.q1::question1/___question1_Button1_click()[D:\wkolcz\My
Documents\Flex Builder
3\IPExpertBrainBuster\src\com\ipexpert\questions\q1\question1.mxml:21]
Here is checkSingle() and nextQuestionPlease()
public function checkSingle(answer:Object):void {
if (answer !=0) {
Application.application.score += 1;
}
currentQuestion += 1;
nextQuestionPlease();
}
public function nextQuestionPlease():void {
questionsVS.selectedIndex=currentQuestion;
}
----------------------------------------
From: Michael Wills
<[email protected]>
Sent: Monday, March 09, 2009 11:44 AM
To: [email protected]
Subject: Re: [flexcoders] Moving a viewStack through
actionscript
If I am understanding your code correctly, you actually want to
call
test1(parent).checkSingle instead of testMod. The reason is
because you
are instantiating a brand new, and unrelated, test1 component with
public var testMod:test1 = new test1();
You might be able to do
public var testMod:test1 = test1(parent);
casting the variable to the type test1.
If it's a simple app you are working on, you can do this kind of
tightly
coupled design. By that I mean, your question1 component knows about
your test1 component. If it's a more complex app though, you may want
to
look at "decoupling" your app. Otherwise this app will quickly become
inflexible and harder to maintain as it grows.
In this case, you could set up your question components to dispatch an
event that your test1 then listens to. That way question1 doesn't have
to be directly connected to "parent".
I hope that makes sense!
Michael
Wally Kolcz wrote:
>
> When I added the click="nextQuestion()" it was triggered off
the
> selecting of a radio button and not the button. Here all the code
I
> have. It is telling me that the viewStack doesn't exist when I
believe
> it should.
>
> test1.mxml
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
> width="100%" height="98%"
> xmlns:questions="com.ipexpert.questions.q1.*"
> creationComplete="initApp()" >
> <mx:Script>
> <![CDATA[
> import mx.core.Application;
>
> public var testID:int = 1;
> public var currentQuestion:int = 0;
>
> public function initApp():void {
>
> }
>
> public function checkSingle(answer:Object):void {
> if (answer !=0) {
> Application.application.score += 1;
> }
> currentQuestion += 1;
> nextQuestionPlease();
> }
>
> public function nextQuestionPlease():void {
> }
> ]]>
> </mx:Script>
> <mx:ViewStack id="questionsVS" width="98%" height="100%"
> paddingLeft="10">
>
> <questions:question1 />
> <questions:question2 />
> <questions:question3 />
> <questions:question4 />
> </mx:ViewStack>
> </mx:Module>
>
>
> question1.mxml
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
width="98%"
> height="98%">
>
> <mx:Script>
> <![CDATA[
>
> import com.ipexpert.tests.test1;
>
> public var testMod:test1 = new test1();
> ]]>
> </mx:Script>
>
> <mx:Label fontWeight="bold" text="1. Which layer(s) of the
OSI
> Reference Model provide(s) for internetwork connectivity?"
/>
> <mx:RadioButtonGroup id="group1" />
> <mx:RadioButton paddingLeft="10" groupName="group1"
label="Data
> Link" value="0" selected="true" />
> <mx:RadioButton paddingLeft="10" groupName="group1"
> label="Physical" value="0" />
> <mx:RadioButton paddingLeft="10" groupName="group1"
> label="Session" value="0" />
> <mx:RadioButton paddingLeft="10" groupName="group1"
> label="Network" value="1" />
> <mx:RadioButton paddingLeft="10" groupName="group1"
> label="Presentation" value="0" />
>
> <mx:Button label="Next"
> click="testMod.checkSingle(group1.selectedValue)"
/>
> </mx:VBox>
>
> ----------------------------------------------------------
> *From*: Michael Wills <[email protected]>
> *Sent*: Monday, March 09, 2009 10:55 AM
> *To*: [email protected]
> *Subject*: Re: [flexcoders] Moving a viewStack through actionscript
>
> Hey there Wally,
>
> You can get access to the currently selected viewstack, and also
set it,
> through selectedIndex. So your initial declaration would be like
> <mx:ViewStack id="questionVS" etc... selectedIndex="0"
> click="nextQuestion()">
>
> and in your function nextQuestion you can increment the
selectedIndex.
> I'd have to check but I think you can find the number of items in
your
> viewStack with numChildren. That would give you your max value to
finish
> your quiz or handle the end how you would like.
>
> You can find more about what you can do with viewstacks in the
docs:
>
> http://livedocs.adobe.com/flex/3/langref/mx/containers/ViewStack.html
> <http://livedocs.adobe.com/flex/3/langref/mx/containers/ViewStack.html>
>
> Hope that helps!
>
> Michael
>
> Wally Kolcz wrote:
> >
> > I have a test that I want to walk through the questions using
a
> > function. How can I set the inital value of the ViewStack
> > (questionsVS) to 0 and then increment it by one each time
someone
> > clicks a button?
> >
> > <mx:ViewStack id="questionsVS" width="98%" height="100%"
> paddingLeft="10">
> > <questions:question1 />
> > <questions:question2 />
> > <questions:question3 />
> > <questions:question4 />
> > </mx:ViewStack>
> >
> >
>
>
>