I wrote a class called ButtonTile to extend the SimpleButton class. I then create an array of ButtonTile objects and add them to my stage in a grid formation.
When I run the code all of the ButtonTile objects appear on the stage, but they are not clickable and their color does not change for their over and down states.
Here’s the code for the ButtonTile class:
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);
}
}
and this is the code that creates the array of ButtonTile objects:
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;
}
The event listener is added to each object as it is added to the stage.
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);
}
Thanks for your help!!!
I believe you’re just missing the
hitTestStateproperty in your ButtonTile class: