i have a problem with my as3 code,
i try to link a movie clip to different scene (quiz scene).
so,i have 2 quiz in total, first quiz is using external script (as)
And the second quiz have the same script like first quiz, but i placed the action script inside fla. and it had different xml.
but then this error message appeared:
TypeError: Error #2007: Parameter format must be non-null.
at flash.text::TextField/set defaultTextFormat()
at hiragana/createText()[E:\flash\!!!! FLASH JADI\PAK ASHAR FIX\hiragana.as:80]
at hiragana/kuisdua()[hiragana::frame209:48]
at hiragana/frame209()[hiragana::frame209:249]
this is the code:
// creates a text field
public function createText(text:String, tf:TextFormat, s:Sprite, x,y: Number, width:Number): TextField {
var tField:TextField = new TextField();
tField.x = x;
tField.y = y;
tField.width = width;
tField.defaultTextFormat = tf; //looks like this is the source of problem (-.-)
tField.selectable = false;
tField.multiline = true;
tField.wordWrap = true;
if (tf.align == "left") {
tField.autoSize = TextFieldAutoSize.LEFT;
} else {
tField.autoSize = TextFieldAutoSize.CENTER;
}
tField.text = text;
s.addChild(tField);
return tField;
}
and this is the ettire code
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
///*public class*/ kuisduaa extends MovieClip {
// question data
/*private*/ var dataXML2:XML;
// text formats
/*private*/ var questionFormat2:TextFormat;
/*private*/ var answerFormat2:TextFormat;
/*private*/ var scoreFormat2:TextFormat;
// text fields
/*private*/ var messageField2:TextField;
/*private*/ var questionField2:TextField;
/*private*/ var scoreField2:TextField;
// sprites and objects
/*private*/ var gameSprite2:Sprite;
/*private*/ var questionSprite2:Sprite;
/*private*/ var answerSprites2:Sprite;
/*private*/ var gameButton2:GameButton;
// game state variables
/*private*/ var questionNum2:int;
/*private*/ var correctAnswer2:String;
/*private*/ var numQuestionsAsked2:int;
/*private*/ var numCorrect2:int;
/*private*/ var answers2:Array;
/*public*/ function kuisdua() {
// create game sprite
gameSprite2 = new Sprite();
addChild(gameSprite2);
// set text formats
questionFormat2 = new TextFormat("Arial",80,0xffffff,true,false,false,null,null,"center");
answerFormat2 = new TextFormat("Arial",50,0xffffff,true,false,false,null,null,"left");
scoreFormat2 = new TextFormat("Arial",30,0xffffff,true,false,false,null,null,"center");
// create score field and starting message text
scoreField2 = createText("",scoreFormat,gameSprite,-30,550,550);
messageField2 = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);
// set up game state and load questions
questionNum2 = 0;
numQuestionsAsked2 = 0;
numCorrect2 = 0;
showGameScore2();
xmlImport2();
}
// start loading of questions
/*public*/ function xmlImport2() {
var xmlURL:URLRequest = new URLRequest("kuis2.xml");
var xmlLoader:URLLoader = new URLLoader(xmlURL);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
// questions loaded
/*public*/ function xmlLoaded2(event:Event) {
dataXML = XML(event.target.data);
gameSprite.removeChild(messageField);
messageField = createText("Tap Untuk Memulai",scoreFormat,gameSprite,-10,250,500);
showGameButton("mulai");
}
// creates a text field
/*public*/ function createText2(text:String, tf:TextFormat, s:Sprite, x,y: Number, width:Number): TextField {
var tField2:TextField = new TextField();
tField2.x = x;
tField2.y = y;
tField2.width = width;
tField2.defaultTextFormat = tf;
tField2.selectable = false;
tField2.multiline = true;
tField2.wordWrap = true;
if (tf.align == "left") {
tField2.autoSize = TextFieldAutoSize.LEFT;
} else {
tField2.autoSize = TextFieldAutoSize.CENTER;
}
tField2.text = text;
s.addChild(tField2);
return tField2;
}
// updates the score
/*public*/ function showGameScore2() {
scoreField2.text = "Soal: "+numQuestionsAsked2+" Benar: "+numCorrect2;
}
// ask player if they are ready for next question
/*public*/ function showGameButton2(buttonLabel:String) {
gameButton = new GameButton();
gameButton.label.text = buttonLabel;
gameButton.x = 240;
gameButton.y = 480;
gameSprite2.addChild(gameButton);
gameButton.addEventListener(MouseEvent.CLICK,pressedGameButton2);
}
// player is ready
/*public*/ function pressedGameButton2(event:MouseEvent) {
// clean up question
if (questionSprite2 != null) {
gameSprite2.removeChild(questionSprite2);
}
// remove button and message
gameSprite2.removeChild(gameButton);
gameSprite2.removeChild(messageField2);
// ask the next question
if (questionNum >= dataXML.child("*").length()) {
gotoAndStop(6);
} else {
askQuestion2();
}
}
// set up the question
/*public*/ function askQuestion2() {
// prepare new question sprite
questionSprite2 = new Sprite();
gameSprite2.addChild(questionSprite2);
// create text field for question
var question2:String = dataXML.item[questionNum].question2;
if (dataXML.item[questionNum].question.#type == "text") {
questionField2 = createText(question2,questionFormat2,questionSprite2,50,150,300);
} else {
var questionLoader2:Loader = new Loader();
var questionRequest2:URLRequest = new URLRequest("triviaimages/"+question2);
questionLoader2.load(questionRequest2);
questionLoader2.y = 150;
questionLoader2.x = 180;
questionSprite2.addChild(questionLoader2);
}
// create sprite for answers, get correct answer and shuffle all
correctAnswer2 = dataXML.item[questionNum2].answers.answer[0];
answers2 = shuffleAnswers(dataXML.item[questionNum2].answers);
// put each answer into a new sprite with a icon
answerSprites2 = new Sprite();
var xpos:int = 0;
var ypos:int = 0;
for(var i:int=0;i<answers2.length;i++) {
var answerSprite2:Sprite = new Sprite();
if (answers2[i].type == "text") {
var answerField2:TextField = createText(answers2[i].value,answerFormat2,answerSprite2,30,-35,200);
} else {
var answerLoader2:Loader = new Loader();
var answerRequest2:URLRequest = new URLRequest("triviaimages/"+answers2[i].value);
answerLoader2.load(answerRequest2);
answerLoader2.y = -22;
answerLoader2.x = 25;
answerSprite2.addChild(answerLoader2);
}
var letter:String = String.fromCharCode(65+i); // A-D
var circle:Circle = new Circle(); // from Library
circle.letter.text = letter;
circle.answer = answers[i].value;
answerSprite2.x = 100+xpos*250;
answerSprite2.y = 350+ypos*100;
xpos++
if (xpos > 1) {
xpos = 0;
ypos += 1;
}
answerSprite2.addChild(circle);
answerSprite2.addEventListener(MouseEvent.CLICK,clickAnswer); // make it a button
// set a larger click area
answerSprite2.graphics.beginFill(0x000000,0);
answerSprite2.graphics.drawRect(-50, 0, 200, 80);
answerSprites2.addChild(answerSprite2);
}
questionSprite2.addChild(answerSprites2);
}
// take all the answers and shuffle them into an array
/*public*/ function shuffleAnswers2(answers:XMLList) {
var shuffledAnswers2:Array = new Array();
while (answers2.child("*").length() > 0) {
var r:int = Math.floor(Math.random()*answers.child("*").length());
shuffledAnswers2.push({type: answers2.answer[r].#type, value: answers2.answer[r]});
delete answers2.answer[r];
}
return shuffledAnswers2;
}
// player selects an answer
/*public*/ function clickAnswer2(event:MouseEvent) {
// get selected answer text, and compare
var selectedAnswer2 = event.currentTarget.getChildAt(1).answer;
if (selectedAnswer2 == correctAnswer2) {
numCorrect++;
messageField2 = createText("Hai, kamu benar ! ",scoreFormat2,gameSprite2,-30,280,550);
} else {
messageField2 = createText("Iie, Jawabanmu Salah, yang benar adalah:",scoreFormat2,gameSprite2,53,280,370);
}
finishQuestion();
}
/*public*/ function finishQuestion2() {
// remove all but the correct answer
for(var i:int=0;i<4;i++) {
answerSprites2.getChildAt(i).removeEventListener(MouseEvent.CLICK,clickAnswer);
if (answers2[i].value != correctAnswer2) {
answerSprites2.getChildAt(i).visible = false;
} else {
answerSprites2.getChildAt(i).x = 200;
answerSprites2.getChildAt(i).y = 400;
}
}
// next question
questionNum2++;
numQuestionsAsked2++;
showGameScore2();
showGameButton2("Lanjutkan");
}
// clean up sprites
/*public*/ function CleanUp2() {
removeChild(gameSprite);
gameSprite2 = null;
questionSprite2 = null;
answerSprites2 = null;
dataXML2 = null;
}
the first quiz played perfectly with that code,
and i have no clue why this error appeared,
i'm beginner in as3 so i'm still lack in many ways,
can anyone help me,?
i really apreciate it.. :)
Are you sure that you're parsing a non-null TextFormat instance as the second argument of createText()?
The error means that you're supplying a null value for tf:TextFormat.
Unless I am missing something, your issue is here:
messageField2 = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);
&
messageField = createText("Tap Untuk Memulai",scoreFormat,gameSprite,-10,250,500);
I see questionFormat2 and scoreFormat2 are instantiated, but I never see a questionFormat or scoreFormat. Your error says that the defaultTextFormat (or TextFormat supplied using setTextFormat(), for future reference), cannot be null. Null means that the object has not been instantiated/created yet. questionFormat and scoreFormat are never instantiated. Hell, their variables do not even exist. If you were using a proper development IDE (FlashBuilder, FlashDevelop, etc), you'd never be able to compile this code.
Switch those two lines to use the correct formats and you should be fine.
I am trying to make button panel. each button have two type btn_home and btn_home_white I am trying to reach those buttons.It is work if I write for each button their own methods like
btn_home.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_home_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
function overEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween( btn_home,"height",Bounce.easeOut,25,0,3,true);
var myTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,0,25,3,true);
var myTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,0,1,2,true);
}
function outEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween btn_home,"height",Bounce.easeOut,0,25,3,true);
var myTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,25,0,3,true);
var myTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,1,0,2,true);
}
But I have 10 buttons as btn_buttonname and btn_buttonname_white . I tryed to create Event listener on stage for all.It works for firts type buttons btn_buttonname but How can I get second type buttons? I tryed e.target["_white"] but it does not work .
stage.addEventListener(MouseEvent.MOUSE_OVER , overEffect);
stage.addEventListener(MouseEvent.MOUSE_OUT , outEffect);
function overEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween(e.target,"height",Bounce.easeOut,25,0,3,true);
trace("height");
var myTweenHight2:Tween = new Tween(e.target["_white"],"height",Bounce.easeOut,0,25,3,true);
var myTweenAlpha:Tween = new Tween(e.target["_white"],"alpha",Strong.easeOut,0,1,2,true);
}
function outEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween(e.target,"height",Bounce.easeOut,0,25,3,true);
var myTweenHight2:Tween = new Tween(e.target["_white"],"height",Bounce.easeOut,25,0,3,true);
var myTweenAlpha:Tween = new Tween(e.target["_white"],"alpha",Strong.easeOut,1,0,2,true);
}
EDIT:
I made it work as Daniel Carvalho sad adding for each buttons event listener.Now it works but if I move out mouse on button to other button previous button does not turn original form .
btn_home.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_home_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_contact.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_contact_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_menu.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_menu_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_restaurant.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_restaurant_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_rezervation.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_rezervation_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
function overEffect(e:MouseEvent)
{
switch (e.target)
{
case ( btn_home) :
{
var HomeTweenHight:Tween = new Tween(btn_home,"height",Bounce.easeOut,25,0,0.5,true);
var HomeTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,0,25,0.5,true);
var HomeTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,0,1,2,true);
break;
};
case ( btn_contact) :
{
var ContactTweenHight:Tween = new Tween(btn_contact,"height",Bounce.easeOut,25,0,0.5,true);
var ContactTweenHight2:Tween = new Tween(btn_contact_white,"height",Bounce.easeOut,0,25,0.5,true);
var ContactTweenAlpha:Tween = new Tween(btn_contact_white,"alpha",Strong.easeOut,0,1,2,true);
break;
};
case ( btn_menu) :
{
var MenuTweenHight:Tween = new Tween(btn_menu,"height",Bounce.easeOut,25,0,0.5,true);
var MenuTweenHight2:Tween = new Tween(btn_menu_white,"height",Bounce.easeOut,0,25,0.5,true);
var MenuTweenAlpha:Tween = new Tween(btn_menu_white,"alpha",Strong.easeOut,0,1,2,true);
break;
};
case ( btn_restaurant) :
{
var RestourantTweenHight:Tween = new Tween(btn_restaurant,"height",Bounce.easeOut,25,0,0.5,true);
var RestourantTweenHight2:Tween = new Tween(btn_restaurant_white,"height",Bounce.easeOut,0,25,0.5,true);
var RestourantTweenAlpha:Tween = new Tween(btn_restaurant_white,"alpha",Strong.easeOut,0,1,2,true);
break;
};
case (btn_rezervation) :
{
var RezervationTweenHight:Tween = new Tween(btn_rezervation,"height",Bounce.easeOut,25,0,0.5,true);
var RezervationTweenHight2:Tween = new Tween(btn_rezervation_white,"height",Bounce.easeOut,0,25,0.5,true);
var RezervationTweenAlpha:Tween = new Tween(btn_rezervation_white,"alpha",Strong.easeOut,0,1,2,true);
break;
}
}
};
function outEffect(e:MouseEvent)
{
switch (e.target)
{
case (btn_home_white) :
{
var HomeTweenHight:Tween = new Tween(btn_home,"height",Bounce.easeOut,0,25,0.5,true);
var HomeTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,25,0,0.5,true);
var HomeTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,1,0,2,true);
break;
};
case (btn_contact_white) :
{
var ContactTweenHight:Tween = new Tween(btn_contact,"height",Bounce.easeOut,0,25,0.5,true);
var ContactTweenHight2:Tween = new Tween(btn_contact_white,"height",Bounce.easeOut,25,0,0.5,true);
var ContactTweenAlpha:Tween = new Tween(btn_contact_white,"alpha",Strong.easeOut,1,0,2,true);
break;
}
case (btn_menu_white) :
{
var MenuTweenHight:Tween = new Tween(btn_menu,"height",Bounce.easeOut,0,25,0.5,true);
var MenuTweenHight2:Tween = new Tween(btn_menu_white,"height",Bounce.easeOut,25,0,0.5,true);
var MenuTweenAlpha:Tween = new Tween(btn_menu_white,"alpha",Strong.easeOut,1,0,2,true);
break;
};
case (btn_restaurant_white) :
{
var RestourantTweenHight:Tween = new Tween(btn_restaurant,"height",Bounce.easeOut,0,25,0.5,true);
var RestourantTweenHight2:Tween = new Tween(btn_restaurant_white,"height",Bounce.easeOut,25,0,0.5,true);
var RestourantTweenAlpha:Tween = new Tween(btn_restaurant_white,"alpha",Strong.easeOut,1,0,2,true);
break;
};
case (btn_rezervation_white) :
{
var RezervationTweenHight:Tween = new Tween(btn_rezervation,"height",Bounce.easeOut,0,25,0.5,true);
var RezervationTweenHight2:Tween = new Tween(btn_rezervation_white,"height",Bounce.easeOut,25,0,0.5,true);
var RezervationTweenAlpha:Tween = new Tween(btn_rezervation_white,"alpha",Strong.easeOut,1,0,2,true);
break;
};
}
};
Your e.target should work, though you don't (as far as I can see) need the [_"white"] after e.target for your _white buttons. The reason the e.targets aren't working is because you're just adding event listeners to the stage, and not the individual buttons.
I see two choices (perhaps there are others): manually go through writing event listeners for each button, or put all the buttons that will use the overEffect function into one Array, and all the buttons using the outEffect function into another Array.
Then create two for loops, and go through those Arrays adding the event listeners to each child of the array. Your e.target code should work at that point.
Let me know if you have any trouble with that, hope it helps.
debu
Here is a quick example that may help you:
http://wonderfl.net/c/vhay
function createButton():MovieClip{
var button:MovieClip=new MovieClip();
var stateA:Shape=new Shape();
var g:Graphics = stateA.graphics;
g.beginFill(0xFF0000);
g.drawRect(0, 0, 10, 10);
g.endFill();
button["stateA"] = stateA;
button.addChild(stateA);
var stateB:Shape=new Shape();
g = stateB.graphics;
g.beginFill(0x00FF00);
g.drawRect(0, 0, 10, 10);
g.endFill();
button["stateB"] = stateB;
button.addChild(stateB);
stateB.visible=false;
return button;
}
var nbButtons:int=10;
var buttons:Array=[];
var selectButton:MovieClip;
for (var i : int = 0;i < nbButtons; i++) {
var button:MovieClip=createButton();
addChild(button);
button.y=10;
button.x=i*12+10;
addChild(button);
buttons.push(button);
setUpButton(button);
}
function setUpButton(button:MovieClip):void {
button.buttonMode=true;
button.addEventListener(MouseEvent.ROLL_OVER, buttonRollOver);
button.addEventListener(MouseEvent.ROLL_OUT, buttonRollOut);
button.addEventListener(MouseEvent.CLICK, buttonClick);
}
function buttonClick(e:Event):void{
switchButtonStateToNotSelected();
switchButtonStateToSelected(e.currentTarget as MovieClip);
}
function switchButtonStateToSelected(button:MovieClip):void{
button.buttonMode=true;
button.removeEventListener(MouseEvent.ROLL_OVER, buttonRollOver);
button.removeEventListener(MouseEvent.ROLL_OUT, buttonRollOut);
button.removeEventListener(MouseEvent.CLICK, buttonClick);
button["stateA"]["visible"]=false;
button["stateB"]["visible"]=true;
selectButton=button;
}
function switchButtonStateToNotSelected():void{
if(selectButton) {
setUpButton(selectButton);
selectButton["stateA"]["visible"]=true;
selectButton["stateB"]["visible"]=false;
selectButton=null;
}
}
function buttonRollOver(e:Event):void{
e.currentTarget["stateA"]["visible"]=false;
e.currentTarget["stateB"]["visible"]=true;
}
function buttonRollOut(e:Event):void{
e.currentTarget["stateA"]["visible"]=true;
e.currentTarget["stateB"]["visible"]=false;
}