13 Replies Latest reply on Jun 28, 2011 2:39 AM by rtmemory168

    I can't get this piece of code to work.

    rickbreda1

      Hello everybody!

       

      I am using AS2 and I am trying to make a simple 'game' for an assignment for school.

       

      I am first trying to make a simplified version of the game, to get some experience before I make the final version.

      I will try to explain this simplified version:

       

      The player has to drag an object in a big square and then release it. The object resamples 20 liter water. In total the player has to drag 100 liter water to the square, so it has to drag and drop the 'water' object 5 times.

       

      I want some text in screen that says: You still need ... liters of water.

       

      The ... is a dynamic text field with the name 'liters'.

      To help myself I made a counter that counts how many times the water is dragged and dropped.

       

      This is the code I placed in the 'water' movieclip:

       

      <code>

      onClipEvent(load)

      {

      var countWater = 0;

      _root.createTextField("watercount",1,500,100,100,40); //this is the counter I was talking about

      _root.watercount.text = countWater;

      var literWater;

       

      }

       

      on(press)

      {

      startDrag(this);

      }

      on(release)

      {

      stopDrag();

       

      if(this._droptarget == "/watertarget"){ //watertarget is the box the waterobject has to be placed in

      this._x = 150;

      this._y = 300;

       

      countWater++;

      _root.watercount.text = countWater;

       

      literWater = (100 - (20 * countWater)); //I thought this should work. For example if countWater is 1, literWater schould be 100 - 20*1 = 80.

      _root.liters.text = literWater;

      } else

      {

      this._x = 150;

      this._y = 300;

      }

      }

      </code>

       

      But this for some reason doesn't work. Before I drop the waterobject in the box, the text displayed is 'lev' and after I drop the waterobject, '0' is displayed, instead of 80.

       

      The counter does actually works well. It counts 0, 1, 2, 3, 4, 5, etc.

      So I figured there is probably something wrong in one of these 2 lines:

      literWater = (100 - (20 * countWater));

      _root.liters.text = literWater;

       

      Could someone please help me out with this? I would really appreciate it!

      By the way, I'm quitte new to AS, so it could be some newby mistake.

       

      Message was edited by: rickbreda1

        • 1. Re: I can't get this piece of code to work.
          Ned Murphy Adobe Community Professional & MVP

          Learn how to make use of the trace() function to see what values you are encountering as you go.  For example, if you are suspscious of the line....

           

          literWater = (100 - (20 * countWater));

           

          then just after that put:  trace(literWater);

           

          so that yoiu can see if it reflects a value you expect it should.  Do the same for any variables that you think might be related to not giving the expected results.

          1 person found this helpful
          • 2. Re: I can't get this piece of code to work.
            rickbreda1 Level 1

            thanks for your reply!

             

            The output given is:

            80

            60

            40

            20

            0

            -20

            -40

            etc.

             

            So the mathematics are fine! The var literWater contains the right number, but the line

            _root.liters.text = literWater;

            seems to always be printing '0'. Has this got something to do with the variable literWater containing a number, and the textfield needing to print a string?

             

            I fixed the 'lev' problem by putting the same code in the onClipEvent(load), but now it is always printing '100'

            I am really stumped by this!

             

             

            And another question. What tags should I use in here to paste code?

            • 3. Re: I can't get this piece of code to work.
              Ned Murphy Adobe Community Professional & MVP

              I would be most suspscious of your liters textfield not being named properly.  If I recreate what would support your code it works fine for me.

               

              Why don't you try creating it the same way do the watercount textfield just to see if it is working correctly.  I've been assuming there is only one water movieclip that you use over and over again.  If not, then you should get take the on(clipEvent) code off of it and just put that code in the timeline, less the on(clipEvent) part of it.

               

              Actually, placing any code on objects is not a good practice.  You should assign instance names to all your objects and then place the code in the timeline, using the instance names to target them.  If you give your "water" movieclip that name, your code in the timeline would end up looking like...

               

              var countWater = 0;

              _root.createTextField("watercount",1,500,100,100,40);

              _root.watercount.text = countWater;

              var literWater;

               

              water.onPress = function(){

                  startDrag(this);

              }

               

              water.onRelease = function(){

                 stopDrag();

               

                 if(this._droptarget == "/watertarget"){

                      countWater++;

                      _root.watercount.text = countWater;

               

                     literWater = (100 - (20 * countWater));_root.liters.text = literWater;

                 }

               

                 this._x = 150; // no need to have an else if it only does part of what the if does

                 this._y = 300;

              }

               

              As far as placing code into posts goes,  I usually just paste it (Ctrl-v) and then do what I can to make it look properly indented.  Others will use the >> tool offered in the editing interface to enter an area for code.  As far as having tags for code, I don't think that is supported any longer.

              1 person found this helpful
              • 4. Re: I can't get this piece of code to work.
                rickbreda1 Level 1

                the instance name of that textfield was 'liters' with the same spelling, and totally in small cases. So it should have been right.

                 

                For placing the code on my moving clip, I got teached to do that in a couple of tutorials. Having all the code on the same place like you said is actually a better idea!

                 

                Your idea worked: creating a textfield called 'liters' in the code instead of in the design. But I don't know why, because they were both textfields and both had the instance name 'liters'.

                 

                But when doing this in the code instead of in design, I do not have the font I want to use. Is there a way I could change this in the code too?

                 

                 

                edit:

                 

                I figured the font things out a bit, I figured I had to insert this piece of code:

                 

                liters.autoSize = true;

                liters.embedFonts = true;

                litersFormat = new TextFormat();

                litersFormat.font = "font";

                litersFormat.size = 30;

                liters.setTextFormat(litersFormat);

                 

                I already have a font embedded that is called 'font' with the options Export for ActionScript and Export in frame 1. But now there isn't any text displayed. I guess there is something wrong with the above piece of code, because when I put /* and */ around it, it works again.

                • 5. Re: I can't get this piece of code to work.
                  Ned Murphy Adobe Community Professional & MVP

                  That's a problem with alot of tutorials and actually even with some school classroom's... people don't know better and they pass along the bad habits they learned.  When you have your code in the timeline you don't have to go searching or it.

                   

                  If you could show a screenshot of the properties panel for the liters textfield you had, then it can be confirmed whether or not it has been named properly.

                  • 7. Re: I can't get this piece of code to work.
                    Ned Murphy Adobe Community Professional & MVP

                    I still work with an older version of Flash, so I'll have to assume that's the correct place to put the liters instance name.  The one thing you might want to change is the multiline setting that shows near the bottom.  You don't happen to animate things such that there are more keyframes with that textfield earlier in the timeline, do you?  If so, you'll want to assign that name in every keyframe.

                     

                    As for the dynamic version of the textfield, I haven't dealt much with using a library font, so I'd have to spend time seeing if I can get that working.  So what you might try instead of waiting to see if i do, is to start a new posting specifically for that issue and you are very likely to get kglad or someone who will readily know what you need to do.

                    • 8. Re: I can't get this piece of code to work.
                      kglad Adobe Community Professional & MVP

                      (he has to make sure he's not using a tlf textfield somewhere in his project.  the easiest way to ensure that is to change the publish settings to use fp 9.)

                      • 9. Re: I can't get this piece of code to work.
                        Ned Murphy Adobe Community Professional & MVP

                        Keith, just curious... can a tlf textfield be used in AS2?  I have yet to meet the creature myself, but it would make me wonder how something brand new to Flash could be backward compatible with older versions.  If it isn't, then there should be no option to implement one.

                        • 10. Re: I can't get this piece of code to work.
                          kglad Adobe Community Professional & MVP

                          oops, you're correct.  you must use as3 AND fp10+ to use tlf text so tlf text can't be the problem.

                          • 11. Re: I can't get this piece of code to work.
                            rickbreda1 Level 1

                            Thanks for your help Ned, you really helped me out!

                             

                            I will put this question as answered and open a new discussion for the text, as said.

                            • 13. Re: I can't get this piece of code to work.
                              rtmemory168

                              Passing, learning to learn