AS3: Find parent of specified type - actionscript-3

I have this repeating pattern thoughout my code:
var scroller:Object = target;
while(scroller != null && !(scroller is Scroller) ) {
if(scroller.hasOwnProperty('parent')) {
scroller = scroller.parent;
} else return;
}
What I want is to make a generic function to call like so:
var scroller: Scroller = Scroller(dotParent(target, Class(Scroller)));
But I'm new to the language (I come from C#), so I don't know what to use for the comparison. I need it to handle any type, not just scrollers.

Here is the example of a such function:
protected function dotParent(target:DisplayObject, parentClass:Class):DisplayObject
{
var res:DisplayObject = target;
while(res && !(res is parentClass))
{
if(res.parent)
{
res = res.parent;
}
else
{
return null;
}
}
return res;
}
Test:
//create test display list: stage->movieclip->sprite
var sp:DisplayObject = (addChild(new MovieClip()) as MovieClip).addChild(new Sprite());
var st: Stage = dotParent(sp, Stage) as Stage;
trace(st);
var mc: MovieClip = dotParent(sp, MovieClip) as MovieClip;
trace(mc);
var sh: Shape = dotParent(sp, Shape) as Shape;
trace(sh);
output:
[object Stage]
[object MovieClip]
null

Related

why doesn't (if) work with no mistakes in code

my code is with AS3 ,( if-else) when i run it doesn't give me a mistake,but also doesn't give me the result of (if) that i want.
the ( tom) object has to be unvisible if touched another object called (food )else (tom) go back to ot's place
the idea is with ( drag and drop )
var hits = 0;
tom.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tom.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}
function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
var target = obj.dropTarget;
if (target == food && obj == tom) {
obj.visible=false;
} else {
obj.x=1051.05;
obj.y=135.05;
obj.stopDrag();
}
}
"When I run it doesn't give me a mistake, but also doesn't give me the result of if that I want."
That's because there is no error. You don't get a result because your IF condition is never reached. Think about example: if (obj == tom) Here what would ever make var obj be equal to var tom?
Solution:
I suspect food and tom are names, right? Try code to check by instance name...
function mouseUpHandler (evt:MouseEvent) :void
{
var obj = evt.target;
var target = obj.dropTarget;
if (target.name == "food" && obj.name == "tom")
{
obj.visible = false;
}
else
{
obj.x = 1051.05;
obj.y = 135.05;
obj.stopDrag();
}
}

how to call actionscript functions from flash timeline

