A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: as3 can't assign text to a variable

  1. #1
    Ryan :D ryanp321's Avatar
    Join Date
    Nov 2007
    Location
    Wallasey
    Posts
    292

    as3 can't assign text to a variable

    Hi, i've gotten pretty close on this one,
    i wanted to make a little quiz kind of game, 4 choices and a sum randomly displayed.
    And i wanted to assign each time a random choice (to contain the answer)
    and i've done that, and can trace that
    'rightChoiceBox' = _choice(random num)
    and i can trace the answer itself
    but what i've commented at the end won't work
    I get the error

    Scene 1, Layer 'Layer 1', Frame 5, Line 22 1119: Access of possibly undefined property text through a reference with static type String

    Code:
    var answer:String = new String();
    answer = String(((sum1)+(sum2)));
    var answerString:String = new String();
    answerString = answer;
    
    var selectRightChoiceBox:String = new String();
    selectRightChoiceBox = String(Math.floor(Math.random()*4)+1);
    
    var rightChoiceBox:String = new String();
    rightChoiceBox =("_choice"+ selectRightChoiceBox);
    
    trace(rightChoiceBox);
    trace(answerString);
    
    //rightChoiceBox.text = answerString;
    Ryan :]

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    That's because you declared rightChoiceBox as a String. The .text property you tried to access belongs to a TextField. Just do
    PHP Code:
    rightChoiceBox answerString

  3. #3
    Ryan :D ryanp321's Avatar
    Join Date
    Nov 2007
    Location
    Wallasey
    Posts
    292
    Ah, thanks! I understand that now!
    A small problem, is there anyway to make a random number, not repeat another 'choices' number?

    Code:
    if(rightChoiceBox == "_choice1"){
    	_choice1.text = answerString;
    }
    
    if(rightChoiceBox != "_choice1"){
    	_choice1.text = String(Math.floor(Math.random()*4)+1);
    }
    
    if(rightChoiceBox == "_choice2"){
    	_choice2.text = answerString;
    }
    
    if(rightChoiceBox != "_choice2"){
    	_choice2.text = String(Math.floor(Math.random()*4)+1);
    }
    
    if(rightChoiceBox == "_choice3"){
    	_choice3.text = answerString;
    }
    
    if(rightChoiceBox != "_choice3"){
    	_choice3.text = String(Math.floor(Math.random()*4)+1);
    }
    
    if(rightChoiceBox == "_choice4"){
    	_choice4.text = answerString;
    }
    
    if(rightChoiceBox != "_choice4"){
    	_choice4.text = String(Math.floor(Math.random()*4)+1);
    }
    would something like this possibly work?
    Code:
    while(choice4.text == _choice1 or _choice2 or _choice3){
    	_choice4.text = String(Math.floor(Math.random()*4)+1);
    }
    Sorry for asking different questions in the thread
    Last edited by ryanp321; 09-01-2011 at 12:04 PM.
    Ryan :]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center