2012-08-07 78 views
1

我是新搭建Metro應用程序。 我只是想創建每秒一個新的矩形,並開始與位置x = 0Win8 Metro應用程序setInterval Javascript

但與我的代碼,似乎在繪製第一個矩形之前調用x + = 10。但我不知道我怎麼能改變這個..有人可以幫我嗎?

(function() { 
    "use strict"; 

    var app = WinJS.Application; 
    var activation = Windows.ApplicationModel.Activation; 
    WinJS.strictProcessing(); 

    var x = 0; 
    var y = 0; 

    function Clock(context) { 

     context.fillRect(x, y, 9, 9); 
     context.fillStyle = "red"; 

     x += 10; 
    } 

    app.onactivated = function (args) { 
     if (args.detail.kind === activation.ActivationKind.launch) { 
      if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) { 
       var canvas = document.getElementById("uhr"); 
       var context = canvas.getContext("2d"); 

       setInterval(function() { Clock(context) }, 1000); 


      } else { 

      } 
      args.setPromise(WinJS.UI.processAll()); 
     } 
    }; 

    app.oncheckpoint = function (args) { 

    }; 


    app.start(); 
})(); 

回答

0

嘗試將x和y的值存儲在Clock()中的臨時和本地作用域變量中。

function Clock(context) { 
    var tmpX = x; 
    var tmpY = y; 
    context.fillRect(tmpX, tmpY, 9, 9); 
    context.fillStyle = "red"; 

    x += 10; 
} 
0

我已經用context.fill()解決了它;在做x + = 10之前;

相關問題