I am using AS3, and i want to call function 'startMatchThree' which is in a MatchThree.as (external actionscript file) into the flahs main timeline actions.
here is my code:
package {
import flash.display.;
import flash.events.;
import flash.text.*;
import flash.utils.Timer;
public class MatchThree extends MovieClip {
// constants
static const numPieces:uint = 4;
static const spacing:Number = 45;
static const offsetX:Number = 30;
static const offsetY:Number = 50;
// game grid and mode
private var grid:Array;
private var gameSprite:Sprite;
private var firstPiece:Piece;
private var isDropping,isSwapping:Boolean;
private var gameScore:int;
// set up grid and start game
public function startMatchThree() {
// create grid array
grid = new Array();
for(var gridrows:int=0;gridrows<8;gridrows++) {
grid.push(new Array());
}
setUpGrid();
isDropping = false;
isSwapping = false;
gameScore = 0;
addEventListener(Event.ENTER_FRAME,movePieces);
}
public function setUpGrid() {
// loop until valid starting grid
while (true) {
// create sprite
gameSprite = new Sprite();
// add 64 random pieces
for(var col:int=0;col<8;col++) {
for(var row:int=0;row<8;row++) {
addPiece(col,row);
}
}
// try again if matches are present
if (lookForMatches().length != 0) continue;
// try again if no possible moves
if (lookForPossibles() == false) continue;
// no matches, but possibles exist: good board found
break;
}
// add sprite
addChild(gameSprite);
}
// create a random piece, add to sprite and grid
public function addPiece(col,row:int):Piece {
var newPiece:Piece = new Piece();
newPiece.x = col*spacing+offsetX;
newPiece.y = row*spacing+offsetY;
newPiece.col = col;
newPiece.row = row;
newPiece.type = Math.ceil(Math.random()*4);
newPiece.gotoAndStop(newPiece.type);
newPiece.select.visible = false;
gameSprite.addChild(newPiece);
grid[col][row] = newPiece;
newPiece.addEventListener(MouseEvent.CLICK,clickPiece);
return newPiece;
}
// player clicks on a piece
public function clickPiece(event:MouseEvent) {
var piece:Piece = Piece(event.currentTarget);
// first one selected
if (firstPiece == null) {
piece.select.visible = true;
firstPiece = piece;
// clicked on first piece again
} else if (firstPiece == piece) {
piece.select.visible = false;
firstPiece = null;
// clicked on second piece
} else {
firstPiece.select.visible = false;
// same row, one column over
if ((firstPiece.row == piece.row) && (Math.abs(firstPiece.col-piece.col) == 1)) {
makeSwap(firstPiece,piece);
firstPiece = null;
// same column, one row over
} else if ((firstPiece.col == piece.col) && (Math.abs(firstPiece.row-piece.row) == 1)) {
makeSwap(firstPiece,piece);
firstPiece = null;
// bad move, reassign first piece
} else {
firstPiece = piece;
firstPiece.select.visible = true;
}
}
}
// start animated swap of two pieces
public function makeSwap(piece1,piece2:Piece) {
swapPieces(piece1,piece2);
// check to see if move was fruitful
if (lookForMatches().length == 0) {
swapPieces(piece1,piece2);
} else {
isSwapping = true;
}
}
// swap two pieces
public function swapPieces(piece1,piece2:Piece) {
// swap row and col values
var tempCol:uint = piece1.col;
var tempRow:uint = piece1.row;
piece1.col = piece2.col;
piece1.row = piece2.row;
piece2.col = tempCol;
piece2.row = tempRow;
// swap grid positions
grid[piece1.col][piece1.row] = piece1;
grid[piece2.col][piece2.row] = piece2;
}
// if any pieces are out of place, move them a step closer to being in place
// happens when pieces are swapped, or they are dropping
public function movePieces(event:Event) {
var madeMove:Boolean = false;
for(var row:int=0;row<8;row++) {
for(var col:int=0;col<8;col++) {
if (grid[col][row] != null) {
// needs to move down
if (grid[col][row].y < grid[col][row].row*spacing+offsetY) {
grid[col][row].y += 5;
madeMove = true;
// needs to move up
} else if (grid[col][row].y > grid[col][row].row*spacing+offsetY) {
grid[col][row].y -= 5;
madeMove = true;
// needs to move right
} else if (grid[col][row].x < grid[col][row].col*spacing+offsetX) {
grid[col][row].x += 5;
madeMove = true;
// needs to move left
} else if (grid[col][row].x > grid[col][row].col*spacing+offsetX) {
grid[col][row].x -= 5;
madeMove = true;
}
}
}
}
// if all dropping is done
if (isDropping && !madeMove) {
isDropping = false;
findAndRemoveMatches();
// if all swapping is done
} else if (isSwapping && !madeMove) {
isSwapping = false;
findAndRemoveMatches();
}
}
// gets matches and removes them, applies points
public function findAndRemoveMatches() {
// get list of matches
var matches:Array = lookForMatches();
for(var i:int=0;i<matches.length;i++) {
var numPoints:Number = (matches[i].length-1)*50;
for(var j:int=0;j<matches[i].length;j++) {
if (gameSprite.contains(matches[i][j])) {
var pb = new PointBurst(this,numPoints,matches[i][j].x,matches[i][j].y);
addScore(numPoints);
gameSprite.removeChild(matches[i][j]);
grid[matches[i][j].col][matches[i][j].row] = null;
affectAbove(matches[i][j]);
}
}
}
// add any new piece to top of board
addNewPieces();
// no matches found, maybe the game is over?
if (matches.length == 0) {
if (!lookForPossibles()) {
endGame();
}
}
}
when I put 'startMatchThree' in the actions tab in timeline, it is showing this error message:
1180: Call to a possibly undefined method startMatchThree.
SO how to solve this error!!
Thank you for the help!! :)
startMatchThree() is a method of the MatchThree class, so you need a reference to an instance of MatchThree. Is there a symbol on the timeline somewhere which is linked to the MatchThree class? Then you can do something like this, where "matchThree" is the instance name of the symbol:
matchThree.startMatchThree();
Are you instantiating the MatchThree class in code? Then you just need to use that code reference, something like this,:
var matchThree:MatchThree = new MatchThree();
matchThree.startMatchThree();

#2007: Parameter format must be non-null?

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.

Turn this AS3 into an array and work with it

How can I turn this basic function method into an Array and call to easily. Pretty much I am just comparing if objects have become false and then do something...but this just seems like a lot of code for something so easy. any ideas?
var b:Boolean = true;
var i:Boolean = true;
var t:Boolean = true;
var a:Boolean = true;
var m:Boolean = true;
var ii:Boolean = true;
var n:Boolean = true;
var e:Boolean = true;
var s:Boolean = true;
level5_mc.let_b_mc.addEventListener(MouseEvent.CLICK, hitB);
level5_mc.let_i_mc.addEventListener(MouseEvent.CLICK, hitI);
level5_mc.let_t_mc.addEventListener(MouseEvent.CLICK, hitT);
level5_mc.let_a_mc.addEventListener(MouseEvent.CLICK, hitA);
level5_mc.let_m_mc.addEventListener(MouseEvent.CLICK, hitM);
level5_mc.let_ii_mc.addEventListener(MouseEvent.CLICK, hitII);
level5_mc.let_n_mc.addEventListener(MouseEvent.CLICK, hitN);
level5_mc.let_e_mc.addEventListener(MouseEvent.CLICK, hitE);
level5_mc.let_s_mc.addEventListener(MouseEvent.CLICK, hitS);
function hitB(event:MouseEvent){
b=false;
trace("good");
level5_mc.removeChild(level5_mc.let_b_mc);
}
function hitI(event:MouseEvent){
if (b==false){
i=false;
level5_mc.removeChild(level5_mc.let_i_mc);
}
else {
//decrease timer
i=true;
}
}
function hitT(event:MouseEvent){
if (b==false && i==false){
t=false;
level5_mc.removeChild(level5_mc.let_t_mc);
}
else {
//decrease timer
i=true;
}
}
and so forth... edited below
var b=level5_mc.let_b_mc;
var i=level5_mc.let_i_mc;
var t=level5_mc.let_b_mc;
var movieClips:Array = [b,i,t];
var movieClipFlags:Object = {
b:[],
i:[b],
t:[b,i]
};
for each(var mc:MovieClip in movieClips) {
mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}
function movieClipHit(e:MouseEvent) {
var mc:MovieClip = e.target as MovieClip;
if(readyToRemove(mc))
level5_mc.removeChild(mc);
else
trace("Can't remove yet.");
}
function readyToRemove(mc:MovieClip):Boolean {
for each(var mc:MovieClip in movieClipFlags[mc]) {
//if it has parent, it isn't removed yet.
if(mc.parent)
return false;
}
return true;
}
Something like the following should give you some ideas. I haven't tested the code you migt need to make some adjustments depending on your needs.
//put other movies clips in this array too.
var movieClips:Array = [level5_mc.let_b_mc,level5_mc.let_i_mc];
var movieClipFlags:Object = {
level5_mc.let_b_mc:[],
level5_mc.let_i_mc:[level5_mc.let_b_mc],
level5_mc.let_t_mc:[level5_mc.let_b_mc,level5_mc.let_i_mc]
//put other movie clips here too with the movie clips that need to be removed in the array
};
foreach(var mc:MovieClip in movieClips) {
mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}
private function movieClipHit(e:MouseEvent) {
var mc:MovieClip = e.target as MovieClip;
if(readyToRemove(mc))
level5_mc.removeChild(mc);
else
trace("Can't remove yet.");
}
private function readyToRemove(mc:MovieClip):Boolean {
foreach(var mc:MovieClip in movieClipFlags[mc]) {
//if it has parent, it isn't removed yet.
if(mc.parent)
return false;
}
return true;
}
Hope it helps.

as3 error loading "good" "bad" array

I am creating a "good" "bad" array to display on screen which i can later use simple if statements to do something upon if the player has collided with the "good" "bad" objects.
I cant get the objects to randomly generate on screen with the following code.
// Create and Set good/bad random Word objects
public function newObject(e:Event)
{
var goodObjects:Array = ["WordObject1"];
var badObjects:Array = ["WordObject2"];
if (Math.random() < .5)
{
var r:int = Math.floor(Math.random()*goodObjects.length);
var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
var newObject:MovieClip = new classRef();
newObject.typestr = "good";
} else
{
r = Math.floor(Math.random()*badObjects.length);
classRef = getDefinitionByName(badObjects[r]) as Class;
newObject = new classRef();
newObject.typestr = "bad";
}
newObject.x = Math.random();
newObject.y = Math.random();
addChild(newObject);
objects.push(newObject);
placeWords();
}
// create random Word objects
public function placeWords() {
objects = new Array();
for(var i:int=0;i<numWordObjects;i++) {
// loop forever
while (true) {
// random location
var x:Number = Math.floor(Math.random()*mapRect.width)+mapRect.x;
var y:Number = Math.floor(Math.random()*mapRect.height)+mapRect.y;
// check all blocks to see if it is over any
var isOnBlock:Boolean = false;
for(var j:int=0;j<blocks.length;j++) {
if (blocks[j].hitTestPoint(x+gamesprite.x,y+gamesprite.y)) {
isOnBlock = true;
break;
}
}
// not over any, so use location
if (!isOnBlock) {
newObject.x = x;
newObject.y = y;
newObject.gotoAndStop(Math.floor(Math.random()*1)+1);
gamesprite.addChild(newObject);
objects.splice(newObject);
break;
}
}
}
}
i get the following errors:
1119: Access of possibly undefined property x through a reference with static type Function.
1119: Access of possibly undefined property y through a reference with static type Function.
1067: Implicit coercion of a value of type Function to an unrelated type flash.display:DisplayObject.
Try renaming var x and var y to something else. Those are public properties in any class that extends as a display object (Sprite/MovieClip/Shape).