13 Replies Latest reply on Oct 2, 2007 10:55 PM by DarthTurtle

    how to use  "and"

    DarthTurtle
      I am trying to make a game that uses the arrow keys and i was wondering how to make it so that when my person hits a wall, they stop moving in that direction and dont go through the wall.
      heres my actionscript

      for some reason i cant get the "&&" to work. And i think i'm not using it correctly. So can anyone help me?
      or if theres a better way to do this then please tell me.
        • 1. Re: how to use  "and"
          Greg Dove Level 4
          I think one problem is that 'then' is not valid actionscript. Reminds me of my old ZXSpectrum though, lol.

          Remove all the 'then' s and see if it works
          • 2. Re: how to use  "and"
            Greg Dove Level 4
            And where you're doing the checking use '==' (equality) instead of '=' (assignment)

            So

            if (Key.isDown(Key.LEFT)) && left=true {

            becomes:
            if (Key.isDown(Key.LEFT)) && left==true {
            • 3. Re: how to use  "and"
              clbeech Level 3
              @GWD: lol!! yeah, and the 'Apple IIe' green screen man, lol!! 'HLINE 0,0 TO ... '

              @DarthTurtle: Another tip for you here as well, you should 'declare' your variables elsewhere on the timeline, rather than in the clipevent, as in:

              stop();
              var left:Boolean = true;
              var right:Boolean = true;
              var up:Boolean = true;
              var down:Boolean = true;

              you should also stop() the timeline in this mc from playing so that it doesn't continue to refresh the vars and try to run the other codes. once the variables are instantiated on the timeline then you can refer to them by name elsewhere in your code.

              sorry GWD, there's a *typeo* there in the statements, the parenthesis are off, they should read:

              if (Key.isDown(Key.LEFT) && left==true) {
              }

              @DarthTurtle: all 'conditions' should be within the parenthesis
              • 4. Re: how to use  "and"
                Greg Dove Level 4
                @clbeech. Well spotted I can't believe I missed that. The equality thing jumped out at me and I missed that.
                • 5. Re: how to use  "and"
                  Greg Dove Level 4
                  here's another one.

                  I think you need to remove all quotes in your hittests

                  if (this.hitTest(_root."wall4" ))

                  if (this.hitTest(_root.wall4 ))
                  • 6. Re: how to use  "and"
                    DarthTurtle Level 1
                    so i would change the actionscript in my Player to


                    onClipEvent (enterframe){
                    if (Key.isDown(Key.LEFT) && left==true) {
                    this._x-= 5
                    }
                    if (Key.isDown(Key.RIGHT)) && right==true {
                    this._x+= 5
                    }
                    if (Key.isDown(Key.UP)) && up==true {
                    this._y-= 5
                    }
                    if (Key.isDown(Key.DOWN)) && down==true {
                    this._y+= 5
                    }
                    if (this.hitTest( _root.wall1 ) ) {
                    up=false
                    }
                    if (this.hitTest(_root.wall2 )) {
                    right=false
                    }
                    if (this.hitTest(_root.wall3 )) {
                    down=false
                    }
                    if (this.hitTest(_root.wall4 )) {
                    left=false
                    }
                    }

                    but i keep getting an error message
                    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Unexpected '&&' encountered
                    if (Key.isDown(Key.RIGHT)) && right==true {

                    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 10: Unexpected '&&' encountered
                    if (Key.isDown(Key.UP)) && up==true {

                    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 11: Statement must appear within on/onClipEvent handler
                    this._y-= 5

                    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 12: Unexpected '}' encountered
                    }

                    Total ActionScript Errors: 4 Reported Errors: 4

                    and on the frame i would put
                    stop();
                    var left:Boolean = true;
                    var right:Boolean = true;
                    var up:Boolean = true;
                    var down:Boolean = true;

                    that doesnt have any errors.

                    and i was wondering how all of you people know so much about flash, how did all of you learn the actionsript?
                    • 7. Re: how to use  "and"
                      Bob_Robertson Level 1
                      One thing that caught my eye was the syntax of your if statements.
                      You use if(condition1) && condition2{ code }; I will confess to have never seen it done that way before. In the spirit of readability, you might try the more standard
                      if(condition1 && condition2){ code }
                      . A sample from your code is attached.
                      • 8. Re: how to use  "and"
                        clbeech Level 3
                        yes that's correct, you need to remove the 'extra' parenthesis, like bob's goto it above.

                        @GWD: yeah I missed the 'quotes' one too ... LOL!!!
                        • 9. Re: how to use  "and"
                          clbeech Level 3
                          Darth - put the variable declarations on the same frame, before the onClipEvent handler.

                          AS will take time to learn, just be patient and try to learn something new every day, and read, read, read ... especially the Flash Help manual, the As class listing has a ton of info in it on every method, handler, function and so on in Flash, it just takes time :)
                          • 10. Re: how to use  "and"
                            DarthTurtle Level 1
                            @Bob Robertson: so i would change it to

                            if (Key.isDown(Key.LEFT && left==true)){
                            this._x-=5
                            }

                            ??
                            @clbeech: so i would keep the actionscript on the frame the same?
                            and could someone tell me what the ClipEvent handler is? is that the actionscript like

                            on ClipEvent (load)

                            or something like that?
                            and is there an online flash help manual? because i dont have one.
                            and by the way if it makes any difference I'm using Flash MX 2004 Educational.

                            • 11. Re: how to use  "and"
                              Greg Dove Level 4
                              That should be more like this (I tend to over-use parentheses so perhaps there are redundant pairs here)

                              if ((Key.isDown(Key.LEFT)) && (left==true)) {
                              this._x-= 5
                              }


                              Online manual for mx2004 here:
                              http://livedocs.adobe.com/flash/mx2004/
                              • 12. Re: how to use  "and"
                                clbeech Level 3
                                @GWD: for sure, there any left?! LOL!!! jk :)

                                @DarthT: yes the variables should be declared before they are used, and it appeared as though you were kinda doing this with the onClipEvent(load) call earlier so i would assume ... on the same frame at the top of your code.

                                As to the parenthesis issue, remember that you should surround things that must happen first, that is ... this happens, then this happens. But also many of the handlers and methods in AS also use parentheses to include things as parameters, like the IF statement is including a conditional argument or arguments, and in this case the two are separated by the the && operator, also in this case you are getting a return value from the Key.isDown() method which requires the input parameter of Key.LEFT for instance, but this is to be included within the () for the method's call, and that parameter only.

                                So you can use parenthesis to enclose items, like GWD did above, enclosing the two conditional arguments within a set of parentheses, but has still used them around the isDown() method enclosing the parameter as needed. And both conditions are then enclosed within the IF statement. The minimal needed parentheses needed for this statement would be:

                                if( Key.isDown(Key.LEFT) && left ) {
                                this._x -=5;
                                }

                                I also did something here that's a little different, I only used the variable 'left' and did not use the rest of the conditions statement, this is not a typeo :) We can use a Boolean value simply by calling the variable, and do not have to check the entire condition because the IF statement checks for Boolean values of the conditional expression, or the value of the varibles. to put this another way if you were only checking the 'left' variable for true, you could say:

                                if(left) {
                                //do something
                                }

                                and to check a Boolean for false, add the ' ! ', as to say 'not true' just like ' != ' :

                                if(!left) {
                                //do something
                                }

                                OK just a little extra info for you to put into you noodle ;)
                                • 13. Re: how to use  "and"
                                  DarthTurtle Level 1
                                  Well i couldn't get the code to work so I used google and i followed a tutorial and came up with
                                  And i was wondering how the "while" things work?