0

我想知道为什么我正在做的应用程序出错了?当我摇晃电话时,他没有开始演奏。我按照这个网站上的说明进行操作,http://software.intel.com/pt-br/articles/create-handbell-using-html5-and-phonegap,但我不太明白它是如何工作的。这是代码:

 <!DOCTYPE HTML>
<html>
  <head>
    <meta name="viewport" content="width=320; user-scalable=no" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>BellShapes</title>

      <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
      <script type="text/javascript" charset="utf-8">

       function onLoad(){
        // Aguardar o carregamento do Cordova
        document.addEventListener("deviceready", onDeviceReady, false);
        // inicialização
        bellrightring = document.getElementById("bellrightring");
        bellleftring = document.getElementById("bellleftring");
        bellleft = document.getElementById("bellleft");
        bellright = document.getElementById("bellright");
        bellcenter = document.getElementById("bellcenter");
        // inicialização da mídia
        bellsound = new Media("/android_asset/www/resources/bell.mp3",
        onRingSuccess(bellsound), onRingError);
        bellend = new Media("/android_asset/www/resources/bell-end.mp3",
        onRingSuccess(bellend), onRingError);
        }
        function sign(val, delta) {
            if (typeof delta == undefined) delta=0
            if (val > 0 && Math.abs(val)-delta > 0) return 1;
            else if (val<0 && Math.abs(val)-delta > 0) return -1;
            else return 0;
        }
        function isShaking(last, current, threshold) {
        var deltaX = Math.abs(last.x - current.x),
            deltaY = Math.abs(last.y - current.y),
            deltaZ = Math.abs(last.z - current.z);
                return ((deltaX > threshold && deltaY > threshold) ||
                    (deltaX > threshold && deltaZ > threshold) ||
                    (deltaY > threshold && deltaZ > threshold) || deltaX > threshold);   
        }
        accelID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
        function onSuccess(acceleration) {
            var x = acceleration.x;
            var y = acceleration.y;
            var z = acceleration.z;
            var t = false;
            if (previous){
            if (isShaking(previous, acceleration, 10)){     
            moveBell(sign(x)); 
            bellsound.play();
            accelerated = true;
            }
            else {
                stopBell(acceleration);
                if (accelerated == true){
                bellend.play();
            }
            accelerated = false;
        }
    }

        previous = acceleration;
    }



</script>  

  </head>
  <body id="onLoad">
    <div id="handbell">

        <div id="bellcenter" class="position"></div>

        <div id="bellright" class="position"></div>

        <div id="bellleft" class="position"></div>

        <div id="bellrightring" class="ring"></div>

        <div id="bellleftring" class="ring"></div>

    </div>
  </body>
</html>

我对javascript了解不多,所以如果您能以更简单的方式解释,我将不胜感激。

4

1 回答 1

0

在你的身体标签中,你写了

<body id="onLoad">

这应该是

<body onLoad="onLoad()">

您需要在页面加载时调用 onLoad 函数。

或者,您可以删除 id 并忘记 body 标记中的 onload。然后将这一行添加到 Javascript。

window.addEventListener("load", onLoad);
于 2014-02-26T00:00:09.437 回答