2013-04-15 28 views
0

我正在嘗試創建多個可拖放的文本框。我接着前一個教程,使用四邊形作爲一個例子,你可以使用:Starling textField觸摸事件

var target:Quad = event.target as Quad; 

從而有針對性四,你徘徊在,我試圖在編制將其更改爲

var target:TextField = event.target as TextField; 

哪裏給了我錯誤「無法訪問空對象引用的屬性或方法」。我不太確定問題是什麼,所以如果有人能爲我清楚這將是一件好事。

這裏是代碼其餘多數民衆贊成有關:

public function onAdded():void{ 
//stuff that initialises bitmap 

for(var i:int=0; i<3; i++){ 

      //create Textfield 
      var bmpFont:starling.text.TextField = new starling.text.TextField(100,100, "test", "Arial", 10); 

      bmpFont.fontSize = 50; 
      if(i==0){ 
       bmpFont.color = Color.WHITE; 

      } 
      else if (i==1){ 
       bmpFont.color = Color.RED; 
      } 

      else if (i==2){ 
       bmpFont.color = Color.BLUE; 
      } 

      bmpFont.x = Math.random() * (stage.stageWidth - bmpFont.width); 
      bmpFont.y = stage.stageHeight/2; 
      //centering pivot point 
      bmpFont.pivotX = 50; 
      bmpFont.pivotY = 50; 

      //centering code 
      //bmpFont.x = stage.stageWidth - bmpFont.width >> 1; 
      //bmpFont.y = stage.stageHeight - bmpFont.height >> 1; 

      useHandCursor = true;     
      bmpFont.addEventListener(starling.events.TouchEvent.TOUCH, onTouch); 
      parent.addChild(bmpFont); 

      } 

的ontouch功能:

 //function activating on touch 
     public function onTouch(event:starling.events.TouchEvent):void{ 
      var touches:Vector.<Touch> = event.getTouches(stage, TouchPhase.MOVED); 
      //var target:Quad = event.target as Quad; 

      var target:starling.text.TextField= event.target as starling.text.TextField 

      //single finger manipulations 
      if(touches.length == 1){ 
       var delta:Point = touches[0].getMovement(parent); 
       target.x += delta.x; 
       target.y += delta.y; 
      } 
+0

你確定這個轉換不會導致空目標對象? var target:starling.text.TextField = event.target as starling.text.TextField –

+0

我不太清楚你的意思,你能否詳細說明一下? – David

+0

我忘記提及當我嘗試移動textField時發生錯誤,並且在發佈 – David

回答

0

嘗試

//function activating on touch 
    public function onTouch(event:starling.events.TouchEvent):void{ 
     var target:starling.text.TextField= event.target as starling.text.TextField; 
     var touches:Vector.<Touch> = event.getTouches(target, TouchPhase.MOVED); 


     //single finger manipulations 
     if(touches.length == 1){ 
      var delta:Point = touches[0].getMovement(target.parent); 
      // delta and target should be not null 

      target.x += delta.x; 
      target.y += delta.y; 
     } 
+0

它不會出現錯誤,但沒有任何TextFields移動 – David

+0

我做了一個跟蹤,它似乎沒有檢測到任何一個手指觸摸,不太確定爲什麼它沒有檢測到它,因爲當我使用它的四個例子是「var target:Quad = event.target as Quad;」這似乎檢測觸摸元素的罰款 – David

+0

那麼這意味着問題是何時計算增量。使用斷點並查看delta.x和delta.y的值。還嘗試計算觸摸[0] .getMovement(this)或getMovement(stage),抱歉,但我無法調試您的代碼。我能看到的是,你從Starling幫助頁面中弄出了一些原始樣本。 –