|
-
Object = null
Hi there, I'm having a problem I cant figure out.
Im passing from my main class some values from an array which is in the Main class to a lower class. It is working for one of the classes, but when I try it with another class it says the class is not filled with my array values.
Main class:
Actionscript Code:
package { import events.EventType; import flash.display.Sprite; import flash.events.Event; /** * ... */ public class Main extends Sprite { private const QUESTIONS:Array = ["How old are you?", "Whats your hair color", "How ugly are you", "end"] private const ANSWERS:Array = ["22", "one feet", "very"] private const POSSIBLE_ANSWERS:Array = [ ["22", "21", "20"], ["brown", "blonde", "red"], ["very", "not", "a bit"], ["this is the end", "c", "c"] ] private const CORRECTED_TEXT:Array = ["Correct Answer! 20 is the good answer!", "Correct Answer! brown is the good answer!", "Correct Answer! ugly is the good answer!"] public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); //addQuestionsClass(); addAnswersClass(); } private function addAnswersClass():void { var answers:Answers = new Answers(ANSWERS, POSSIBLE_ANSWERS, QUESTIONS, CORRECTED_TEXT); addChild(answers); } } }
Answer Class:
Actionscript Code:
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; /** * ... */ public class Answers extends Sprite { private var _answers:Array; private var _questions:Array; private var _possibleAnswers:Array; private var _correct:Array; private var _numberOfAnswers:Number = 3; private var _activeId:int; private var _answerItems:Array; private var _itemClicked:Boolean = false; private var box:Sprite; private var close:Sprite; private var next:Sprite; private var clickedId:int; private var questionId:int = 0; private var answerId:int = 0; private var correctedAnswerTextId:int = 0; private var question:QuestionItem; private var answer:AnswerItem; private var answerHolder:Sprite; private var questionHolder:Sprite; private var correctHolder:Sprite; private var _correctAnswerTextHolder:Array; public function Answers(answers:Array, possibleAnswers:Array, questions:Array, correct:Array):void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); _answers = answers; _possibleAnswers = possibleAnswers; _questions = questions; _correct = correct; } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point showQuestions(questionId); showAnswer(answerId); setItemActive(_activeId); okayBox(); createCloseBox(); createNextButton(); showCorrectText(correctedAnswerTextId); }
private function showQuestions(id:int):void { questionHolder = new Sprite(); addChild(questionHolder); for (var i:int = 0; i < 3; i++) { question = new QuestionItem(_questions[id]); question.id = i; questionHolder.addChild(question); question.x = 300; question.y = 100; } } private function showAnswer(id:int):void { answerHolder = new Sprite(); addChild(answerHolder); _answerItems = []; for ( var i:int = 0; i < 3; i++) { answer = new AnswerItem(_possibleAnswers[id][i]); answerHolder.addChild(answer); answerHolder.x = 300; answerHolder.y = 200; answer.y = 25 * i; answer.addEventListener(MouseEvent.CLICK, clickAnswer); answer.buttonMode = true; answer.mouseChildren = false; answer.id = i; _answerItems.push(answer); } } private function showCorrectText(id:int):void { correctHolder = new Sprite; addChild(correctHolder); _correctAnswerTextHolder = []; for ( var i:int = 0; i < 3; i++) { var _correctAnswerText:CorrectAnswerText = new CorrectAnswerText(_correct[id][i]); correctHolder.addChild(_correctAnswerText); correctHolder.x = 210; correctHolder.y = 210; _correctAnswerText.id = i; _correctAnswerTextHolder.push(_correctAnswerText); } } private function setItemActive(newActiveId:int):void { AnswerItem (_answerItems[_activeId]).active = false; AnswerItem (_answerItems[newActiveId]).active = true; _activeId = newActiveId; } // clicks and e.current target is the answer you clicked + id // item is active // if item is active show box private function clickAnswer(e:MouseEvent):void { clickedId = AnswerItem (e.currentTarget).id; setItemActive (clickedId); _possibleAnswers[clickedId]; _itemClicked = true; if (_itemClicked) { box.visible = true; close.visible = true; next.visible = true; } else { box.visible = false; close.visible = false; next.visible = false; } } // create a box to make your answer sure. private function okayBox():void { box = new Sprite(); addChild(box); box.graphics.beginFill (0xFFF000, 1); box.graphics.drawRect(200, 200, 400, 200); box.graphics.endFill(); box.visible = false; } //create a box to close the "are you sure" box. private function createCloseBox():void { close = new Sprite(); addChild(close); close.graphics.beginFill (0xFF000, 1); close.graphics.drawRect(200, 200, 10, 10); close.graphics.endFill(); close.addEventListener(MouseEvent.CLICK, closeBox); close.visible = false; close.buttonMode = true; } private function closeBox(e:MouseEvent):void { box.visible = false; close.visible = false; next.visible = false; } private function createNextButton():void { next = new Sprite(); addChild(next); next.graphics.beginFill (0xFF000, 1); next.graphics.drawRect(200, 400, 40, 10); next.graphics.endFill(); next.addEventListener(MouseEvent.CLICK, clickNext); next.visible = false; next.buttonMode = true; } private function clickNext(e:MouseEvent):void { checkAnswer(); } private function checkAnswer():void { //check if answer is right or wrong by users choice. //splice the first answers of the array if (_possibleAnswers[clickedId] == _possibleAnswers[0]) { questionHolder.visible = false; box.visible = false; close.visible = false; next.visible = false; questionId++; showQuestions(questionId); questionHolder.visible = true; answerHolder.visible = false; answerId++; showAnswer(answerId); answerHolder.visible = true; correctHolder.visible = false; correctedAnswerTextId++; showCorrectText(correctedAnswerTextId); correctHolder.visible = true; setPopUpAbove(); hideAllQuestions(); } else { trace("you fail"); }
} private function setPopUpAbove():void { setChildIndex(box, 1); setChildIndex(answerHolder, 0); } private function createEndScreen():void { var endScreen:Sprite = new Sprite(); addChild(endScreen); endScreen.graphics.beginFill(0xFF0000, 1); endScreen.graphics.drawRect(450, 200, 200, 200); endScreen.graphics.endFill(); } private function hideAllQuestions():void { for (var i:int = 0; i < _questions.length; i++) { if ( questionId > 2) { questionHolder.visible = false; answerHolder.visible = false; createEndScreen(); } } } public function get activeId():int { return _activeId; } public function set activeId(value:int):void { _activeId = value; setItemActive(value); } } }
Does anybody see what I did wrong? I cant figure it out.
Thanks.
-
I see the array values are passed in, so thats not the problem.
The problem is that I get an error from the CorrectAnswerText Class, it keeps saying that it is null for some reason, but I did make the class and I did save it in the right directory.
Anyone can help me solve this? Am I not seeing things?
-
maybe how you are merging that one multi array?
private const POSSIBLE_ANSWERS:Array = [ "22", "21", "20", "brown", "blonde", "red", "very", "not", "a bit", "this is the end", "c", "c" ]
maybe just define the array as a single one...
and you know that the answers come in sets of 3 so just unshift the array by three once you are done with that set of answers?
Last edited by YBAB; 01-22-2012 at 06:49 PM.
-
Code:
var _correctAnswerText:CorrectAnswerText = new CorrectAnswerText(_correct[id][i]);
You are referring to your _correct array as if it were 2 dimensional (array of arrays), but it is not. _correct[id] is the text, there is no _correct[id][i]. And I'm not sure why you have that in a loop anyway.
-
 Originally Posted by 5TonsOfFlax
