2013-02-15 30 views
1

嗨,我在iPad中建立一個應用程序,我有對象,當我移動ipad甚至對象應該使用acccelerometer功能移動。但是當我使用下面的代碼時,它不會進入Mobeobject()功能我可以知道哪裏出錯了。在ipad中使用加速度計移動對象

我的對象不動。

// Start moving the object 

var startMove = $('#startMove'); 

startMove.live("click",function() 
{ 
    alert("startMoving>>"); 

    startMoving(); 

    $(this).hide(); 

}); 

// Start watching the acceleration 

function startMoving(){ 

    alert("startMoving"); 

    var options = { frequency: 500 }; 

    alert("insidestartMoving"); 

    watchMove = navigator.accelerometer.watchAcceleration(moveObject, onError, options); 

    alert("instartMoving"); 

} 

// moveObject 

function moveObject(acceleration) { 

    alert("moveObject"); 

    var myObj = $('#obj'); 

    var wall = $('#obj_wall'); 

    var objPosition = myObj.position(); 

    var leftBoundary = 0; 

    var topBoundary = 0; 

    var rightBoundary = wall.width() - myObj.width() - 10; // 10 represents the 10px for the margin 

    var bottomBoundary = wall.height() - myObj.height() - 10; // 10 represents the 10px for the margin 

    if(acceleration.x < 0 && objPosition.left <= rightBoundary) { 

     myObj.animate({ 
      left:'+=10' 
     },100); 
    } else if(acceleration.x > 0 && objPosition.left > leftBoundary) { 
     myObj.animate({ 
      left:'-=10' 
     },100); 
    } 
    if(acceleration.y < 0 && objPosition.top > topBoundary) { 
     myObj.animate({ 
      top:'-=10' 
     },100); 
    } else if(acceleration.y > 0 && objPosition.top <= bottomBoundary) { 
     myObj.animate({ 
      top:'+=10' 
     },100); 
    } 
} 
<!DOCTYPE html> 
<html> 
    <head> 
    <title></title> 

    <body> 
    <div data-role="page"> 
    <div data-role="content"> 
      <div id="obj_wall"> 
      <div id="obj"></div> 
     </div> 
     <div> 
      <a href="#" id="startMove" data-role="button">Start Moving</a> 
     </div> 
    </div> 
    </div> 

開始移動

+0

你可以在上面的代碼中添加startMove按鈕嗎? – 2013-02-15 06:33:17

+0

@ClintonWard我編輯了代碼,並張貼上面.. ..可以檢查它的按鈕method.its進入看到之後,它不進入moveobject(加速度) – crazy2431 2013-02-15 06:41:59

+0

你試過使用最新的科爾多瓦? 2.4.0 – 2013-02-15 08:56:19

回答

1

分析和嘗試了很長一段時間之後,我想通了,問題是用cordova.We必須使用科爾多瓦,1.7.0.js和上面的代碼工作絕對罰款...

1

瓚GE的動畫代碼下面

myObj.animate({ 'left': -10}, "slow"); 
+0

哪部分代碼可以更改? – crazy2431 2013-02-15 09:37:31