Actionscript 3 2D移动和命中测试点

Actionscript 3 2D移动和命中测试点,actionscript-3,flash,actionscript,flash-cs6,Actionscript 3,Flash,Actionscript,Flash Cs6,我试图让一个角色能够在世界电影圈中移动,但不能穿过墙壁。 这是角色类,非常基本。移动变量指示角色是否移动。 问题是,当我对着世界向左或向右移动角色时,角色也会向上或向下移动 // Code to move character package { import flash.display.*; import flash.events.*; import flash.ui.*; public class Character extends MovieClip

我试图让一个角色能够在世界电影圈中移动,但不能穿过墙壁。 这是角色类,非常基本。移动变量指示角色是否移动。 问题是,当我对着世界向左或向右移动角色时,角色也会向上或向下移动

// Code to move character
package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;

    public class Character extends MovieClip
    {
        public var Moving:Boolean = true;
        public var maxSpeed = 10;
        public var dx = 0;
        public var dy = 0;
        public var rot = 0;
        public var rof = 30;
        private var keysDown:Array = new Array  ;
        // Declare variables:
        //var bullet:Bullet;
        var reload:int = 10;
        private var bullets:Array;


        public function Character()
        {
            //bullets = bulletList;
            // Initialize variables:

            addEventListener(Event.ADDED_TO_STAGE,init);
        }


        function init(e:Event):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyReleased);
            this.addEventListener(Event.ENTER_FRAME,processInput);
            this.addEventListener(Event.ENTER_FRAME,moveMe);
            removeEventListener(Event.ADDED_TO_STAGE,init);
        }

        public function removeT():void
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
            stage.removeEventListener(KeyboardEvent.KEY_UP,keyReleased);
            this.removeEventListener(Event.ENTER_FRAME,processInput);
            this.removeEventListener(Event.ENTER_FRAME,moveMe);
        }






        private function keyPressed(e:KeyboardEvent):void
        {
            keysDown[e.keyCode] = true;
            // Handle keys pressed:
        }

        private function keyReleased(e:KeyboardEvent):void
        {
            keysDown[e.keyCode] = false;
        }

        private function processInput(e:Event)
        {
            // Handle keys held:
            dx = 0;
            dy = 0;

            if (keysDown[Keyboard.LEFT])
            {

                dx =  -  maxSpeed;
                dy = 0;
                rot = -180;
                this.gotoAndStop(4);
            }
            if (keysDown[Keyboard.RIGHT])
            {
                dx = maxSpeed;
                dy = 0;
                rot = 0;
                this.gotoAndStop(1);
            }
            if (keysDown[Keyboard.UP])
            {
                dx = 0;
                dy =  -  maxSpeed;
                rot = -90;
                this.gotoAndStop(3);
            }
            if (keysDown[Keyboard.DOWN])

            {
                this.gotoAndStop(2);
                dx = 0;
                dy = maxSpeed;
                rot = 90;
            }

        }

        private function moveMe(e:Event)
        {
            if(Moving){
            this.x +=  dx;
            this.y +=  dy;
            this.rotation = rot;
            }

             //stop if it tries to go off the screen
            if (this.x < 0)
            {
                this.x = 0;
            }
            if (this.x > stage.stageWidth)
            {
                this.x = stage.stageWidth;
            }
            if (this.y < 0)
            {
                this.y = 0;
            }
            if (this.y > stage.stageHeight)
            {
                this.y = stage.stageHeight;
            }
        }
    }
}
下面是对象和世界碰撞检查的代码。我使用函数checkFrom和checkFromX来检查movieclip周围的点。因此,当检查其中一个函数时,它将遍历movieclip角色这一侧的所有点 如果有人感到困惑,请拍摄电影唇角色的照片


伙计,为什么不使用简单的hitTest呢?您可以轻松地为边界创建对象,并使用单次命中测试代替循环的所有命中测试。。我不理解“向左走向上”这句话-请描述得更好。@Andreypov我有义务使用这段代码,因为这是一个学校项目。我遇到的问题是,当角色在上升或下降时碰到对象世界时,角色会停止,因为hitTestPoint函数会迭代所有点并检测碰撞。但是,当我与世界的右侧或左侧碰撞时,角色将停止向右或向左移动,而是开始向上或向下移动。您还没有展示如何将碰撞检查器与移动逻辑链接。。我们不知道为什么会发生这种情况:
function checkFrom(x1:int, x2:int, y:int )
{
    if (x2<x1) {
        trace("x2<x1");
        return false;
    }
    for (var i=x1; i<=x2; i++)
    {
        if (world.hitTestPoint(i,y,true))
        {
            return true;
        }
    }
    return false;
}

//made this to check for right and left
function checkFromX(y1:int, y2:int, x:int )
{//y1 at top
    if (y2<y1) {
        trace("y2<y1");
        return false;
    }
    for (var a=y1; a<=y2; a++)
    {
        if (world.hitTestPoint(x,a,true))
        {
            return true;
        }
    }
    return false;
}







function moveDude(e:Event):void
{

    var obj:Object = e.target;

    if (obj.hitTestObject(world.lv1))
    {
        cleanup();
        gotoAndStop("donkeyKong");
    }
    else
    {



        obj.Moving = true;
        while (checkFrom(obj.x - obj.width / 2 + obj.width / 8,obj.x + obj.width / 2 - obj.width / 8,obj.y + obj.height / 2 - obj.height / 9))
        {//bot
            obj.y--;
            obj.Moving = false;

        }
        while (checkFrom(obj.x - obj.width / 2 + obj.width / 8,obj.x + obj.width / 2 - obj.width / 8,obj.y - obj.height / 2 + obj.height / 9))
        {

            obj.y++;
            obj.Moving = false;

        }
        while (checkFromX(obj.y - obj.height / 2 + obj.height / 7,obj.y - obj.height / 7 + obj.height / 2,obj.x - obj.width / 2 + obj.width / 9))
        {//left
            obj.x++;
            obj.Moving = false;

        }
        while (checkFromX(obj.y - obj.height / 2 + obj.height / 7,obj.y + obj.height / 2 - obj.height / 7,obj.x + obj.width / 2 - obj.width / 9))
        {
            obj.x--;
            obj.Moving = false;
        }