You are referring to your _correct array as if it were 2 dimensional (array of arrays), but it is not. _correct[id] is the text, there is no _correct[id][i]. And I'm not sure why you have that in a loop anyway.
I made a var as you can see which is called CorrectedTextID which I give to the showCorrectedText function. There i nthe function itself it gets an ID which is the CorrectedTextID. So thats why I use [id] in the for loop.
@YBAB
sorry I cant really understand you!
-
Your variable is correctedAnswerTextId not CorrectedTextID.
I never said you shouldn't use the id, I said you shouldn't attempt to get the ith element of _correct[id] because _correct[id] is the thing you want.
So, remove the loop I said I didn't know why you were using, and use just the id.
Code:
private function showCorrectText(id:int):void{
correctHolder = new Sprite();
addChild(correctHolder);
_correctAnswerTextHolder = [];
var _correctAnswerText:CorrectAnswerText = new CorrectAnswerText(_correct[id]);
correctHolder.addChild(_correctAnswerText);
correctHolder.x = 210;
correctHolder.y = 210;
_correctAnswerText.id = id;
_correctAnswerTextHolder.push(_correctAnswerText);
}
Now I'm not sure why you're creating correctHolder and _correctAnswerTextHolder within this function, and I'm not at all sure what you are doing with _correctAnswerText.id. But whatever, different problems.
-
 Originally Posted by Mushrambo
sorry I cant really understand you!
if you dont understand a single array and how to unshift it... then how to you expect to work a multidimensional array?
-
Thanks for thinking with me to solve this problem, it has been solved!
It was something stupid I should've known. It is exactly what 5TonsOfFlax said, remove the loop. I couldnt see the problem until now.
@YBAB
It is not that I dont know how any form of arrays work, but it was just I didnt really understand what you said.
Even though thanks you guys
-
 Originally Posted by Mushrambo
Thanks for thinking with me to solve this problem, it has been solved!
It was something stupid I should've known. It is exactly what 5TonsOfFlax said, remove the loop. I couldnt see the problem until now.
@YBAB
It is not that I dont know how any form of arrays work, but it was just I didnt really understand what you said.
Even though thanks you guys 
WOOT! im stoked you figured it out!
leave it to flax to know what to do!
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
|