2011-06-06 36 views
1

我寫了一個名爲ButtonTile的類來擴展SimpleButton類。然後,我創建一個ButtonTile對象的數組,並將它們添加到我的舞臺上。SimpleButtons顯示但在舞臺上不活躍

當我運行代碼時,所有的ButtonTile對象出現在舞臺上,但它們不可點擊,並且它們的顏色不會因其上下狀態而改變。

以下是適用於ButtonTile類的代碼:

package com.shakti.gameState{ 

import flash.display.*; 

public class ButtonTile extends SimpleButton { 

    public var id:int; 
    public var quizId:int; 
    public var subjectId:int; 
    public var points:int; 
    public var questionTxt:String; 
    public var order:int; 
    public var option:Array; 

    public function ButtonTile(newId:int, newQuizId:int, newSubId:int, newPoints:int, qtxt:String, newOrder:int, newOption:Array) { 
     this.id=newId; 
     this.quizId=newQuizId; 
     this.subjectId=newSubId; 
     this.points=newPoints; 
     this.questionTxt=qtxt; 
     this.order=newOrder; 
     this.option=newOption; 
     this.upState=TileColor(0); 
     this.downState=TileColor(1); 
     this.overState=TileColor(2); 
     this.useHandCursor = true; 
     this.enabled = true; 
    } 

    public function TileColor(stateFlag:int):Shape{ 

     var newShape:Shape=new Shape(); 
     if (stateFlag == 0){ 
      trace('hi'); 
      newShape.graphics.beginFill(0x000000); 
     } 
     else if (stateFlag == 1) { 
      newShape.graphics.beginFill(0x00ff00); 
     } 
     else { 
      newShape.graphics.beginFill(0x303030); 
     } 
     newShape.graphics.drawRect(0,0,70,50); 
     newShape.graphics.endFill(); 
     return(newShape); 
    } 
} 

,這是創建ButtonTile對象數組的代碼:

 public function MakeButtons():Array{ 

     var a:int = 0; 

     for (var i:int = 0; i < quizState.subjects.length; ++i){ 

      for (var e:int = 0; e < quizState.subjects[i].quizQuestions.length; ++e){ 

       var id:int = quizState.subjects[i].quizQuestions[e].id; 
       var quizId:int = quizState.subjects[i].quizQuestions[e].quizId; 
       var subjectId:int = quizState.subjects[i].quizQuestions[e].subjectId; 
       var points:int = quizState.subjects[i].quizQuestions[e].points; 
       var questionTxt:String = quizState.subjects[i].quizQuestions[e].question; 
       var order:int = quizState.subjects[i].quizQuestions[e].order; 
       var option:Array = quizState.subjects[i].quizQuestions[e].option; 

       tempTile = new ButtonTile(id, quizId, subjectId, points, questionTxt, order, option);     

       buttons[a] = tempTile; 
       ++a; 
      } 
     } 
     return buttons; 
    } 

事件偵聽器添加到每個對象,因爲它是添加到舞臺上。

for (var i:int = 0; i < buttons.length; ++i){ 
var tempTile:ButtonTile; 
tempTile = buttons[i]; 
tempTile.x = boxPoints[i].x; 
tempTile.y = boxPoints[i].y; 
tempTile.addEventListener(MouseEvent.CLICK, PopQuest); 
addChild(tempTile); 

}

感謝您的幫助!

回答

1

我相信你只是缺少在ButtonTile類hitTestState屬性:

this.hitTestState = TileColor(0);