Skip to main content
Known Participant
February 14, 2013
Question

getting an error and been trying to fix it.

  • February 14, 2013
  • 2 replies
  • 967 views

Ive been trying to find whats wrong and that but no luck at all. Can anyone help me out..

Thank you

TypeError: Error #1009: Cannot access a property or method of a null object reference.

  at mygame_fla::MainTimeline/talk()

import com.choices.*;

// hold all questions

var allQuestions:Array = new Array();

function setupChoices(){

          var tempQuestion:question;

          tempQuestion = new question( 1 , "What are you doing here?" , 0xFF000 );

          var tempAnswer:answer;

          tempAnswer = new answer( "I'd like a café latté please." , trace , "EVERYTHING IS WORKING..." );

          tempQuestion.addAnswer( tempAnswer );

          tempAnswer = new answer( "Shut up." , doQuestion , 2 );

          tempQuestion.addAnswer( tempAnswer );

          // add finished question to list of all...

          allQuestions.push( tempQuestion );

          // question 2

          tempQuestion = new question( 2 , "What's it all about?" , 0xFF000 );

          tempAnswer = new answer( "It's about nothing." , trace , "Question two has been answered." );

          tempQuestion.addAnswer( tempAnswer );

          tempAnswer = new answer( "What did you ask me a second ago?" , doQuestion , 1 );

          tempQuestion.addAnswer( tempAnswer );

          // add finished question to list of all...

          allQuestions.push( tempQuestion );

          trace( allQuestions.length );

}

setupChoices();

function getQuestionByID( idARG:int ):question{

          var i:int;

          var lim:int = Math.max( 0 , allQuestions.length );

          var item:question;

          findloop:for( i=0; i<lim; i++ ){

                    item = allQuestions; // access array element

                    if( idARG == item.id ){

                              // found it!

                              break findloop;

                    } // end if

          }// close forloop

          return( item );

} // close function

var selectAnswerBOOL:Boolean = true;

var currentQuestion:question;

currentQuestion = getQuestionByID( 1 );

showQuestion( currentQuestion );

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

stage.addEventListener(KeyboardEvent.KEY_DOWN , talk );

stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownList );

stage.addEventListener(KeyboardEvent.KEY_UP , keyUPList );

charMC.addEventListener(Event.ENTER_FRAME , movementchar);

//addEventListener(Event.ENTER_FRAME , npcTalk);

var leftKeyDown:Boolean = false;

var rightKeyDown:Boolean = false;

var upKeyDown:Boolean = false;

var downKeyDown:Boolean = false;

var talkNPC:Boolean = false;

var charSpeed:Number = 7; // Character Speed

function talk( ke:KeyboardEvent):void{

                    if( selectAnswerBOOL == true ){

                              trace(selectAnswerBOOL);

                              var chosenAnswer:answer;

                              switch( ke.keyCode ){

                                        case 49: // number 1 key

                                                  // select answer number 1

                                                  trace("1 key is pressed");

                                                  // set it

                                                  chosenAnswer = currentQuestion.answersList[0];

                                        break;

                                        case 50: // number 2 key

                                                  // set it

                                                  chosenAnswer = currentQuestion.answersList[1];

                                        break;

                                        default:

                                                  trace( ke.keyCode );

                                        break;

                              } // close switch

                              // use selected answer to establish next function and provide argument

                              chosenAnswer.nextFunction(chosenAnswer.argument);

                              trace(chosenAnswer);

                    } // if close (selectAnswerBOOL)

} // close talk

//char movements

function keyDownList(ke:KeyboardEvent):void{

          if(ke.keyCode == 81){

                    leftKeyDown = true;

          }

          if(ke.keyCode == 68){

                    rightKeyDown = true;

          }

          if(ke.keyCode == 87){

                    upKeyDown = true;

          }

          if(ke.keyCode == 83){

                    downKeyDown = true;

          }

          if(ke.keyCode == 84){

                    talkNPC = true;

          }

}

function keyUPList(ke:KeyboardEvent):void{

          if(ke.keyCode == 81){

                    leftKeyDown = false;

          }

          if(ke.keyCode == 68){

                    rightKeyDown = false;

          }

          if(ke.keyCode == 87){

                    upKeyDown = false;

          }

          if(ke.keyCode == 83){

                    downKeyDown = false;

          }

          if(ke.keyCode == 84){

                    talkNPC = false;

          }

}

function movementchar(event:Event):void{

          if(leftKeyDown){

                    charMC.x -= charSpeed;

          }

          if(rightKeyDown){

                    charMC.x += charSpeed;

          }

          if(upKeyDown){

                    charMC.y -= charSpeed;

          }

          if(downKeyDown){

                    charMC.y += charSpeed;

          }

}

/*function npcTalk (ev:Event):void{

          if(charMC.hitTestObject(npc)){

                    trace("talk");

          }

}*/

function showQuestion( q:question ){

          //if(talkNPC == true){

                    //if(charMC.hitTestObject(npc)){

                              dynTF.text="";

                              dynTF.appendText( q.questionText );

                              dynTF.appendText( "\n1. " + q.answersList[0].answerText );

                              dynTF.appendText( "\n2. " + q.answersList[1].answerText );

                    //}

          //}

}

function doQuestion( idnoARG:int ){

          currentQuestion = getQuestionByID( idnoARG );

          showQuestion( currentQuestion );

}

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
February 14, 2013

The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

 

- is declared but not instantiated

- doesn't have an instance name (or the instance name is mispelled)

- does not exist in the frame where that code is trying to talk to it

- is animated into place but is not assigned instance names in every keyframe for it

- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).

 

If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

aganuzAuthor
Known Participant
February 14, 2013

yeah i worked it out now..

Now im trying to trigger when "T" is pressed it should show the question/answer, it doesn't show anything when i collide to npc and press T.

function showQuestion( event:question ){

          if(talkNPC == true){

                    if(charMC.hitTestObject(npc)){

                              dynTF.text="";

                              dynTF.appendText( event.questionText );

                              dynTF.appendText( "\n1. " + event.answersList[0].answerText );

                              dynTF.appendText( "\n2. " + event.answersList[1].answerText );

                    }

          }

}

Ned Murphy
Legend
February 14, 2013

Use the trace() function to determine if the function is being called, and if so, why whatever inside that function is failing to execute.  Also, if necessary, use it to track backwards in the processing to see where things are not executing as you expect if the function is performing correctly for what it is processing.

aganuzAuthor
Known Participant
February 14, 2013

Ahh might just reply to this than editing it.

I forgot to mention that it only shows error when i press other keys like the movement function. but when pressing 1 or 2 to answer the question it doens;t show any error.