|
-
variable access in classes
Hey there -
I have a project that requires a multiple choice game. Sadly, I'm not able to store the content in XML, so I have to create arrays to hold the questions and answers. I've chosen to hold the content in a separate .as file, QuizContent.as.
I have a MultipleChoiceQuiz.as file as well, a class to handle the quiz functionality. I'm having trouble accessing the content from the class however.
Here's a snippet of the QuizContent.as file:
Code:
var qArray:Array;//store questions
var fArray:Array;//store feedback
qArray[0] = "which are problems for the world's oceans?"//question
qArray[0][0] = "overfishing";
qArray[0][1] = "pollution";
qArray[0][2] = "coastal development";
qArray[0][3] = "all of the above";
fArray[0] = "all of these are problems";//feedback
and what I want to do with the class:
Code:
//==============================================================================
class com.asdf.MultipleChoiceQuiz {
//==============================================================================
private var score:Number;
//==============================================================================
public function MultipleChoiceQuiz() {
//==============================================================================
this.score = 0;
initQuiz();//initialize quiz question array
}
private function initQuiz(){
trace(qArray.length);
}
}
I'm just seeing if I can access the qArray from the class to pick a random group of questions from the available content.
Here's how the content/classes are loaded in my main timeline:
Code:
import com.asdf.*;
#include "as/Landing.as"
#include "as/QuizContent.as"
var act:Activity = new Activity();
var quiz:MultipleChoiceQuiz = new MultipleChoiceQuiz();
And the issue is that the class can't access the quiz content arrays.
Any help is much appreciated. BTW - this is AS2 (flash 8 target player)
Thanks!
-
Senior Member
Hi, for one the code you have in QuizContent.as is not a class. It's simply actionscript code that gets included in your timeline when you compile the movie. I don't know how you want to set up your array, but what you're doing is storing a string in the first entry, then proceding to store other strings in that string, which is incorrect.
You should have something like this instead to store the answers.
PHP Code:
qArray[0] = new Array(); qArray[0][0] = "overfishing"; qArray[0][1] = "pollution"; qArray[0][2] = "coastal development"; qArray[0][3] = "all of the above";
I don't know where you'd store the question in that setup, it's up to you.
Thirdly code from QuizContent.as gets included in the timeline while your trying to access it in an object of class MultipleChoiceQuiz. That object only has visibility of variables and methods inside itself. You can still access global variables using the _global object, but your quiz data is being stored on the timeline not globally and not in MultipleChoiceQuiz object, so it will not have access to it. If you want the quiz data to be accessible in MultipleChoiceQuiz object with the setup you have you should include QuizContent.as inside the class definition of MultipleChoiceQuiz. This will work, but is really not the best solution.
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
|