A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: From occurrence 1 to 50 of this clip ?

  1. #1
    Junior Member
    Join Date
    Jan 2015
    Posts
    5

    From occurrence 1 to 50 of this clip ?

    Hello,

    I have dynamically spawn 25 clip (AS2) i gave them a position and an occurrence from crate1 to c25

    Now i search a way to write "from crate1. to crate10."
    For exemple : if((crate1"to"crate10)._x=10) {;}
    To replace : if (crate1._x=10)||(crate2._x=10)||(crate3._x=10)... etc{;};

    I need to check this ten occurence for exemple in the same time without having to write all of them (because in my final project i have thousand of occurences)

    Thanks a lot if you can help me, i tryed google but i don't really know how it's called so it's harder to find

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi and welcome

    Are you familiar with for loops? I am pretty sure they are what you're looking for. Basically, using them you're able to repeat a same set of code over and over again with only a number changing -- which is perfect for your situation

    Here is a general description of a for loop:

    Code:
    for(start_value; condition; increment){
        // repeat something
    }
    The start_value specifies which number you want to start with, usually it's either 0 or 1. The condition is something that needs to be fulfilled in order for the for loop to continue the repetition of codes, and lastly increment is the part where you define how you want your number to change for each loop of the code.

    Now, all of that probably didn't make any sense at all, and usually an example works way better, so here you go:

    Let's say you wanted to output all the numbers from 1 to 10. We could use your method by manually typing all the numbers, OR we could simplify the task by using a for loop:

    Code:
    for(i = 1; i <= 10; i++){
    	trace(i);
    }
    To translate that into human language, we create a variable i with the start value of 1. The condition for the loop is that the value of our variable i must be SMALLER THAN OR EQUALS ( <= ) to 10. And lastly, for every loop, we increment our variable i with 1 ( i++ ). So basically, this line of code, trace(i); is executed 10 times with only the variable i changing for each execution.

    On the first loop, i = 1. The program first checks if the condition is met, which is i <= 10, and we can see that it's true since 1 <= 10 which is true. Then the trace(i); line of code is executed, which in our first loop will result in this happening: trace(1);, since i = 1. Then, the variable i is incremented by 1, making i = 2. Then the second loop is initiated with i = 2, and the program checks if the condition is met. Since 2 <= 10, the loop is valid, so trace(i); is executed again, with i = 2, so it's actually trace(2); . Then i is incremented to 3, and the loop goes on, until it reaches i = 10. When trace(10); is done and i is incremented to i = 11, then the condition is no longer true, and the for loop ends.

    Before I present you a solution to your problem, you need to keep in mind that you need to use double equal marks when you want to compare two values. if(crate1._x = 10) is not valid, however this is: if(crate1._x == 10) -- given that you actually mean, "if the x position of crate1 is equals to 10, then ...".

    Another thing you need to know, is that you can dynamically refer to an object such as a movieclip using this:

    this["crate"+1]

    this is the same as crate1, but we combine a string "crate" and a number 1, and using the square brackets, we tell flash to look for an object (e.g. movieclip) with the combination of the strings and numbers.

    So, for your code:

    Code:
    for(i = 1; i <= 10; i++){
    	 if(this["crate"+i]._x == 10){
    		// do something 
    	 }
    }
    However, keep in mind that this is NOT the same as:

    if (crate1._x=10)||(crate2._x=10)||(crate3._x=10)

    Rather, the code we have inside our for loop is repeated 10 times, something like this:

    Code:
    if(this["crate"+1]._x == 10){
    	// do something 
    }
    if(this["crate"+2]._x == 10){
    	// do the same thing
    }
    if(this["crate"+3]._x == 10){
    	// do the same thing
    }
    // .. and so on seven more times to 10
    But I hope you learnt something, and if you need more in-depth understanding of for loops, I suggest you google it -- but I really hope this works and helps you

    ------------------------

    EDIT: Actually, re-evaluating your code, I think this code works better for your situation:

    Code:
    for(i = 1; i <= 10; i++){
    	 if(this["crate"+i]._x == 10){
    		// do something 
    	 	break;
    	 }
    }
    Notice the extra break I added? Well, basically when the first if statement is true (meaning that some crate's x position is equals to 10), then some code will be executed and then break will also be executed, which force-exits from the for loop, since that's what your code, if (crate1._x=10)||(crate2._x=10)||(crate3._x=10), does, right? If any one of those statements is true, only one code is executed, but in our first for loop code, all of the if statement would've run regardless of any previous if statement being true (this is worded sooo badly, but it's hard to explain as well, but I hope you get the gist of it )
    Last edited by Nig 13; 01-12-2015 at 07:14 PM. Reason: Revise code
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Jan 2015
    Posts
    5
    Thanks a lot Nig 13, for your time and all this explanation, it's exactly what i looking for !
    Thanks for the "break" too, beacause my final code gonna be a lot more difficult than this one and break can be usefull !

    I make more trys and give you feedback

  4. #4
    Junior Member
    Join Date
    Jan 2015
    Posts
    5
    I know now how to look for a precise crate
    Code:
    this["crate"+1]
    I know how to look for crates from 1 to 10
    Code:
    for(i = 1; i <= 10; i++){
    	 if(this["crate"+i]._x == 10){
    		// do something 
    	 	break;
    	 }
    }
    Now is this possible to just look if there any crate here ?
    Code:
    Something like :this["crate"+anything]
    I mean, if it's start by "crate" { do something; with no needs to know wich crate it is ?

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi, I'm really glad to hear that I could be of help

    That most certainly is possible, but there are different methods to achieve that. I'll let you know one and see if that helps first.

    Let's say you have crate1 through crate99, and you want ONE random crate of all the crates. What you can do is generate a random number between the first and the last crate, meaning a random number from 1 through 99, and then do something to that crate with the random number. Let's see the code for that:

    Code:
    randomNum = Math.ceil(Math.random()*99);
    Let's go over this code. We make a new variable named randomNum, and we assign it ONE random number from 1 through 99, so it can be any number, but we don't know which one -- it's a random number. Math.random() gives us a random number between 0 and 1, for example 0.0653272485360503 or 0.457883422728628 -- the important thing here is that these act like a percentage. For example can the first random number, if multiplied by a hundred, resemble approx. 6.5%, and the second random number mentioned above, 45.7%. Now, if we multiply this randomly generated number with 99 (or any other number you want the limit to be), then we will get the percent of that number. For example, 6.5% of 99 is 6.435, and 45.7% of 99 is 45.243, which is almost the half (almost 50%). But you can notice the unwanted decimals that we get in the variable, we don't want those, because you obviously don't have a crate named crate45.243 -- that goes without saying, hehe :P To fix that, we can use the function Math.ceil() which rounds up a number upwards to the closest integer, for example will Math.ceil(6.435) become 7, and Math.ceil(45.243) become 46. Therefore, the final code, Math.ceil(Math.random()*99) -- will give us a whole number (integer) from 1 through 99. I hope this made sense

    Now that we have a random number from 1-99, we can use this number and find a random crate. So, to do something to a random crate, you could refer to that random crate like this:

    Code:
    this["crate"+randomNum]
    and do something, for example:

    Code:
    if(this["crate"+randomNum]._x == 10){
        // do something
    }
    Like I mentioned earlier, there is another method as well, but it's seemingly complicated, so I think this one will work best for you and also make more sense.

    I hope this helps as well, if you've anymore questions, feel free to ask
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Junior Member
    Join Date
    Jan 2015
    Posts
    5
    Yes but it's a random crate, just one.

    What i try to do it's EVERY CRATE, like "if its a crate {do something}" without looking what number of crate it is
    a code to just look if the occurence start by "crate" without try to know wich number it is (or a way to make a family or something)

  7. #7
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Weedy, Nig

    Sorry to jump in, was bored.

    As far as I gather they all are of the crate family Weedy unless you have many other clips on the stage.

    if you mean find all clips on stage and figure out which one is a Crate family, then you can try something like this
    PHP Code:
    function getCrates(a):Void
    {
        for (var 
    i in a)
        {
            if (
    a[i] instanceof MovieClip)
            {
                if (
    a[i]._name.indexOf("crate") != -1)
                {
                    
    trace(a[i] + ": x = " a[i]._x " - y = " a[i]._y);
                    
    a[i]._alpha 25;
                }
            }
        }
    }

    getCrates(_root); 
    Basically it looks on the area you ask it to getCrates(_root); _root in this case

    then looks for movieClips, then checks for a name containing "Crate", if so then it does what you tell it to do;
    Last edited by fruitbeard; 01-13-2015 at 02:15 PM. Reason: Typo

  8. #8
    Junior Member
    Join Date
    Jan 2015
    Posts
    5
    Thanks fruitbeard but what i try to do is easyer i think...

    For exemple i want to write : crate.removeMovieClip(this) And it remove crate1, crate2, crate3, crate4... etc

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi fruitbeard, Weedy

    @fruitbeard: That was actually the other method I was mentioning in my other post :P Great coding as always (y)

    @Weedy Wonka: unfortunately, there's no way in Flash, or in any other programming for that matter, that you can just write crate.removeMovieClip(this) that it will work for ALL the crates, crate1, crate2, etc. You'll have to use something like a for loop to go through each crate and remove that crate, sorry :P

    Basically, what fruitbeard's code does, is that it first finds ALL the objects on the stage (Shapes, Movieclips, Buttons, etc.), then it narrows all those elements down to only MovieClips, and then from all the MovieClip on stage it picks out all the Movieclips which have the word "crate" in them. If we look at fruitbeard's code, without the function, it will look like this:

    PHP Code:
    for (var i in this// get all the objects on stage
    {
        if (
    this[i] instanceof MovieClip// select all the movieclips from the objects
        
    {
            if (
    this[i]._name.indexOf("crate") != -1// pick out all movieclips with the word "crate" only
            
    {
                
    // do something to the crates here, like for example:
                
    this[i].removeMovieclip(); // removes all crate movieclips
            
    }
        }

    We have to single out all the crate movieclips like this, there's no simpler way to do it :/ The thing about programming is that you have to do most of the things you want, manually -- the computer can't do everything for you, because it won't understand what you mean if you simply write crate.removeMovieclip(), actually that can be interpreted as removing the movieclip named crate.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

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