0

我决定要学习光线投射并试一试。虽然我绝不是一个优秀的程序员,但我只是认为尝试扩展我的知识会很好,而且这在某些时候可能会有用:PI 一直在尝试遵循本指南:http ://www.permadi.com /tutorial/raycast/它实际上是一个非常好的指南,但我担心我在某个地方滑倒了,无法弄清楚在哪里。我的代码:

package rayCast{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Sprite;

    public class Main extends MovieClip{
        public var charPosX:Number = new Number(192);
        public var charPosY:Number = new Number(192);
        public static var charFov:Number = new Number(60);
        public var charPov:Number = new Number(45);
        public static var charHeight:Number = new Number(32);
        public static var projCharRel:Number = new Number(476);
        public static var rayAngle:Number = new Number(60/200);
        public var gridMap:Array = new Array();
        public var screen:Sprite = new Sprite();

        public function Main(){
            gridMap = [[1,1,1,1,1,1,1,1],[1,0,0,0,0,0,0,1],[1,0,0,1,0,0,0,1],[1,0,0,0,0,0,0,1],[1,0,0,0,1,0,0,1],[1,0,0,0,0,0,0,1],[1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1]];
            //addEventListener(Event.ENTER_FRAME, update);
            screen.graphics.lineStyle(1, 0x000000);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownE);
        }

        public function mouseDownE(e:MouseEvent):void{
            for( var i:int =0; i < 200; i++){
                var Ya:Number = new Number();
                var Xb:Number = new Number();
                var oldXb:Number = new Number();
                var oldYa:Number = new Number();
                if(i+charPov-30 > 180){
                    Ya = -64;
                    oldYa = Math.floor(charPosY/64) * (64) -1;
                }
                else{
                    Ya = 64;
                    oldYa = Math.floor(charPosY/64) * (64) + Ya;
                }
                if(charPov > 270 || charPov < 90){
                    Xb = 64;
                    oldXb = Math.floor(charPosX/64) * (64) + Xb;
                }
                else{
                    Xb = -64;
                    oldXb= Math.floor(charPosX/64) * (64) -1;
                }

                var oldXa:Number = charPosX + (charPosY-oldYa)/Math.tan(i+charPov-30);
                var oldYb:Number = charPosX + (charPosX-oldXb)*Math.tan(i+charPov-30);
                var hit:Boolean = false;
                var Xa:Number = 64/Math.tan(i+charPov-30);
                var Yb:Number = 64*Math.tan(i+charPov-30);
                var newYa:Number;
                var newXa:Number;
                var newYb:Number;
                var newXb:Number;
                var aDistance:Number;
                var bDistance:Number;

                while(hit != true){
                    newYa = oldYa + Ya;
                    newXa = oldXa + Xa;
                    if(gridMap[Math.floor(newYa/64)-1][Math.floor(newXa/64)-1] == 1){
                        aDistance = Math.abs(charPosX - newXa)/Math.cos(i+charPov-30);
                        hit=true;
                    }
                    else{
                        oldYa = newYa; oldXa = newXa;
                    }
                }
                hit = false;
                while(hit != true){                 
                    newYb = oldYb + Yb;
                    newXb = oldXb + Xb;
                    if(gridMap[Math.floor(newYb/64)-1][Math.floor(newXb/64)-1] == 1){
                        bDistance = Math.abs(charPosX - newXb)/Math.cos(i+charPov-30);
                        hit=true;
                    }
                    else{
                        oldYb = newYb; oldXb = newXb;
                    }
                }
                if(aDistance >= bDistance){
                    screen.graphics.moveTo(i, 90 - (64 / bDistance * 277)/2)
                    screen.graphics.lineTo(i, 90 + (64 / bDistance * 277)/2)
                }
                else{
                    screen.graphics.moveTo(i, 90 - (64 / aDistance * 277)/2)
                    screen.graphics.lineTo(i, 90 + (64 / aDistance * 277)/2)
                }

            }
        }
    }
}

这试图做的是在按下鼠标时渲染屏幕。不幸的是,每当我按下鼠标按钮时,我都会得到这个 TypeError: Error #1010: A term is undefined and has no properties。在 rayCast::Main/mouseDownE() 我认为可能是我的 gridMap[] 在某个地方有点偏离,但是当我做一个变量表时,一切似乎都工作正常:/ 也许我的变量表出错了,但有人可以帮忙我?在此先感谢,凯尔。

4

0 回答 0