2D运动和命中testPoints

时间:2014-12-11 20:35:53

标签: 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
    {
        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字符一侧的所有点 如果有人感到困惑,那么movieclip角色的图片。 http://prntscr.com/5fqu69

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;
        }

0 个答案:

没有答案