A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Switch

  1. #1
    Antidisestablishmentarianismst
    Join Date
    Oct 2001
    Location
    Columbus, Ohio
    Posts
    18

    Switch

    I have a simple switch string which works like an if, then. The only problem is that I want the case to be more than one thing. how can I get something to work like this:

    on (release, keyPress "<Enter>") {
    switch (input) {
    case "hello" or "hi" :
    tellTarget ("dialogbox") {
    gotoAndStop(2);
    dialogue = "Well hello there.";
    }
    break;

    it works with hello, or just hi, but not both together. I don't want to make seperate cases for each if I don't have to. Also, if the input == "go to mars", can I just get the case to read the "go to" part? This would be extrememly helpful. The user could then input "go to ANYTHING" and it would say the same message as if they put a different word in place of anything. There has to be some wildcard. Thanks in advance.
    -doctor soco.
    chicka chicka...

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Code:
    // put this prototype in the first frame of the movie
    // you only need to put it there and only once
    String.prototype.isOR = function(args){
    	var i, l = arguments.length; // temporary vars
    	for (i=0; i<l; i++) if (this == arguments[i]) return this; // if this string in args array return the string
    	return !this; // if string not return not this
    }
    
    
    // here is a switch example.  two str's are given (one is
    // commented out to try in a seperate test).  The first 
    // case uses the prototype made from the code above, the 
    // second uses a string method called indexOf to see if the
    // string contains the text given.  Go ahead and test ;)
    
    str = "hi"; // <-- use this one first
    // str = "go to Mars"; // <-- try again with this one
    
    switch (str) {
    	case String(str.isOR("hi","hello")):
    		trace ("string is 'hi' or 'hello'");
    		break;
    	case (str.indexOf("go to") != -1) ? str : !str:
    		trace ("string contains 'go to'");
    		break;
    	default:
    		trace ("no case tested true");
    }
    // end test

  3. #3
    Welcome to flavour country
    Join Date
    Sep 2000
    Posts
    662
    You can also do it this way:

    Code:
    on (release, keyPress "<Enter>") {
    switch (input) {
    case "hello":
    case "hi":
    dialogbox.gotoAndStop(2);
    dialogbox.dialogue = "Well hello there.";
    break;
    }
    }

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