0
votes

I'm making a game with simple mouse control. Basically, if the player moves the cursor to the right of the character, he moves right and vice versa. But if you stop moving the mouse, Flash ignores a condition which stops the character until you move the mouse again, so the guy just keeps moving in the same direction until you move the cursor. Why is this happening if mouseX and the player's position are being updated each frame? Here are the input conditions:

stage.addEventListener(MouseEvent.MOUSE_MOVE, CTRLupdateMousePosition)

    private function CTRLupdateMousePosition(e:Event):void 
    {
        if (mouseX > shaman_legs.x-2 - shaman_legs.width / 2 && mouseX < shaman_legs.x+2 + shaman_legs.width / 2) {
            CTRLleftButtonDown = false
            CTRLrightButtonDown = false
        } else if (mouseX < shaman_legs.x - shaman_legs.width / 2 && GlobalVariableLibrary.COREgamePaused == false) {
            CTRLleftButtonDown = true
            CTRLrightButtonDown = false
        } else if (mouseX > shaman_legs.x + shaman_legs.width / 2 && GlobalVariableLibrary.COREgamePaused == false) {
            CTRLleftButtonDown = false
            CTRLrightButtonDown = true
        }
    }

And here is the code that responds to player's input by moving the character and playing animation:

addEventListener(Event.ENTER_FRAME, playerControl)

    private function playerControl(e:Event):void {
        trace("LeftDown:"+CTRLleftButtonDown, "RightDown:"+CTRLrightButtonDown)
        boxSensor.x = shaman_legs.x
        soulCatcher.x = shaman_legs.x
        if (CTRLleftButtonDown == false && CTRLrightButtonDown == false && GlobalVariableLibrary.COREgamePaused == false) {
            shaman_legs.gotoAndPlay("idle")
            shaman_armsDown.gotoAndPlay("idle")
        } else if (CTRLleftButtonDown && shaman_legs.x > 40 && GlobalVariableLibrary.COREgamePaused == false) {
            shaman_legs.gotoAndPlay("walkLeft")
            shaman_armsDown.gotoAndPlay("walk")
            shaman_legs.x -= GlobalVariableLibrary.CHARspeed
            shaman_mask.x -= GlobalVariableLibrary.CHARspeed
            shaman_armsDown.x -= GlobalVariableLibrary.CHARspeed
            shaman_armsUp.x -= GlobalVariableLibrary.CHARspeed
        } else if (CTRLrightButtonDown && shaman_legs.x < 760 && GlobalVariableLibrary.COREgamePaused == false) {
            shaman_legs.gotoAndPlay("walkRight")
            shaman_armsDown.gotoAndPlay("walk")
            shaman_legs.x += GlobalVariableLibrary.CHARspeed
            shaman_mask.x += GlobalVariableLibrary.CHARspeed
            shaman_armsDown.x += GlobalVariableLibrary.CHARspeed
            shaman_armsUp.x += GlobalVariableLibrary.CHARspeed
        }
    }
1
You do not provide enough information. Where is this code that you posted located? What condition are you talking about is ignored? What was the result of stepping through your code with a debugger? What happened? And what did you expect to happen?null
This is a function of enter frame event listener. The condition I meant was the first one (the moment when the cursor is positioned over the player), which is supposed to stop the character's movement and his animation. I tried tracing those Bools, and they both don't toggle to false when the first condition is met. Should I post the movement code as well?Yarick Shokopets
Please edit your question to include all that information. It's still very vague. enter frame listener of what object? Yes, please post the movement code as well.null
Updated my post. This info seems to be sufficient.Yarick Shokopets
I am not quite sure what you are doing here. You update CTRLleftButtonDown / CTRLrightButtonDown after every mouse movement. Then in the enterframe you check these values. How exactly should it work?Fygo

1 Answers

0
votes

You stated the solution yourself: if the mouse is not moving, the MouseEvent will not be dispatched. But only the handler of the MouseEvent changes the variables. The variables never change and the character doesn't stop.

The MouseEvent is simply not appropriate here.

It's also a lot easier to work with the mouse coordinates of the object, as you want to have them relative to its position anyway.

Here is a very simple example program illustrating how to use the mouseX and mouseY properties of the object, instead of using those of the main time line and adding the coordinates of the object. It does not use MouseEvent because as you experienced yourself: it's not doing what you want.

import flash.display.Sprite;
import flash.events.Event; 

const SPEED:Number = 1;

var box:Sprite = new Sprite();
box.graphics.beginFill(0xff0000);
box.graphics.drawRect(-50, -20, 100, 40);
box.graphics.endFill();

addChild(box);

box.x = box.y = 200;

addEventListener(Event.ENTER_FRAME, onLoop);

function onLoop(e:Event):void
{
    var halfHeight:Number = box.height / 2, halfWidth:Number = box.width / 2;         
    if (box.mouseX > halfWidth)
        box.x += SPEED;
    else if (box.mouseX < -halfWidth)
        box.x -= SPEED;

    if (box.mouseY > halfHeight)
        box.y += SPEED;
    else if (box.mouseY < -halfHeight)
        box.y -= SPEED;
}

Try it on the time line of a new empty project.


This is offtopic, but please put those parts of the shaman into a container instead of moving them all individually by the same amount.