A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [CS3]Getting String Values

  1. #1
    Member
    Join Date
    Aug 2008
    Posts
    56

    [CS3]Getting String Values

    I have been trying to get the answer to this forever! I have a string with 5 variables- num1, num2, etc. Now i want a MovieClip to know what each one's values is. I.E. num1 = 3 so the third MovieClip would play instead of the others
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  2. #2
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    Hi Blues Tribute,

    I could use a bit more detail about what you're trying to do, but my gut tells me that you are in need of an array. Are you trying to sequence a number of clips to play in a specific order?

    You may also want to try a split command. Here is an example:
    PHP Code:
    var stringy:String "57, 22, 13, 26";
    var 
    stringArr:Array = stringy.split(","); 
    If I now trace out stringArr[2], it will come up as 13.

    Regards,

    Plasnid
    A little pain never hurt anyone

  3. #3
    Member
    Join Date
    Aug 2008
    Posts
    56
    Finally, I thin k I can finish this game cause someone decided to help! i need 5 variables to have different, random numbers 1-5. Than, when you click one of the buttons, if the variable has a certain value, it will play it's codes, but i dont know:

    How to get each variable a random number
    How to check (well, I think I can check if they are variables)
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  4. #4
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    Hi Bluestribute,

    Okay, here is a start. This will give you a random number between 1 and 5.
    var randNum:Number =1+ Math.floor(Math.random()*5);

    as for how to check the value, you can set a value to a movieclip. Which means you can then check that value.

    example(my movieclip is named zaphod):
    PHP Code:
    var zaphod:MovieClip = new MovieClip();
    zaphod.numVal 1Math.floor(Math.random()*5);

    /*if I want to see what the numVal is of zaphod*/
    trace(zaphod.numVal);
    zaphod.addEventListener(MouseEvent.MOUSE_UP,checkNum);

    function 
    checkNum(e:MouseEvent):void{
         
    trace(e.target.numVal);
         if(
    e.target.numVal==5){
              
    trace("mongo gets candy!");
         }else{
              
    trace("mongo gets carrot");
         }

    I do hope this helps

    Regards,

    Plasnid
    A little pain never hurt anyone

  5. #5
    Member
    Join Date
    Aug 2008
    Posts
    56
    That would be it, but is there a way for all 5 variables to have a different value?
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  6. #6
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    I don't know the specifics of your code, but its a very easy thing to set up a loop to run five times. Below is an example of an array that has five random numbers between 1 and 5.

    PHP Code:
    var mongoArray:Array = new Array();
    for(var 
    i:int=0;i<5;i++){
         var 
    randNum:Number 1Math.floor(Math.random()*5);
         
    mongoArray.push(randNum); 

    Regards,

    Plasnid
    A little pain never hurt anyone

  7. #7
    Member
    Join Date
    Aug 2008
    Posts
    56
    Quote Originally Posted by plasnid
    I don't know the specifics of your code, but its a very easy thing to set up a loop to run five times. Below is an example of an array that has five random numbers between 1 and 5.

    PHP Code:
    var mongoArray:Array = new Array();
    for(var 
    i:int=0;i<5;i++){
         var 
    randNum:Number 1Math.floor(Math.random()*5);
         
    mongoArray.push(randNum); 

    Regards,

    Plasnid
    Would that give each variable a different number?
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  8. #8
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    okay, lets get a better idea of what you're looking for. Do I take it correctly that you are looking for a non repeating random number? 1 instance of 1, 1 instance of 2 etc?

    Here is a strategy you could use. Create an array that names all your clips. Create a second array that will hold a reference to your clips. pick a random entry from your clip array and push it into your second array. Then remove the clip reference from your first array. If you want to add the random position into your clip, add the value of secondArray.length to your movie clip when you add it to the second array.
    A little pain never hurt anyone

  9. #9
    Member
    Join Date
    Aug 2008
    Posts
    56
    Quote Originally Posted by plasnid
    okay, lets get a better idea of what you're looking for. Do I take it correctly that you are looking for a non repeating random number? 1 instance of 1, 1 instance of 2 etc?

    Here is a strategy you could use. Create an array that names all your clips. Create a second array that will hold a reference to your clips. pick a random entry from your clip array and push it into your second array. Then remove the clip reference from your first array. If you want to add the random position into your clip, add the value of secondArray.length to your movie clip when you add it to the second array.
    How would you code this? I'm not familiar wit programming arrays (though I do kinda understand).
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  10. #10
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    How much do you know about arrays? Do you know how to create, add elements to and remove elements from an array?
    A little pain never hurt anyone

  11. #11
    Member
    Join Date
    Aug 2008
    Posts
    56
    Quote Originally Posted by plasnid
    How much do you know about arrays? Do you know how to create, add elements to and remove elements from an array?
    No… haven't had to use them yet… though I saw a tutorial on Newgrounds that was arrays. Haven't seen it either though… though I do know about them and how they store stuff
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  12. #12
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    Arrays are critical to programming. Before you continue on your current project, you need to get familiar with them. Here are a few things you need to look up and get very very comfortable with, as they will make your life so much easier. Start with a glance at the flash help on the following:

    Array
    Array.push
    Array.pop
    Array.sortOn

    Object

    While I could just write up the code for what you are talking about, it is more important for you to understand the concepts so you an re-apply them to what you're doing.

    Follow this tutorial before you continue in your project, and you will have a better understanding of how to do what you're after.

    http://www.actionscript.org/resource...ipt/Page1.html
    A little pain never hurt anyone

  13. #13
    Member
    Join Date
    Aug 2008
    Posts
    56
    Quote Originally Posted by plasnid
    Arrays are critical to programming. Before you continue on your current project, you need to get familiar with them. Here are a few things you need to look up and get very very comfortable with, as they will make your life so much easier. Start with a glance at the flash help on the following:

    Array
    Array.push
    Array.pop
    Array.sortOn

    Object

    While I could just write up the code for what you are talking about, it is more important for you to understand the concepts so you an re-apply them to what you're doing.

    Follow this tutorial before you continue in your project, and you will have a better understanding of how to do what you're after.

    http://www.actionscript.org/resource...ipt/Page1.html
    Thanks. I would rather understand it than copy and paste it. It's loading as I type!
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  14. #14
    Member
    Join Date
    Aug 2008
    Posts
    56
    That's… simple. I think I'm gonna try this with my game I am making in my spare time! But the random values…
    Scratch- just like Flash without "Syntax Error" (yeah, that's me on the front page. I mean, was me)

  15. #15
    Space Midget Wrangler
    Join Date
    Sep 2002
    Posts
    129
    Look at the info on arrays, and look at what I have posted previously. Take a bit of time to explore how this works. Its all here.
    A little pain never hurt anyone

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