14 Replies Latest reply on Jun 22, 2007 2:55 AM by ajayscript

    Setting Random Positon(X,Y) of a object through XML

    ajayscript Level 1
      Hi , Guys

      I am facing a problem : problem is that i have a xml file through which i am getting the value of an object name , path , X , Y position and set it on the screen . But the requirement is that every time when page is loaded position of the object should be changed . Like if the position of an object A is X=20 and Y=30 , first time on load of the page , then second time when page is loaded the position of the object A s hould be different or changed like X=40 , Y=50; This is the problem , i have a XMl file given below through wich i get the value of X, Y of an object.

      So please suggest me what I have to do ?
      Either i have to change in XMl file or have any script for random position of X, y after getting that value from XMl????

      Please Help me out.



      <?xml version="1.0" encoding="utf-8"?>
      <items>

      <item id="1" nme="object1" path="images/a.png" price="250" xpos="20" ypos="300"/>
      <item id="2" nme="object1" path="images/b.png" price="320" xpos="80" ypos="300"/>
      <item id="3" nme="object1" path="images/c.png" price="190" xpos="140" ypos="300"/>
      <item id="4" nme="object1" path="images/d.png" price="120" xpos="200" ypos="300"/>
      <item id="5" nme="object1" path="images/e.png" price="110" xpos="260" ypos="300"/>
      <item id="6" nme="object1" path="images/a.png" price="900" xpos="320" ypos="300"/>
      <item id="7" nme="object1" path="images/c.png" price="250" xpos="380" ypos="300"/>
      <item id="8" nme="object1" path="images/d.png" price="310" xpos="440" ypos="300"/>
      <item id="9" nme="object1" path="images/e.png" price="190" xpos="500" ypos="300"/>
      <item id="10" nme="object1" path="images/c.png" price="120" xpos="560" ypos="300"/>
      <item id="11" nme="object1" path="images/e.png" price="110" xpos="620" ypos="300"/>
      <item id="12" nme="object1" path="images/a.png" price="900" xpos="680" ypos="300"/>

      </items>
        • 1. Re: Setting Random Positon(X,Y) of a object through XML
          Greg Dove Level 4
          Your positions appear to be evenly spaced on x at 60 pixel intervals. If that's always going to be the case then don't do it in the xml. Its pretty easy to do a random placement from actionscript for that.
          • 2. Re: Setting Random Positon(X,Y) of a object through XML
            ajayscript Level 1
            No i want random positions of x ,y , its may be 20 or 60 or 43 or 23 anything for a single object and respectively for all , so please help me out for this problem or give me ny solutions
            • 3. Re: Setting Random Positon(X,Y) of a object through XML
              kodex2
              you should get your XML file length. after that you should set a random number between 0 & xml.length. then set your movie to set your object position with xpos and ypos attributes. if you have any question mail me. " mehran.hadidi@hotmail.com ". l3ye
              • 4. Re: Setting Random Positon(X,Y) of a object through XML
                Greg Dove Level 4
                Ok.. you should do it from actionscript. You don't need xpos and ypos attributes in the xml.
                Can you post your existing code where you are currently "getting the value of an object name , path , X , Y position and set it on the screen" . It will be easier to advise you from your existing code.

                And you should think about these aspects/requirements - please give quick answers:
                1. Is it important that the random positions selected avoid the images overlapping eachother?
                2. What is the width and height of each image... are they the same or do they differ?
                3. If the images differ in size do you need to scale them to be the same size on-screen or do they need to be displayed at their original size.?
                4.If answer to (1) is yes... what do want to have happen in any potential situation where it might be impossible to find sufficient space to place further images (e.g. list is too long, or image size too big with inefficient layout from true random placement) (This is a problem that might be avoided by using random known locations e.g a grid or table layout - but that's not what you asked for).

                • 5. Re: Setting Random Positon(X,Y) of a object through XML
                  ajayscript Level 1
                  Ok fine , what i will do is posting my code u just go through it. and please suggest me. In the code xps is the x position of object and yps is y position of an object.

                  And the answer of ur first question is that there might be images of different size but we have to scale them in a similar size and then show it on the screen randomly so there should'nt overlaping occured. please review the code how i can get path, name , xpositoon and yoisition from a xml file and then show it on the screen , its loks little static rather i want more dynamic so please suggest me on these topics ASAP , its hectic for me . Please help me out Thanks
                  • 6. Re: Setting Random Positon(X,Y) of a object through XML
                    ajayscript Level 1
                    can u plesae describe me thorrowly if u can ?? because i have lots of confusion regarding ur answer.

                    Please
                    • 7. Re: Setting Random Positon(X,Y) of a object through XML
                      Greg Dove Level 4
                      You seem to have understood well the first few points - because you gave answers to them.

                      To explain more thoroughly my last point...as a concept its quite simple: Imagine your images were exactly sized to fit on the screen in a 2 X 2 grid... so each image was the same size and there were 2 across, 2 down - completely filling your screen. You can now randomly place 4 images in each of the set locations... a grid or table (in this case a 2 x 2 grid). If you did that then they would not overlap and you can fit 4 images on screen. But if I randomly place one image and it doesn't go completely in one of the grid positions... then it will limit the number of images that you can place - you will never be able to place 3 more (without scaling down). After 1 image it might already be impossible to place any more in this example. The same principle applies in a smaller space, with larger numbers of smaller images where you don't constrain to a grid layout - and you don't permit overlapping.

                      In terms of solving it... well I'm not too sure... I've not had to deal with it myself before. Depending on the number of images, their size and the space have to put them in... sometimes it might be OK and other times not.
                      If you want all the images to be displayed then (I think) you need to make sure the size of the images is such that you will always be able to place the number you want even with the most inefficient placement possible (even if its not very probable from random placement). In the example of a 2 by 2 grid I believe the most inefficient placement of one image would be in the centre. But by using this as a reference and reducing the size of the image until you get to a size where others can be placed around it then you should be able to fit 4 on-screen again with random placement. I'm not an expert at this stuff (not at all). I guess I'm trying to understand it a little better myself...and probably over-complicating it in the process. I've always done random placement to grid locations in the past. Anyhow perhaps others can advise whether some extension of this approach can be used to determine image size for a given number of images and a given space to allow true random placement of all the images under all circumstances.

                      Also looking at your code I see what you mean about it being static and understand why you want to change it.

                      You should make more use of loops and dynamic naming of the clips you load into.
                      I also suggest using the MovieClipLoader class and the onLoadInit handler for each clip. This will be the best place/time to resize the images if they are different sizes as you say.

                      I would like to be more specific, but I think you need to start with that and get things working that way first. And in terms of the random placement - assuming you have a number and size of images that will always fit in the space when allocated randomly... then I can think of the easy way (which is not very efficient and could get stuck working on placement for some time if the spacing was tight)
                      1.
                      a. choose random x and y coords and check if it is occupied (e.g. hitTest all previously loaded images). If yes go back to start of (a) .
                      b. check the random x coord+image width, y+ image height... is it occupied (same again) ? if yes go back to (a)
                      c. place image there.

                      To do it more efficiently perhaps someone else has some existing code to track placements and limit the selection of the random points to the space that is not currently occupied.
                      • 8. Re: Setting Random Positon(X,Y) of a object through XML
                        ajayscript Level 1
                        that means i have to devide my stage in grids and then place objects according to avaibility of positions right?

                        dont u think it makes heavy line of codes??? any other way we can optimize things ???
                        • 9. Re: Setting Random Positon(X,Y) of a object through XML
                          Greg Dove Level 4
                          No I wasn't saying you had to place in a grid... I was just saying that's a lot easier (and for me at least - more familiar). What is the size of the images (width x height ) -after they've been resized if necessary - and the size of the space (width x height ) that you'll be placing them in?
                          And what is the maximum number of these images you need on screen?
                          • 10. Re: Setting Random Positon(X,Y) of a object through XML
                            ajayscript Level 1
                            its might be above than 20 and the size of the images width=80 and height=120 .
                            • 11. Re: Setting Random Positon(X,Y) of a object through XML
                              ajayscript Level 1
                              i am trying to fix this problem and mya be done by today eve but now i has a problem regarding attahing these movieclips(images) to a label (movieclip) on the stage and when i will move label the all images shoild move with label which attched to that label , i have tried lot through hit test but not working because i have lots of dynamic movieclips(images) and how to attach these movieclips on the label movieclip which is static movieclip in my case do u have any sample or any question regarding this ??? pl help me out .
                              Thnx
                              • 12. Re: Setting Random Positon(X,Y) of a object through XML
                                Greg Dove Level 4
                                Ok... so 20 or more images... each image is 80x120.
                                What is the width and height of the space you want to put them in?
                                • 13. Re: Setting Random Positon(X,Y) of a object through XML
                                  Greg Dove Level 4
                                  Re your last post... have you already solved this problem for random placement? If so... then I suggest you request help on the separae issue in a separate thread - or continue with it in one if you have already started a topic for that. It makes sense for many reasons.
                                  • 14. Re: Setting Random Positon(X,Y) of a object through XML
                                    ajayscript Level 1
                                    ok fine i haveallready posted it in a new thread