18 Replies Latest reply on Mar 1, 2009 10:53 PM by

    [AS2] Making a text based game

    Gamelord2
      I am trying to make a small text based game test so that I can learn the code needed to make an actual one.
      I am working on the battle system right now, and I am pretty new to flash.
      So there's "Enemy HP:" in a static text box in the top right corner, and right below it is a dynamic text box for the enemy's health which starts at 50 for this test. In the top left corner its the same thing, but for Player HP and it starts at 100.
      I have an input Text box at the bottom and a little button to press to input commands (I dont know how to make it so when you press enter it will input the command, if you could help me on that as well, that would be great), but it doesn't work. I have the variable set for the input text box as command. The code I have in the Input button is:
      on(Press) {
      if(command="attack") {
      enemyhp-5
      Clear(commandbox)
      }
      }

      So what I am hoping to do is have it take away 5 Health points ("HP") from the enemy's health when "attack" is typed into the command box. I have set the variables for the player's and the enemy's hp in the first frame, and all the stuff like the text boxes are on Frame 2. The action script I have for Frame 2 is just stop(); .
      If someone could help me get this working, I would be very grateful.
      By the way, the Input button is not a Button, but a movie clip.

      I am also having a problem with my buttons. I have tested this on many occasions and it has only ever worked once, even though I use the same code each time. I have a "Play" movie clip with the Actionscript:
      on(Press) {
      gotoAndPlay(2);
      }
        • 1. [AS2] Making a text based game
          Ned Murphy Adobe Community Professional & MVP
          For a conditional test, use ==, not =

          enemyhp-5 is a dead end statement, assuming enemyhp is the value you want to subtract from

          The correct way(s) of doing this would be...

          enemyhp = enemyhp - 5; //OR
          enemyhp -= 5;

          what is the code on the Play movie clip supposed to do?

          I get a sense that this won't fix all of what's wrong, mainly because you haven't disclosed enough details about the textfields you are using. If you provided their instance names, it might be possible to offer more info in the event you are dealing with them incorrectly as well.
          • 2. [AS2] Making a text based game
            Gamelord2 Level 1
            Okay, Thanks your help!
            The dynamic enemy health text box's variable is enemyhp, the player health box is playerhp.
            I dont have instance names for either of those because I didn't think they were necessary for what I am trying to do right now.
            Then, I got rid of the input button, and instead learned how to do if(key.isdown(enter)){

            and so I use that now (I do have the correct capitalization in the real code, but I can't remember it off the top of my head.)
            I changed the code on each frame to:
            if (Key.isDown(enter)) {
            if (command == "attack") {
            enemyhp = enemyhp-5;
            }
            if (command == "attack self") {
            playerhp = playerhp-5;
            }
            }
            Am I doing this right?

            The Play button is just:
            on(press) {
            gotoAndPlay(2);
            }

            But it never works, with the exception of one time. I am using Flash CS3, and the one time it worked I was in Flash 8, so I don't know if it makes a difference.
            • 3. Re: [AS2] Making a text based game
              Ned Murphy Adobe Community Professional & MVP
              The play button will tell whatever timeline it is sitting in to gotoAndPlay(2). IF you are using CS3, check your Publish Settings to make sure you have AS 2.0 specified for the actionscript version.

              For the textfields, if you are using their variable names, you should also declare these variables in the actionscript code in the timeline (to start, based on what I read before).

              var enemyhp = 50;
              var playerhp = 100;

              if command is also a textfield, then that should also have a var declared to go with it.
              • 4. Re: [AS2] Making a text based game
                Gamelord2 Level 1
                command is the var for the Command box.
                I already had the vars all declared, I have no idea why it isn't working.
                I re-added the Input button just in case I had the code wrong for when you press enter on your keyboard.
                on (press) {
                if (command == "attack") {
                enemyhp = enemyhp-5;
                }
                if (command == "attack self") {
                playerhp = playerhp-5;
                }
                }
                Is the code for the Input button now.
                • 5. Re: [AS2] Making a text based game
                  Ned Murphy Adobe Community Professional & MVP
                  While it might seem like more work, you may be better of utlizing the textfields as input devices with instance names and keeping the variables as separate enities (meaning remove them from the textfields)

                  So when you address/assign the textfield value you would use something like...

                  enemyhp_textfieldName.text = enemyhp;

                  As far as your play button goes, where are you placing that on(press) code? And you may get better results using on(release)
                  • 6. Re: [AS2] Making a text based game
                    Gamelord2 Level 1
                    Okay, my play button is on the first frame of the movie. I right click the Play button and hit Actions and put the code in there. on(release) doesn't work either. Do I need to actually export any of my projects, or is Ctrl+Enter testing enough?
                    • 7. Re: [AS2] Making a text based game
                      Ned Murphy Adobe Community Professional & MVP
                      Do you know how to use trace statements? They are very handy for seeing where things are and are not working so that you can narrow things down (somewhat of a "can you hear me now" means of testing). Trace results pop up in the output panel. Try the following:

                      on (release) {
                      trace("button clicked");
                      trace(command);
                      if (command == "attack") {
                      enemyhp = enemyhp-5;
                      }
                      if (command == "attack self") {
                      playerhp = playerhp-5;
                      }
                      }

                      If you get anything from that in the output panel. It will tell you the button is working. And with the second trace statement it will tell you what the command value is that the function sees.
                      • 8. Re: [AS2] Making a text based game
                        Gamelord2 Level 1
                        Thanks, this is returned in the output panel using the code you gave me (when I press the button):
                        button clicked
                        undefined

                        So I think that means that there is no command for it to do when the button is pressed, but there is.
                        • 9. [AS2] Making a text based game
                          Ned Murphy Adobe Community Professional & MVP
                          So you now know that the problem isn't the button, it's got to do with not targeting and/or assigning the command variable.

                          As I mentioned earlier, you should probably get away from using variables assigned to textfields and use the actual textfield content. It makes things easier to deal with as far as the thought process goes. I gave it up years ago.

                          Give that 'command'-related textfield an instance name, let's say you call it Command, and in your button function trace Command.text. If it comes up with the value you have entered in that textfield, then use Command.text in your if statement instead of comand.
                          • 10. Re: [AS2] Making a text based game
                            Gamelord2 Level 1
                            Okay, I tried it and it now it comes up with two undefined's instead of one.
                            So, the problem isn't the button, the problem is that it doesn't know what I'm pointing it towards?
                            • 11. Re: [AS2] Making a text based game
                              Ned Murphy Adobe Community Professional & MVP
                              The fact that you are getting those traces to appear tells you that the button is working just fine, otherwise it would display the trace results.

                              Did you assign an instance name to the "command" textfield? You assign it in the properties panel.

                              Whatever instance name you give it, use that name with " .text " appended to it either assign or read whatever text is in that textfield. So if you give that textfield a name of "Orders" you would read it's value in the actionscript using Orders.text.

                              To trace that value, you use trace(Orders.text)

                              To assign some text to it, you would use Orders.text = "some text, any text, whatever you want.";
                              • 12. [AS2] Making a text based game
                                Gamelord2 Level 1
                                Thanks, that'll help me for something I was going to do once I got all this figured out. The instance name I game the textfield was "commandbox".
                                so in my code I do: trace(commandbox.text);
                                It just says Undefined in the output box though.
                                So it doesn't know what the commandbox is, or what the variable "command" is because that gets an undefined as well.
                                Or does that just mean it thinks that there is nothing in either of them?

                                Also, I could send you the flv (or fla, or whatever the extension of the project is) if you wanted to take a look at it.
                                • 13. Re: [AS2] Making a text based game
                                  Ned Murphy Adobe Community Professional & MVP
                                  The only thing I can think it might be at this point, assuming you have given the instance name properly, is that the button and the textfield do not occupy the same timeline. What I mean is maybe one is inside a movieclip while the other is not.

                                  If you can make your file available for download via a link here I might take a look at it if I have time.
                                  • 15. [AS2] Making a text based game
                                    Ned Murphy Adobe Community Professional & MVP
                                    Here's a link to a revised version of your file:

                                    Revised File

                                    There were a couple of things wrong with what you did. You did not declare the command variable, so it was undefined. I got rid of it and use the textfield.text property for the test. You place your actionscript in frame one abd your content in frame 2. The objects that you assign things to have to be in the presence of that code. Keep all of your actionscript on layers separate from your objects.

                                    You'll notice I changed the coding for the key press. If you plan to use other keys, you can add them into the function.

                                    In my version I got rid of whatever you are trying to accomplish with the gotoAndPlay(2) bit down the timeline. It would not let me enter text, and I wasn't going to race it.

                                    • 16. Re: [AS2] Making a text based game
                                      Gamelord2 Level 1
                                      Thank you so much Ned, I am going to remake it based off of what I have learned from your help.
                                      Thanks again!
                                      • 17. Re: [AS2] Making a text based game
                                        Ned Murphy Adobe Community Professional & MVP
                                        You're welcome.
                                        • 18. Re: [AS2] Making a text based game
                                          gamelord want to email your games to me


                                          if you want to email your games to me


                                          please email me @ cjmah17@gmail.com