Skip to main content
Participant
August 27, 2011
Answered

String - If else not functioning!?

  • August 27, 2011
  • 1 reply
  • 346 views

hi

I've got the following code, I am trying to appendText to the same text Field (instance name = info). Problem is it seems to be ignoring the if statements and just outputs the last else string only.

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.display.Sprite

crabBtn.addEventListener(MouseEvent.CLICK, ldr1);
kidsBtn.addEventListener(MouseEvent.CLICK, ldr5);

// Function to handle mouse event, and the void means its not returning any value.
function ldr1(Event:MouseEvent):void {
crabLdr.ldrCrab.source="./images/crabMov.swf";
// Converting the text fields content into a string which is called myString
var myString:String = info.text;
/* This will show text enclosed in the "", and if there is any other text before it, it will be added to it.*/
if(myString == "Good Morning! We are Andy and Stacy "){
info.appendText("and I am Ralph the crab!");
} else if(myString == "Good Morning! We are Andy and Stacy "){
info.appendText("and I am Ralph the crab!");
} else {
info.appendText("Hello, I am Ralph. Let's have some fun together at the beach. ");
}
}

function ldr5(Event:MouseEvent):void {
kidsLdr.ldrKids.source="./images/KidsMov.swf";
var myString:String = info.text;
if (myString == "Hello, I am Ralph. Let's have some fun together at the beach."){
info.appendText("We will build a big sand castle together.");
} else if (myString == "whatever"){
info.appendText("whatever");
} else {
info.appendText("Good Morning! We are Andy and Stacy ");
}
}


Any tips please as to why its not working please?

Thanks

This topic has been closed for replies.
Correct answer Ned Murphy

The most logical conclusion would be that the strings you are testing for are not what the code finds, so it resorts to the default.  You will need to use the trace command to test whether or not the strings you are testing for are what it is seeing.  One thing to trace for is the length property of the textfield and the string it contains.  Make sure that agrees with the number of characters your conditional strings have...

function ldr1(Event:MouseEvent):void {
   crabLdr.ldrCrab.source="./images/crabMov.swf";
   var myString:String = info.text;

   // all three of the following should agree in length if the test string is in the textfield

   trace(info.length, myString.length);

   trace(String("Good Morning! We are Andy and Stacy ").length);

   if(myString == "Good Morning! We are Andy and Stacy "){
      info.appendText("and I am Ralph the crab!");
   } else if(myString == "Good Morning! We are Andy and Stacy "){
      info.appendText("and I am Ralph the crab!");
   } else {
      info.appendText("Hello, I am Ralph. Let's have some fun together at the beach. ");
   }
}

If the values are not the same, then you might have some other characters being added to the textfield that you don't see.

1 reply

Ned Murphy
Legend
August 27, 2011

Are both sets of conditionals failing?

What is meant by the following comment because it sounds like there should be quotations in the strings...?

/* This will show text enclosed in the "", and if there is any other text before it, it will be added to it.*/

Participant
August 27, 2011

/* This will show text enclosed in the "", and if there is any other text before it, it will be added to it.*/ -  this is just a comment for me basically saying that text will be appended to the text field, added to the string in the text field.

This is the only part that functions, the text "Hello, I am Ralph. Let's have some fun together at the beach. " is added to the text field with instance name info and the other if and else if statements seem to be ignired.

} else {
info.appendText("Hello, I am Ralph. Let's have some fun together at the beach. ");
}

thanks for your reply

Ned Murphy
Ned MurphyCorrect answer
Legend
August 27, 2011

The most logical conclusion would be that the strings you are testing for are not what the code finds, so it resorts to the default.  You will need to use the trace command to test whether or not the strings you are testing for are what it is seeing.  One thing to trace for is the length property of the textfield and the string it contains.  Make sure that agrees with the number of characters your conditional strings have...

function ldr1(Event:MouseEvent):void {
   crabLdr.ldrCrab.source="./images/crabMov.swf";
   var myString:String = info.text;

   // all three of the following should agree in length if the test string is in the textfield

   trace(info.length, myString.length);

   trace(String("Good Morning! We are Andy and Stacy ").length);

   if(myString == "Good Morning! We are Andy and Stacy "){
      info.appendText("and I am Ralph the crab!");
   } else if(myString == "Good Morning! We are Andy and Stacy "){
      info.appendText("and I am Ralph the crab!");
   } else {
      info.appendText("Hello, I am Ralph. Let's have some fun together at the beach. ");
   }
}

If the values are not the same, then you might have some other characters being added to the textfield that you don't see.