Frage

Im making a maze-like game where the player controls a movieclip (_character) around the stage with the arrows or WASD and collects things in a certain amount of time. The MovieClip has five different frames, each with their own state animations, walk Left, right, up, down and stationary.

On the stage there are squares/boxes with collision detection and I need to be able to manoeuvre around them in all different directions.

My problem is that I cant seem to get the animations inside the movieclip AND correct movement of the movieclip itself working at the same time.

My current controls can zip around the obstacles great, stops when no keys are pressed and plays relevant _character animations, but when two diagonal direction keys are pressed the MovieClip travels diagonally, even after I release one of the keys.

e.g If there is one square/box on the stage, which I want to travel clockwise around it (starting at top side) I should be able to press right, down, left, up, then right once more to return to my original position.

However with my current code I'll press right, down (and before I can press left to go around the box) my Movieclip will travel to the lower right corner of the stage (down+right).

Even if I take my finger off the DOWN key and just press RIGHT it wont correct itself and go right, its stuck in that diagonal position!

Image gives example of pressing LEFT, then DOWN as game starts.

Game Movement

When I remove all the boxes from the stage to test movement in an empty area, the movieclip object still becomes fixed to a diagonal direction if I push down on two buttons, with no boxes to move around it just travels to the corner of the stage it is aimed at.

Preventing this is my problem, I cant just disable multiple key presses because then it becomes too difficult to move around the maze (_character would have to be EXACTLY lined up to the gap)

Current Code:

Variables for directions

private var _bUp:Boolean = false;
private var _bDown:Boolean = false;
private var _bLeft:Boolean = false;
private var _bRight:Boolean = false;

Variables true in KeyDown Handler.

private function keyDownHandler(event:KeyboardEvent):void
        {


            if (event.keyCode == Keyboard.LEFT || event.keyCode == 65 )
            {
                _bLeft=true;

            }
            else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
            {
                _bRight=true;

            }
            else if (event.keyCode == Keyboard.UP || event.keyCode == 87 )
            {
                _bUp=true;

            }
            else if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
            {
                _bDown=true;

            }

False in KeyUp.

private function keyUpHandler(event:KeyboardEvent):void
    {
                    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT 
                    || event.keyCode == 65 || event.keyCode == 68)
            {
                    _bLeft=false;
                    _bRight=false;

            } 
            else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP 
                    || event.keyCode == 87 || event.keyCode == 83 )
            {
                _bUp=false;
                _bDown=false;

            }

In the enterFrameHandler _character velocity is moved and animations play. If no keys pressed, velocity is 0 and _character is still. Then character movieclip gets the value of velocity to move, we check stage boundaries and then collisions with the boxes.

private function enterFrameHandler(event:Event):void
        {   

                            var _updown:Boolean=Boolean(!(_bUp==_bDown)); 
                            var _leftright:Boolean=Boolean(!(_bLeft==_bRight));

                        if (!_updown && !_leftright) 
                        {
                            // not moving anywhere
                            _character.gotoAndStop(1);
                            _character.vy=0;
                            _character.vx=0; 

                        } 
                        else
                        {
                            if (_bUp) 
                            {
                                _character.vy=-4;
                                _character.gotoAndStop(2);
                            } 
                            else if (_bDown)
                            {
                                _character.vy=4;
                                _character.gotoAndStop(3);
                            } 

                            if (_bLeft)
                            {
                                _character.vx=-4;
                                _character.gotoAndStop(4);
                            } 
                            else if (_bRight)
                            {
                                _character.vx=4;
                                _character.gotoAndStop(5);
                            } 
                        }

                    _character.x += _character.vx; 
                    _character.y += _character.vy;
                    checkStageBoundaries(_character);
                    //Collisions
                    Collision.block(_character,_box1);
                        Collision.block(_character,_box2);

collisions with the boxes.^^^

On a side note

Not sure if its of significance but my older keyboard method only used the enterFrame Handler to link the velocity to the character object and the KeyDown Handler moved the chacters velocity. This used the same collision detection boxes but I had no problems moving the movieclip object

That code was:

private function keyDownHandler(event:KeyboardEvent):void
            {
                if (event.keyCode == Keyboard.LEFT || event.keyCode == 65 )
                {
                    _character.vx = -4;
                    _character.gotoAndStop(4);
                }
                else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
                {
                    _character.vx = 4;
                    _character.gotoAndStop(5);
                }
                else if (event.keyCode == Keyboard.UP || event.keyCode == 87 )
                {
                    _character.vy = -4;
                    _character.gotoAndStop(2);
                }
                else if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
                {
                    _character.vy = 4;
                    _character.gotoAndStop(3);
                }   
            }

// Key up handler stops velocity and plays first frame which is the stationary image.

private function keyUpHandler(event:KeyboardEvent):void
            {
                if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT 
                    || event.keyCode == 65 || event.keyCode == 68)
                {
                    _character.vx = 0;
                    _character.gotoAndStop(1);
                } 
                else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP 
                    || event.keyCode == 87 || event.keyCode == 83 )
                {
                    _character.vy = 0;
                    _character.gotoAndStop(1);
                }
            }  

This movement technique moved the MovieClip around the stage perfectly, no strange routes, but the animations wouldnt play properly with multiple key presses, so I switched to the boolean variables and enterFrame control.

In Conclusion

I have no idea if the collision detection or keyboard controls are to blame, but I would just like to be able to manoeuvre the player Movieclip quickly around the boxes and have the appropriate state animations play.

Ive seen tile based design mentioned before, but I can get what I want in the two different keyboard attempts above. Surely I can get the best of both worlds without redesigning the whole thing as tiles?! Help me please!

War es hilfreich?

Lösung

Got it. I didn't tell you that _character's velocity should be cleared before parsing current movement. Sorry. So, you add this:

_character.vx=0;
_character.vy=0;

into the function enterFrameHandler at its start, and this should make your char go proper way.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top