运行范例

描述

该范例展示碰撞检测相关功能。

了解微软脚本库的更多信息请参见微软官方帮助文档. 微软官方帮助文档.

代码 (文件格式为 UTF-8)



    <!DOCTYPE>
    <html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=8"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>碰撞检测</title>
        <script type="text/javascript" src="SuperMap.Include.js"></script>
        <script type="text/javascript">
            var htmlUrl = document.location.host;
            var scene = null;
            var sceneControl = null;

            //判断网页的打开方式是本地打开还是通过网络打开
            //不同的打开方式url赋值不同
            if (htmlUrl == "") {
                htmlUrl = "http://localhost:8090";
            }
            else {
                htmlUrl = "http://" + htmlUrl;
            }
            var sceneUrl = htmlUrl + "/iserver/services/3D-sample/rest/realspace";

            function onPageLoad() {
                //初始化三维场景控件
                try {
                    //初始化三维场景控件实例,参数为包含控件的HTML元素
                    sceneControl = new SuperMap.Web.UI.Controls.SceneControl($get("sceneControlDiv"), initCallback, failedCallback);
                }
                catch (e) {
                    //若没有安装插件,则抛出该异常
                    if (e.name == SuperMap.Web.Realspace.ExceptionName.PlugInNotInstalled) {
                        var url = htmlUrl + "/iserver/iClient/for3D/plugin/Setup.exe";
                        document.write("<a href='" + url + "'>未检测到 SuperMap iClient3D for  Plugin 插件,请单击此处下载并安装插件。</a>");
                        return;
                    }
                    //若使用非IE浏览器,则抛出该异常
                    else if (e.name == SuperMap.Web.Realspace.ExceptionName.BrowserNotSupport) {
                        document.write("<p>SuperMap iClient3D for  Plugin 目前仅支持 InternetExplorer 浏览器,请更换浏览器后重新尝试加载本页面。</p>");
                        return;
                    }
                    //抛出其他异常
                    else {
                        alert(e.message);
                    }
                }
            }

            //控件初始化完成后的回调函数,初始化完成之后才能进行数据加载
            function initCallback() {
                //获取Realspace控件的场景,控件和场景是一对一的捆绑关系
                scene = sceneControl.get_scene();
                scene.open(sceneUrl,"scene_olympicgreen");
                var camera = new SuperMap.Web.Realspace.Camera(116.396072, 39.987669, 11.865241);
                camera.set_heading(136.22209);
                camera.set_tilt(90.162235);
                scene.set_firstPersonCamera(camera);

                var collision = scene.get_collisionDetection();
                var walkMode = scene.get_walkingMode();
                if(collision){
                    document.getElementById('collisionStatus0').checked = 'true';
                }else{
                    document.getElementById('collisionStatus1').checked = 'true';
                }
                if(walkMode == 0){
                    document.getElementById('walkMode0').checked = 'true';
                }else if(walkMode == 1){
                    document.getElementById('walkMode1').checked = 'true';
                }else if(walkMode == 2){
                    document.getElementById('walkMode2').checked = 'true';
                }else if    (walkMode == 3){
                    document.getElementById('walkMode3').checked = 'true';
                }
            }

            //控件初始化失败后的回调函数
            function failedCallback() {
                alert("Realspace initialized failed!");
            }

            function setCollision(obj){
                var value = obj.value;
                scene.set_collisionDetection(value);
            }

            function setWalkMode(obj){
                var value = obj.value;
                scene.set_walkingMode(value);
            }

        </script>
    </head>
    <body onLoad="onPageLoad()" style="font-size:13.5px">
    <div>
        碰撞检测:<input type='radio' id='collisionStatus0' name='collisionStatus' value='true' onclick='setCollision(this)'/>开启
        <input type='radio' id='collisionStatus1' name='collisionStatus' value='false' onclick='setCollision(this)'/>关闭
        <span style='margin-left:100px'></span>
        行走模式:<input type='radio' id='walkMode0' name='walkMode' value='0' onclick='setWalkMode(this)'/>无
        <input type='radio' id='walkMode1' name='walkMode' value='1' onclick='setWalkMode(this)'/>通过键盘
        <input type='radio' id='walkMode2' name='walkMode' value='2' onclick='setWalkMode(this)'/>自动行走
        <input type='radio' id='walkMode3' name='walkMode' value='3' onclick='setWalkMode(this)'/>自动跑步
    </div>
    <div id="sceneControlDiv" style="position:absolute;width:100%;height:100%;border:3px solid #cfcfcf;margin-top:10px">
    </div>
    </body>
    </html>