A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: 8 direction movement

  1. #1
    swashbuckling vector bandit
    Join Date
    Nov 2003
    Posts
    31

    8 direction movement

    Does anyone know the code for 8 direction movement with the arrow keys?

    Steppenwolf is a good example of what i am trying to find. I have the code for 4 direction movement, but i cant seem to get the character to go diagonal.

    Any help would be much appreciated

    -Dave

  2. #2
    Member
    Join Date
    Mar 2004
    Posts
    80
    you use && like you would do

    if this && that

  3. #3
    Member
    Join Date
    Mar 2004
    Posts
    80
    sorry, that was vague, i just didnt feel like thinking, there's a few things you can do, Im sure they talk about it in the game tutorial section here, check that out
    Last edited by premiere522; 03-28-2004 at 12:52 PM.

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Make movie clip and place this code on the movie clip:
    Code:
    onClipEvent (load) {
        speed = 5;
    }
    onClipEvent (enterFrame) {
        if (Key.isDown(Key.LEFT) and Key.isDown(Key.UP)) {
            _x -= speed;
            _y -= speed;
        } else if (Key.isDown(Key.RIGHT) and Key.isDown(Key.UP)) {
            _x += speed;
            _y -= speed;
        } else if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
            _x -= speed;
            _y += speed;
        } else if (Key.isDown(Key.DOWN) and Key.isDown(Key.RIGHT)) {
            _x += speed;
            _y += speed;
        } else if (Key.isDown(Key.LEFT)) {
            _x -= speed;
        } else if (Key.isDown(Key.RIGHT)) {
            _x += speed;
        } else if (Key.isDown(Key.UP)) {
            _y -= speed;
        } else if (Key.isDown(Key.DOWN)) {
            _y += speed;
        }
    }

  5. #5
    swashbuckling vector bandit
    Join Date
    Nov 2003
    Posts
    31
    Thanks Tonypa, that worked perfect, legend!

    -Dave

  6. #6
    swashbuckling vector bandit
    Join Date
    Nov 2003
    Posts
    31
    One last thing, how can i get the characater to play the standing animation once the user has stopped moving, facing the way the user last pressed?

    -Dave

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Originally posted by beastyeast
    One last thing, how can i get the characater to play the standing animation once the user has stopped moving, facing the way the user last pressed?

    -Dave
    This can be done by using separate frames for each direction in the hero movie clip. Lets say frame 1 for moving left, frame 2 for moving right... 8 frames total. In each frame place the movie clip with the correct animation for that movement and give that movie clip an instance name "hero".

    Now when left key is pressed send movie clip to frame 1 and make animations play:
    Code:
        } else if (Key.isDown(Key.LEFT)) {
            _x -= speed;
            gotoAndStop (1);
            this.hero.play();
        }
    same way for the right key:

    Code:
        }else if (Key.isDown(Key.RIGHT)) {
            _x += speed;
            gotoAndStop (2);
            this.hero.play();
        }
    To make animation stop as soon as user has released the keys, send the animations movie clip to the frame 1 (which should have standing position).
    Add in the end of code after last key detection:

    Code:
    } else {
            this.hero.gotoAndStop(1);
    }
    Fla attached too with left/right animation.

  8. #8
    swashbuckling vector bandit
    Join Date
    Nov 2003
    Posts
    31
    Thanks again tonypa, worked brilliantly. Took me a while to get it working but its worked. Heres a little link to it working (tho i havent got the boundaries or the up animation done yet).

    http://www33.brinkster.com/beastyeast/move_test.swf

    Thanks again,

    -Dave

  9. #9
    Member
    Join Date
    Jan 2004
    Location
    Ontario,Canada
    Posts
    76
    That looks really amazing great potention.. is it just me or when you press up does he look like hes grabbin his crotch and sliding?

  10. #10
    Senior Member St. Nick's Avatar
    Join Date
    Sep 2003
    Location
    Ontario, Canada
    Posts
    312
    Lol, it so does
    You're going to change the _xscale and _yscale too, right? Otherwise when he is close up he looks normal sized but when you go near the door he is a giant. Looks kinda wierd but you might be able to get away with it if you only have many small rooms like this.

  11. #11
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Very nice animation
    Could become great game.

    I dont know if you should change the scale. For the example room it doesnt bother me, but if you create some street for example, going long way further, you might need to scale the char. If you do change the scale, make sure you place the registration point of the char movie clip in the bottom center, so his feet will stay on the ground.

  12. #12
    swashbuckling vector bandit
    Join Date
    Nov 2003
    Posts
    31
    aha, yeah when hes walking up he isnt animated yet, so just looks like hes sliding with some michael jackson pose.

    I will definately have scaling. Might proove to be a pain as different scenes will need different scaling. Some scenes will require the character to scale quicker than others etc.

    Im pretty new to coding, so i will be asking many a question here i am affraid. I dont even want to start thinking about coding the inventory items part yet.

    -Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center