28 Replies Latest reply on Mar 28, 2007 6:50 PM by Charles Parcell

    Change an Image for another

    Joaquin_Rod
      Hello,

      I have an XML database which contains 40 elements, I would like to put an image on the screen of Macromedia Director if the value of each element is 0 or another if its 1. Thanks for your help
        • 1. Re: Change an Image for another
          Joaquin_Rod Level 1
          I mean I would like to have an image and then when you click on it change it for another image and change the value in an XML document or text file or something like that...

          Thanks
          • 2. Re: Change an Image for another
            Charles Parcell Level 1
            This can be done, but perhaps not as simply as you might think.

            First off, XML was originally designed to be a read only data format. To that end most all implementations of XML sockets and components are simply read only (note I didn't say all).

            My first question would be, do you require that your file be XML or even text?

            If it has to be XML then you could read in an XML doc, parse it, and then rewrite the file out once changes have been made. I would stay away from writing out the file with every little change. If you need to do that, then I would like at a different mechanism to save out through.

            Writing out to a text file is a little easier but requires that you format the data in some fashion.

            Perhaps if you gave a little more detail as to what your requirements are I could point you in a more concrete direction.
            • 3. Re: Change an Image for another
              Joaquin_Rod Level 1
              I am simulating a cinema, I would like to show in the screen 40 seats(free seats), and then select one by clicking on it. When you click the image of the seat, this should be changed by an occupied seat. And I do would not mind if I use a text file to do this or any other kind of file. Thank you for your help,

              Best Regards,

              Joaquin Rodriguez
              • 4. Re: Change an Image for another
                Charles Parcell Level 1
                I am missing the part where you need to have the external file as a requirement. I am only harping on it because reading data and saving it out to an external file does increase the complexity of a design.

                For example, if I were to read between the lines of your issue, I would think that each seat in your project would refer to a different movie to be played or information displayed. I would also assume that the 40 potential movies would be changed daily or even more often. In this case I can understand the need to read from an XML, but I am still failing to understand the need to write back out to the XML. If the 40 items would nearly never change, then what is the point of an external file?

                Something else I failed to ask is, are you developing for Shockwave or Projector?
                • 5. Re: Change an Image for another
                  Joaquin_Rod Level 1
                  Well I don´t need necesarily use an external file. But I would like to have an option to restart the whole application and put the forty seats free again. I thought the best option was to save it in a text file, and from there restarting manually by changing the values, but if you have another idea it would be great... Another thing I would like to ask is how can I write something from a text file in a node of an XML document, I have tried with writescreen and puting it in variables and make them equal, but it doesn´t work, this is for the clients database, and in this point I am forced to do it with XML.
                  This all is for my Master Thesis, many thanks for your help.

                  Best Regards,

                  Joaquin Rodríguez
                  • 6. Re: Change an Image for another
                    Charles Parcell Level 1
                    Here is perhaps the simplest way to "save out" any state variables you might want.

                    Check out setPref() and getPref()

                    If you track your 40 seats via a list you can then save out that list in a pref and reload it during another session.

                    I will make the XML question a separate response.
                    • 7. Re: Change an Image for another
                      Charles Parcell Level 1
                      Actually let me follow up on my own post for a moment.

                      Let's say you track your 40 seats within a list.

                      mySeats = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                      This indicates none of the seats have been "sat in". As they are selected you can just set the value in the list to 1.

                      mySeats[5] = 1

                      Then if you want to reset the seats just...

                      mySeats = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
                      • 8. Re: Change an Image for another
                        Joaquin_Rod Level 1
                        OK, but now how can I change the image? when you push on a seat it seems easy to change the calue, from 0 to 1, but to put one image if it´s 0 and other if it is 1?
                        • 9. Re: Change an Image for another
                          Charles Parcell Level 1
                          I am a bit lost with your reference to writescreen. Could you explain.

                          Further more, are you talking about saving the XML file or are you talking about adding text to the XML object in memory?

                          There is no method to save individual nodes to an XML file. What you would have to do, is read in the whole XML file, reconstruct it with your altered data, and then save out the whole XML file, overwriting the original. Not a very simple task.

                          What is your clients database that requires XML? Perhaps we can conceive an alternate solution to save the data.
                          • 10. Re: Change an Image for another
                            Charles Parcell Level 1
                            To change images you can just construct the list a little differently. For simplicity let's say you have 5 seats.

                            mySeats = [[0,"StarWars"],[0,"BattleStar"],[0,"StarTrek"],[0,"StarFighter"],[0,"SpaceBalls"]]

                            In this list the string names are the names of the images in the cast.

                            Now, if you select a seat. Let's assume that the image on the movie theater screen is sprite(3)

                            -- sets the seat to have been sat in
                            mySeats[2][1] = 1

                            -- sets the displayed movie image
                            sprite(3).member = mySeats[2][2]
                            • 11. Re: Change an Image for another
                              Joaquin_Rod Level 1
                              I wanted to say WriteString(), but now I know that is not possible.
                              I would prefer to do the first thing you told me, read the whole XML, change, and then overwrite. That would be great, because I am suppossed to use XML in that part, but if you know a much better solution, maybe I could use it.
                              Many thanks in advance for your help
                              • 12. Re: Change an Image for another
                                Joaquin_Rod Level 1
                                Respect to the images, could I do it all together in one script? But I don't know how to put the images in a good order to be shown properly...
                                • 13. Re: Change an Image for another
                                  Charles Parcell Level 1
                                  XML
                                  --------
                                  What is the database that you are interacting with? This is really key.

                                  Steps to alter XML
                                  1) Read in the XML files (use FileIO)
                                  2) Save the data as a text member
                                  3) Parse the text using XMLParser
                                  4) Build a search function to find the location in the text member to replace
                                  5) Replace the text
                                  6) Write out the XML file (use FileIO)

                                  It is really messy.

                                  This is why I suspect that you must have an alternate possible solution available to you.

                                  If you are communicating to a database server of some sort, then I bet it is sending you XML based on the function call provided to it. There are other ways to communicate to a database which are MUCH simpler than this.
                                  • 14. Re: Change an Image for another
                                    Joaquin_Rod Level 1
                                    Well, I supossed that I could use the function with childs, that is very simple to search the fields that I want, but if I have to that to use XML, maybe it is better to do it simplier. What did you think? Using Excel or sth like that? Please let me know.
                                    Regarding to the images I think I could solve it, the only thing I need is to define a global varible that doesn't change when the program finish. To know which seats where occupied when we restart the aplication.
                                    Thank you
                                    • 15. Re: Change an Image for another
                                      Charles Parcell Level 1
                                      Image
                                      ----------
                                      Sure you can do it all in one script which would be attached to all the chairs.

                                      Something like...

                                      [untested code]
                                      • 16. Re: Change an Image for another
                                        Charles Parcell Level 1
                                        To save variables use getPref() and setPref() as I stated before.

                                        Example:

                                        setPref("saveData", seats)

                                        Then...

                                        global seats
                                        on startMovie
                                        seats = getPref("saveData")
                                        end
                                        • 17. Re: Change an Image for another
                                          Joaquin_Rod Level 1
                                          I got an error with setPref, it says Script Error: String Expected
                                          setPref("saveData")
                                          [["0","free_seat"] ...
                                          It seems that I cannot save that kind of variable
                                          • 18. Re: Change an Image for another
                                            Charles Parcell Level 1
                                            Try...

                                            saveData=string(saveData)
                                            setPref("saveData", seats)

                                            then to load it...

                                            seats = getPref("saveData")
                                            seats = value(seats)
                                            • 19. Re: Change an Image for another
                                              Joaquin_Rod Level 1
                                              I get the same error. The code used was:
                                              global seats
                                              on startMovie
                                              seats = getPref("saveData")
                                              seats = value(seats)

                                              end

                                              --Frame Script
                                              on beginSprite me
                                              -- This initializes the seats list
                                              seats = []
                                              seatNum = 40
                                              repeat with i=1 to seatNum
                                              seats = [0,"free_seat"]
                                              end repeat
                                              saveData=string(saveData)
                                              setPref("saveData", seats)

                                              end

                                              Which is the alternative that you thought to save the data instead of XML?

                                              Thanks,

                                              Joaquin
                                              • 20. Re: Change an Image for another
                                                Charles Parcell Level 1
                                                See and this is why I should just add "Code untested" in my sig.

                                                Here is the proper Lingo.
                                                • 21. Re: Change an Image for another
                                                  Joaquin_Rod Level 1
                                                  Now it works perfectly. Thank you very much. My thesis is goingo on very quickly because of you. Now my only problem is with the database, as you told me it is a bit complicated to continue it with XML, do you think is better to do it with another code or program? Which one should be easier to manage it?
                                                  I would like to put you in my gratitudes of the thesis if you don´t mind, because thanks to the help of this forum I think I will be able to finish the whole implementation on time. Thanks again.
                                                  • 22. Re: Change an Image for another
                                                    Charles Parcell Level 1
                                                    One addition to my previous code. Just to make a complete implementation...

                                                    • 23. Re: Change an Image for another
                                                      Charles Parcell Level 1
                                                      What is the purpose of the database? Are you going to be using the data within to generate some sort of metric from a different application? Or was this to simply maintain the users current progress? If it is the latter then we have already succeeded in this via the getPref() and setPref().
                                                      • 24. Re: Change an Image for another
                                                        Joaquin_Rod Level 1
                                                        The purpose is, First I read a card which generate a text file with an ID, then I search in the database for that ID and I get some values of the fields I need. Here I can succeed with the database made in XML. The problem is when I have to modify one field, for example the movie that the user select is writed in a text file, here I should get the text form the file and put it in the corresponding field of the user (Knowing the ID), here, as you told me, I cannot use XML. So I think I should change the format of the database (maybe Execel or Access), but I don´t think I could do it with setPref and GetPref, because it is a bit bigger, 5 or 6 fields and "no limit" of clients. Or it is not that complicated?
                                                        Thanks
                                                        • 25. Re: Change an Image for another
                                                          Charles Parcell Level 1
                                                          If you want to play with Access then check out this Xtra.
                                                          http://www.xtramania.com/Products/ADOxtra/

                                                          If you want a more versatile external file format for use with Director then i would check out either of these Xtras.
                                                          http://xtras.tabuleiro.com/products/vlist/index.tdb
                                                          http://xtras.tabuleiro.com/products/arca/index.tdb

                                                          In any case there is no out of the box way for Director to talk with any database or external file other than a plane text file.
                                                          • 26. Re: Change an Image for another
                                                            Joaquin_Rod Level 1
                                                            Finally, I have to do it with XML mandatory. The thing is, In a movie Script, at the begining I do:
                                                            on parseXML aFilename

                                                            fio = xtra("FileIO").new()
                                                            -- check instance
                                                            fio.openFile(aFilename, 1)
                                                            -- check for error
                                                            tString = fio.readFile()
                                                            -- check for error
                                                            fio.closeFile()
                                                            fio = VOID

                                                            parser = xtra("XMLParser").new()
                                                            tResult = parser.parseString(tString)
                                                            if voidP(tResult) then
                                                            tList = parser.makePropList()
                                                            else
                                                            -- error with parsing
                                                            tList = [parser.getError()]
                                                            end if
                                                            parser = VOID

                                                            return tList

                                                            end

                                                            Initializating the XMLParser, now in a Sprite Script, I do
                                                            lParsedXML = parseXML("D:\Joaquin\database.xml")
                                                            And then I need to put something, first in this field for example:
                                                            lParsedXML.child[1].child[1].chardata = "string"
                                                            But the above sentence does not work, as you told me because I cannot do it directly, How can I Parse the XML to a plain text, write exactly in this field and Reverse the Parse again? I think here is the messy thing, but doing it once, it should be simple manage with the whole fields...
                                                            • 27. Re: Change an Image for another
                                                              Joaquin_Rod Level 1
                                                              Hello,
                                                              I am trying to save varibles from one Sprite to another, but I don´t know why, I cannot write in a list, can anybody see my mistake, this is the code:

                                                              global ID
                                                              global Nam

                                                              on startMovie
                                                              ID = getPref("IDs")
                                                              ID = value(ID)
                                                              Nam = getPref("Nams")
                                                              Nam = value("Nams")
                                                              end
                                                              on mouseUp me
                                                              if ID = void then
                                                              ID = [#1:"id=0", #2:"id=0", #3:"id=0",#4:"id=0",#5:"id=0"]
                                                              end if
                                                              if Nam = void then
                                                              Nam = [#1:"-", #2:"-", #3:"-",#4:"-",#5:"-"]
                                                              init=getProp(ID, #1)
                                                              if(init = "id=0") then
                                                              pos=1
                                                              ID.addProp(#1,objFileio.readFile()) --Here nothing is saved
                                                              Nam.addProp(#1,member("entry-Name").text) --Nothing Saved
                                                              end if
                                                              IDs = string(ID)
                                                              setPref("IDs", IDs)
                                                              Nams = string(Nam)
                                                              setPref("Nams", Nams)
                                                              • 28. Re: Change an Image for another
                                                                Charles Parcell Level 1
                                                                I think line 7 should be....

                                                                Nam = value(Nam)

                                                                Notice no quotes and the variable name change.

                                                                What are you trying to do in the following code?

                                                                ID.addProp(#1,objFileio.readFile()) --Here nothing is saved
                                                                Nam.addProp(#1,member("entry-Name").text) --Nothing Saved