2012-09-28 41 views
0

我是新的AppMobi世界的新手,所以你們可以幫助我。使用AppMobi&Events

我已經創建了一個圖像對象並將其繪製到舞臺上。我的問題是我想添加一個事件到對象。

我已經設置使用圖像:

function objImage(imageURL,x,y,width,height) { 
    this.imgCanvas = new Image(); 
    this.imageURL = imageURL; 
    this.loaded = false; 
    this.x = x; 
    this.y = y; 
    this.height = height; 
    this.width = width; 

    this.draw = function() { 

     if (this.width == null || this.height == null) 
     { 
      ctx.drawImage(this.imgCanvas,this.x,this.y); 
     } 
     else 
     { 
      ctx.drawImage(this.imgCanvas,this.x,this.y,this.width,this.height);   
     } 
    } 
} 

而且我已經吸引到舞臺:

var usercontrols_left = new objImage('images/left.png',10,HEIGHT-100,50,50); 
usercontrols_left.draw(); 

現在我想將事件添加到「usercontrols_left」,例如檢測當用戶觸摸/點擊它時。

回答

2

我不能告訴你是否使用directCanvas或不。我相信這個解決方案應該可以工作。

捕獲touchstart事件,並通過它的座標感知它是否位於左元素的位置。如果是,則執行需要觸發的命令。

僞代碼:

Canvas.addEventListener('touchstart', startTouch, false); 

function startTouch(){ 
    if(touch.x<60&&touch.x>10&&touch.y<height-50&&touch.y>height-100) 
    { handleLeftControl(); } 
} 

編輯*使用的WebView代替的例子: 在網頁視圖你必須:

<div id="Leftbutn" ontouchstart="AppMobi.canvas.execute('switchCommand(\'leftBTNdown\');')" ontouchend="AppMobi.canvas.execute('switchCommand(\'leftBTup\');')" ></div> 

然後在directCanvas方面,你只是處理那些命令通過你想在交換機或功能中做什麼。

+0

謝謝你的回答。我正在使用directCanvas。那麼是否沒有辦法在特定元素上設置事件? –

+1

我個人建議在webview中創建你的UI,然後把事件傳遞給DC,因爲這樣更容易維護。 – appMobiTyler

+0

謝謝!所以最好的做法是在html中創建所有UI元素,顯示和隱藏部分主菜單,遊戲等,並通過事件傳遞給DC來執行功能? –