21 Replies Latest reply on Feb 14, 2009 12:26 PM by FlashMonkey666

    if (Key.isUp(Key.DOWN)) {

    FlashMonkey666 Level 1
      I know there are ways to do this with different types of syntax. But is there a simple way to do:

      if (Key.isUP(Key.DOWN)) {
      //do blah blah
      }

      So that something happens when the key is released?

      This is in the onClipEvent (enterFrame) { handler, by the way

      thanks
        • 1. if (Key.isUp(Key.DOWN)) {
          clbeech Level 3
          in order to use the onKeyDown and onKeyUp methods you need to create a listener object as in:

          var lstn = new Object();
          lstn.onKeyUp = function() {
          //do stuff
          }
          lstn.onKeyDown = function() {
          //do stuff
          }
          Key.addListener(lstn);
          • 2. Re: if (Key.isUp(Key.DOWN)) {
            FlashMonkey666 Level 1
            Oh, yeah. Thanks. I've seen that listenerobject stuff before, and I used it in a previous game I made. I've tried adapting it to my current project. But it's weird. It doesn't seem to do anything. Can you tell me what's wrong with this script?

            onClipEvent (enterFrame) {
            keyDownA = [];
            lo = new Object();
            //listenerobject
            lo.onKeyDown = function() {
            //KEYDOWN. only when the key is pushed once.
            if (!keyDownA[Key.getAscii()]) {
            keyDownA[Key.getAscii()] = true;
            whichKey = Key.getAscii();
            if ((whichkey == _root.upkey) or (whichkey == _root.upkeycaps)) {
            _root.mov = "up";
            }
            if ((whichkey == _root.leftkey) or (whichkey == _root.leftkeycaps)) {
            _root.mov = "left";
            }
            if ((whichkey == _root.rightkey) or (whichkey == _root.rightkeycaps)) {
            _root.mov = "right";
            }
            if ((whichkey == _root.downkey) or (whichkey == _root.downkeycaps)) {
            _root.mov = "down";
            trace("");
            }
            }
            };
            //KEYup. only when the key is pushed once.
            if (!keyDownA[Key.getAscii()]) {
            keyDownA[Key.getAscii()] = true;
            whichKey = Key.getAscii();
            if ((whichkey == _root.upkey) or (whichkey == _root.upkeycaps)) {
            if (_root.mov == "up") {
            _root.mov = "stop";
            }
            }
            if ((whichkey == _root.leftkey) or (whichkey == _root.leftkeycaps)) {
            if (_root.mov == "left") {
            _root.mov = "stop";
            }
            }
            if ((whichkey == _root.rightkey) or (whichkey == _root.rightkeycaps)) {
            if (_root.mov == "right") {
            _root.mov = "stop";
            }
            }
            if ((whichkey == _root.downkey) or (whichkey == _root.downkeycaps)) {
            if (_root.mov == "down") {
            _root.mov = "stop";
            }
            }
            }
            • 3. Re: if (Key.isUp(Key.DOWN)) {
              clbeech Level 3
              you're welcome :) but sorry dude, when I look at this code i see a lot of problems i'm afraid :(

              first, you should try not to 'attach' code to instance (ie. using an 'on' handler) there are a number of reasons, it's just not a 'best practice' - instead you codes should go on a timeline or class file.

              next, there are a lot of 'redundant' operations here, as well as the re-declaring of several variable objects within the event loop on every enter frame - got to slow things down.

              but in here is a key handling method that i liked to use under AS2 that may help you to eliminate some of that code and restructure things - although I can't say 'what' you're doing for certain since i don't have the rest of the code structure or file design.
              • 4. Re: if (Key.isUp(Key.DOWN)) {
                FlashMonkey666 Level 1
                awesome! What does "break" do?

                • 5. Re: if (Key.isUp(Key.DOWN)) {
                  FlashMonkey666 Level 1
                  I'm trying to figure out where to put this code. I get by what you said that I shouldn't put it within the onClipEvent (enterFrame) { handler.

                  Where should I put it? I tried putting it in the script of the frame. Flash seemed to like it there, but it still didn't work when I ran the program.

                  If you're curious to see the thing in its entire glorious nature:
                  http://www.iamyourpappi.com/vegehunter1.6.fla

                  And thanks again so much for helping me!
                  • 6. Re: if (Key.isUp(Key.DOWN)) {
                    FlashMonkey666 Level 1
                    I'm re-reading your notes again and trying to glean as much as I can from them:

                    "your codes should go on a timeline or class file." I'm guessing you mean codes in general, not just this particular code. So, I mentioned that I put it in the script of the frame. Is this what you mean? else, what's a timeline or class file?

                    Thanks again for all your help. Flash is still mysterious to me. I grew up programming Hypercard. And I'm barely learning what I'm in for here. :)
                    • 7. Re: if (Key.isUp(Key.DOWN)) {
                      clbeech Level 3
                      lol - that's ok - you'll get the hang of it :)

                      a 'break' statement is used for a few different things, to terminate a loop, or -like in this method- to stop a 'switch' statement from continuing.

                      the code does belong in a frame on the timeline of the MC that this was attached to - if is a 'button' instance, you should probably change it to a MC (movieclip). however - it 'could' go on the main timeline (the first one you see when the file opens) where the variable 'mov' should be declared - you could then also remove any calls to '_root' as the code resides in the same object (eg. main timeline)

                      it's hard to say just 'where' to place it, not knowing your file's structure, however it seems that the 'mov' variable is being used to 'adjust' some position value, likely within another enterframe event. this could most likely be done 'together' in a single statement on the main timeline, but it difficult to say without see the file.

                      just keep at it and try to learn something new each day, you be writing new games in no time :) also a good place to learn more is to use the Flash help documentation - there's a ton of info in there, and descriptions of every property, method and class.
                      • 8. Re: if (Key.isUp(Key.DOWN)) {
                        FlashMonkey666 Level 1
                        Huh, well I did put the code there, and it doesn't seem to be working.

                        I checked and double checked everything

                        Do you mind checking it out? http://www.iamyourpappi.com/vegehunter1.6.fla

                        Here's what I changed it to:

                        stop();
                        keyDownA = [];
                        lo = new Object();
                        lo.onKeyDown = function() {
                        //KEYDOWN. only when the key is pushed once.
                        if (!keyDownA[Key.getAscii()]) {
                        keyDownA[Key.getAscii()] = true;
                        whichKey = Key.getAscii();
                        if ((whichkey == _root.upkey) or (whichkey == _root.upkeycaps)) {
                        _root.mov = "up";
                        }
                        if ((whichkey == _root.leftkey) or (whichkey == _root.leftkeycaps)) {
                        _root.mov = "left";
                        }
                        if ((whichkey == _root.downkey) or (whichkey == _root.downkeycaps)) {
                        _root.mov = "down";
                        trace("");
                        }
                        if ((whichkey == _root.rightkey) or (whichkey == _root.rightkeycaps)) {
                        _root.mov = "right";
                        }
                        }
                        };
                        lo.onKeyUp = function() {
                        keyDownA[Key.getAscii()] = false;
                        whichKey = Key.getAscii();
                        if ((whichkey == _root.upkey) or (whichkey == _root.upkeycaps)) {
                        if (_root.mov == "up") {
                        _root.mov = "stop";
                        }
                        }
                        if ((whichkey == _root.leftkey) or (whichkey == _root.leftkeycaps)) {
                        if (_root.mov == "left") {
                        _root.mov = "stop";
                        }
                        }
                        if ((whichkey == _root.downkey) or (whichkey == _root.downkeycaps)) {
                        if (_root.mov == "down") {
                        _root.mov = "stop";
                        }
                        }
                        if ((whichkey == _root.rightkey) or (whichkey == _root.rightkeycaps)) {
                        if (_root.mov == "right") {
                        _root.mov = "stop";
                        }
                        }
                        };
                        • 9. Re: if (Key.isUp(Key.DOWN)) {
                          clbeech Level 3
                          the code that i wrote above should 'replace' all of the code you've posted. I will try to take a look at the file tomorrow for you.
                          • 10. Re: if (Key.isUp(Key.DOWN)) {
                            FlashMonkey666 Level 1
                            Yeah, I tried to replace my code with your code. And I tried to twerk it a number of different ways.
                            You can see one of those ways at the link I posted. I'm sure for someone with your experience, you'll be able to spot immediately what needs to be fixed.

                            Thanks for taking a look at it!
                            • 11. Re: if (Key.isUp(Key.DOWN)) {
                              clbeech Level 3
                              ok - I've finally had a opportunity to review your file. I've also taken a little time to rebuild some of it in order to show you a bit more about how complex systems like this can be constructed. you had placed the code I'd posted in with the original code which was all 'attached' to the maze mc - there are many reasons why this is not a good practice and your codes should go on the 'timeline'. however i do understand that your just getting going and that things like this take time to learn, i also see that you are using a very old version of Flash6 and that you are programming in AS1 - this is going to be a huge issue for you in developing something that is as complicated as what you are intending - and it is part of the reason for the problems you are experiencing here. I would recommend that you upgrade to at *least* Flash7 using AS2 in order to accomplish your goals - in fact I'm pretty certain that the code that I have rewritten and commented heavily for you will not even run in the version that you have now. however hopefully you will at least be able to open the file and review the script, most of which is located on frame2 - I've uploaded for you HERE and have published down as low as i can (FlashMX2004 - F7)

                              as far as 'what the issue were/are' - I'm afraid the list is much too long. so i think it would be best for you to review the file and the new code previous to discussing this further. once you have post here again or PM me and we can discuss it via email, rather than here on the forum. I hope that I haven't offended you, it is my intention to help you develop your skills further.
                              • 12. Re: if (Key.isUp(Key.DOWN)) {
                                FlashMonkey666 Level 1
                                Hi, clbeech, thanks again for all you've done for me.

                                I haven't had a chance to look at your hard work yet because I'm still trying to get hold of a copy of F7.

                                My mac OS is 10.3.9. Will F7 work on this OS?
                                • 13. Re: if (Key.isUp(Key.DOWN)) {
                                  clbeech Level 3
                                  you're welcome - yes it should, I've got a ibook with the same OS running 8 you should be good.
                                  • 14. Re: if (Key.isUp(Key.DOWN)) {
                                    FlashMonkey666 Level 1
                                    Well! I finally got a copy of Flash 7 and I'm up and running.

                                    I finally got a chance to look at your great work! And it's super. Very elegant code, and it really gets the job done.

                                    I understand 95% of it, but I wonder if you wouldn't mind explaining some things.

                                    "length"? Like in if (playerKeys.length>0) {
                                    What does this (is it called a parameter) mean?

                                    And..
                                    var resultValue:Boolean = false;
                                    What is boolean? What is this line doing?

                                    And..
                                    mazeDoors .swapDepths(10000);
                                    What is swapDepths?

                                    Overall, I'm really pleased with it, and I'm already moving forward.
                                    I'll post my recent updates pretty soon, but I'm curious about those few questions first.

                                    Also, I'm just curious if there's a way to make the key controls a little smoother? If you play it for a minute, you'll see what I mean; It's easy to get "stuck" and then you have to lift up on the key and press it again. This might be annoying for the user.

                                    Thanks again
                                    Andrew
                                    • 15. Re: if (Key.isUp(Key.DOWN)) {
                                      FlashMonkey666 Level 1
                                      Okay, here's the latest version. I've made some small corrections, explained below.
                                      http://www.iamyourpappi.com/vegehunter2.0.fla

                                      I added a function called "monsterCheck" which checks to see if you're inside any of the monsters, and if you are it takes you to a frame which says "you got eaten."
                                      I added an if-then clause to that function to check whether or not you have a bomb to blow up the monster. But for some reason, Flash doesn't want to recognize that you have a bomb. I'm still trying to figure that one out

                                      I fixed something: In your version, when you were close to a door without a key, the player was rendered immobile. I fixed it so that he can walk up to a door with no key, it doesn't open, and he can still turn around and go the other way.

                                      The only other little thing I did was add a little space between the player and the walls. I did this hoping that he wouldn't get "stuck" in the walls as much. But it didn't help much; the keys still "stick" like I was describing in my last response.

                                      I hope you like my updates. :)

                                      Thanks again for all your help. It has been tremendous.

                                      Andrew
                                      • 16. Re: if (Key.isUp(Key.DOWN)) {
                                        clbeech Level 3
                                        you're welcome :) ok - i haven't reviewed the file atm, but thought I'd respond to your questions.

                                        length - is a 'property' of the Array class, which returns the number of overall elements currently within the array.

                                        swapDepths - is a 'method' of the MovieClip class and is used to control the 'z-order' or the 'stacking' of objects on 'top of' one another - like layers. each item on the stage is 'always' stacked one on top of another, to get objects to appear in front or behind other things we control the 'depth', one way to do so is to use swapDepths to place a object at a particular depth - or to 'swap' the depths of two objects in the stack. the usage of it, in this case is for an addition issue however - if an object is 'placed' on the stage in the IDE, we cannot 'remove' that object using code 'unless' we call swapDepths first and then we can use removeMovieClip()

                                        bomb issue - not sure, i'll have to review the file.

                                        door issue - yes, the problem here is that the doors are not vector drawings within Flash and they have 'solid' backgrounds since they are image bitmap files. the problem here is that the hitTest is finding that the point is 'hitting' the object even though it is not close enough, and as a result can't move 'away' and gets stuck - one of the items i would have advised you to change (draw the door clip in Flash)

                                        getting stuck - yes, really a large part of the issue here is that the map and the position of the player, and the movement quantity are just not really 'in-sync' - that is to say the the 'sizes' of the items aren't related, and in much of the drawing 'not' on 'even' pixel increments. what I mean by that the sizes don't 'work' with each other, and are not 'positioned' on the correct points. for instance, the thickness of the walls (example) is 12 pixels, and the thickness of the 'hall' is 18px and the player is positioned 'not' at the 'center' of the hall at 8px, if the player steps right and our movement rate is 5px and the player is 'checking' for a hitTest 5px from his 'centerX' point, on the first iteration he finds that there is nothing there and moves right 5px, on the second iteration he again finds nothing there and moves right again 5px - however 'now' at this point his 'body' overlaps the wall by 3px and from now on the test will 'fail' because he's 'always' touching the wall - and as a result 'gets stuck'.

                                        how you work out the relationships between the spaces, the sizes, the movement and the player are all CRITICAL to getting a system like this to function correctly. and drawing the map is one of those items, which is a larger problem the way you have if done now (last i saw) when working on something like this i usually have the 'snap to grid' function on and have my grid set up to an even increment - 'even' is to say 'uniform' it can be whatever works for you make everything 9px if you want -but follow through with that measurement in 'all' aspect of the designed elements and controls.

                                        that's enough for now, keep working - sounds like you getting there ;)
                                        • 17. Re: if (Key.isUp(Key.DOWN)) {
                                          FlashMonkey666 Level 1
                                          Thanks for your explanations.

                                          I understand what you're saying about the maze walls and the player's movement not being in-synch. In an earlier version of this game, I wrote a complicated repeat loop for every time he walks a step forward to anticipate if there's going to be a wall in front of him. Then, still move him forward, but not all the way, so he's placed directly in front of the wall.

                                          I'm thinking about implementing that with this version.

                                          However, I think you'll find that the "stickyness" of the character's movement is more than just this issue. If you delete the whole maze and then play with the character just moving around in a white field, you'll notice he "sticks" every time you are going one direction and then you change directions without fully letting up on the first key.

                                          In other words, if you're holding down right and then you hit down, it usually hesitates or stalls out. Of course this doesn't happen if you're conscientious about releasing one key before hitting the next. But most users aren't going to have that patience. Do you see what I'm describing? Are there any ways around this issue?

                                          Thanks
                                          • 18. Re: if (Key.isUp(Key.DOWN)) {
                                            clbeech Level 3
                                            yeah could be any number of reasons for the issue, i think that i used a switch handler right? can't remember for sure.

                                            however, here's something that would probably solve several of these issue, i didn't want to get into something quite as advance as you learn, but here it is anyway. check my answer in this thread:

                                            http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid =1421897&CFID=12754630&CFTOKEN=fe258c041332ce5-3264EAB6-03CC-0856-FCE110C1CF705493&jsessio nid=48307fb6d6e988760655687703158a443258
                                            • 19. Re: if (Key.isUp(Key.DOWN)) {
                                              FlashMonkey666 Level 1
                                              I looked at your mouse game. Very nice! That's basically what I'm trying to do.

                                              Regarding the maze walls not being the right size for the character. I just spent a frustrating hour trying to resize the character and the maze walls so that they "fit." I made the character exactly 20 pixels round. And I played around with the maze walls until when you zoom in really close, they are (almost) 20 pixels apart.

                                              This still didn't work. You still have the character stopping _almost_in front of walls, walking partially inside walls and the like.

                                              So, am I understanding you correctly, that I'll have to somehow devise a new way to "draw" the maze from the ground up?

                                              How did you do it with your rat maze so that the character and the maze walls "fit"?

                                              Thanks!
                                              • 20. Re: if (Key.isUp(Key.DOWN)) {
                                                clbeech Level 3
                                                that game was my very first program :) and i knew very little at the time, what i did to solve the issue was to iterate through a series of 10 pixels from the registration point of the mouse for each direction, and hitTested each of them with loops LMAO! that was a long way around for sure. but it was important to have my mazes drawn exactly on increments - as it sounds like you are working towards (btw using the 'snap to grid' option is useful for this and you can change the size of your 'grid' to suit the size you want to draw to - right click outside the stage to view the options)

                                                see the above thread for a much better solution using a shape based hitTest - using this methodology you can draw the maze 'any' shape or configuration and not test by points, but rather by overlaying to bitmaps that you draw dynamically as the player moves.
                                                • 21. Re: if (Key.isUp(Key.DOWN)) {
                                                  FlashMonkey666 Level 1
                                                  Thanks. I just re-read that previous posting of yours, and I didn't understand it any more than when I read it the first time :)
                                                  Basically the whole second half, where you're drawing new bitmaps, is over my head.

                                                  What does it mean to "overlay to bitmaps"?

                                                  I appreciate all your help