So, the same college project as last time, and this time im having a trouble with undefined methods. The entirity of my code is below
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
}
and the problems I'm getting are:
1180: Call to a possibly undefined method match.
I have very little knowledge with AS3, and have spent more than 2 hours trying to resolve this problem. Thanks in advance guys, PS any links to tutorials etc are massively appreciated
You misplaced close braces'}' of the function onGuessButtonClick_2. Use the below code and check it.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
Hope it helps.
Your match() function is inside the onGuessButtonClick_2(MouseEvent) function.
So when you try it from anywhere else that is not the inside of the onGuessButtonClick_2 function, flash gets confused because it cannot find the function match() because it is only callable inside the onGuessButtonClick_2() function.
1180 errors are caused by Flash being dead.
1180 Error: Flash is Dead, please convert your code to HTML5 or do it natively.
okthxbai
Adobe
Related
I have written a drag and drop game and, for the most part, it works.
It runs and does what I need it to do.
However, I cannot figure out two things.
How to remove the toy objects from the screen and re-add them after game over.
How to remove the event listeners for dragging/collision action after game over has initiated.
At the moment, after score is displayed you can still drop items in the toybox and make the score rise even when game over has been displayed.
Does anyone have any idea what I am doing wrong?
I would love to figure this out but it is driving me crazy.
Any help would be great.
Here is my code ...
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.Font;
import flash.filters.GlowFilter;
public class MainGame extends MovieClip {
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var tryAgain:TryAgain = new TryAgain;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000, 30);
var myText:TextField = new TextField;
var myText2:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var myTextFormat2:TextFormat = new TextFormat;
var font1:Font1 = new Font1;
var kids:Kids = new Kids;
var count:int = 0;
var finalScore:int = 0;
var score:Number = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
public function mainGame():void
{
trace("HI");
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChildAt(kids, 2);
kids.x = 310;
kids.y = 200;
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
}
public function bgScroll (e:Event)
{
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
e.target.addEventListener(Event.ENTER_FRAME, collision);
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var m:int=0; m< toyArray.length; m++)
{
(toyArray[m] as MovieClip).x -=BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int=0; j< toyArray.length; j++)
{
(toyArray[j] as MovieClip).x +=BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
} // End of BGScroll
public function collision (e:Event)
{
for (var l:int=0; l< toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x=100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
if (score == 200)
{
timerDone();
myTimer.stop();
}
}
}
public function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
public function arrayDrop(e:MouseEvent)
{
stopDrag();
}
public function resetGame(e:Event):void {
trace("CLICK");
countDown = 30;
myText.text = "0" + countDown.toString();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.reset();
removeChild(tryAgain);
myText2.visible = false;
score = 0;
}
public function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
if (countDown < 10)
{
myText.text = "0" + countDown.toString();
myText.x = 270;
displayText();
}
else if (countDown < 20 && countDown > 9)
{
myText.text = countDown.toString();
myText.x = 280;
displayText();
}
else
{
myText.text = countDown.toString();
myText.x = 270;
displayText();
}
} // end of countdown function
public function displayText():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 50, myText.y = 10;
myTextFormat.size = 50, myTextFormat.font = font1.fontName;
myText.setTextFormat(myTextFormat);
}
public function displayText2():void
{ myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
addChild(myText2);
myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;
myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;
myText2.setTextFormat(myTextFormat2);
}
public function timerDone(e:TimerEvent=null):void
{
if (countDown == 0)
{
count = 0;
finalScore = score;
}
else
{
count = (30) - (myTimer.currentCount);
finalScore = (count * 10) + (score);
}
myText.text = "GAME OVER!";
myText.x = 195;
displayText();
myText2.text = "Your score = " + (finalScore);
displayText2();
addChild(tryAgain);
tryAgain.x = 300;
tryAgain.y = 300;
tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
}
} // End of class
} //End of package
Nevermind ... solved it.
Easiest was was to change
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
To
function addToys(xpos:int, ypos:int)
{
stage.addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
And then, in the timerDone function I added thisif statement ...
for (var m = 0; m < toyArray.length; m++) {
if (stage.contains(toyArray[m])) {
stage.removeChild(toyArray[m]);
}
Worked a treat!
I have now figured out that After I restart my game The Enter_frame function is not looping? why is that.
Enter_Frame:
public function enter_frame(e:Event)
{
trace("rGame = " + rGame);
_Menu();
if(mGameStart)
{
//Stage & Players
_PlayerStart();
_PlayerCon();
_MoveStage();
}
if(rGame)
{
_PlayerCon();
trace("_PlayerCon(): Done");
_MoveStage();
}
if(Game_Over)
{
mGameStart = false;
GameOver();
}
}
Pipe_Spawn:
private function eSpawn()
{
for(var i:int = 0; i < ePipeMax; i++)
{
var _Pipe:EPipe = new EPipe;
_Pipe.x = (i * 250) + 640;
_Pipe.y = Math.random() * 90;
PipeLayer.addChild(_Pipe);
}
}
Pipe_Move:
public function _MoveStage():void
{
//Pipes Move
for (var i:int; i < vPipeMax; i++)
{
var _Pipe = PipeLayer.getChildAt(i);
if(_Pipe.hitTestPoint(_Player.x, _Player.y, true))
{
blink = true;
Remove(_White);
Game_Over = true;
}
else
{
if(_Pipe.x < 241 && _Pipe.x > 234){
ScoreReady = true;
}
else
{
ScoreReady = false;
two = true;
}
if(ScoreReady && two)
{
Score++;
Scores.text = Score.toString();
two = false;
}
if(_Pipe.x <= -20)
{
_Pipe.x = 640 + 100;
_Pipe.y = Math.random()*90;
}
_Pipe.x -= xSpeed;
}
}
}
Game_Restart:
public function restartButton(m:MouseEvent)
{
GOSign.y = 1000;
R_Button.y = 1000;
Scores.y = 105.65;
HighScore.x = 500;
ScoreReady = false;
two = true;
HighScore5();
_Player.x = 240;
_Player.y = 320;
//This is where I restart the position of the pipes
for (var i:int = 0; i < vPipeMax; i++)
{
var _Pipe = PipeLayer.getChildAt(i);
_Pipe.x = (i * 250) + 640;
_Pipe.y = Math.random() * 90;
}
rGame = true;
}
Iv been at this for about two weeks and can not fix it. Thank you for reading.
I have found my problem. It was a VERY stupid mistake and I cant believe it took me this long to figure it out. I forgot to reset a variable ~facepalm~. I guess you learn from your mistakes lol
Iv managed to get a somewhat working procedural generated terrain. But.. for some reason my tiles get placed 1 square to the left on the screen on the second row on, and then makes 2 extra tiles at the end :D
Im sure it has something to do with
tile.y += TILE_SIZE *(Math.floor(tilesInWorld.length/ROW_LENGTH));
tile.x = TILE_SIZE * (tilesInWorld.length-(ROW_LENGTH*Math.floor(tilesInWorld.length/ROW_LENGTH)));
but im not sure...
heres the code for the generated terrain tho
package
{
import flash.display.*;
import flash.events.Event;
public class World
{
protected var tilesInWorld:Vector.<MovieClip> = new Vector.<MovieClip>();
public var worldTiles:Sprite;
protected var tile:MovieClip;
protected var TILE_SIZE = 25;
protected var MAP_WIDTH = 800;
protected var MAP_HEIGHT = 600;
protected var ROW_LENGTH = (MAP_WIDTH/TILE_SIZE)+1;
protected var COL_LENGTH = (MAP_HEIGHT/TILE_SIZE);
protected var MAX_WATER = 100;
protected var waterTiles;
protected var nextTile:String;
protected var maxTiles = ROW_LENGTH * COL_LENGTH;
protected var waterChain:int;
protected var waterBuffer:int;
public function World(parentMC:MovieClip)
{
waterBuffer = Math.random()*(maxTiles-MAX_WATER);
waterTiles = 0;
waterChain = 5;
nextTile = "";
parentMC.addEventListener(Event.ENTER_FRAME, update);
worldTiles = new Sprite();
parentMC.addChild(worldTiles);
}
protected function generateTile()
{
if (tilesInWorld.length > 0)
{
if (waterTiles >= MAX_WATER)
{
tile = new Grass();
nextTile = "grass";
trace(waterTiles);
}
else
{
if(tilesInWorld.length + 1 < waterBuffer){
nextTile = "grass";
}
for (var i:int = 0; i < tilesInWorld.length; i++)
{
if (tilesInWorld[i].x == (tilesInWorld[tilesInWorld.length-1].x + TILE_SIZE) && tilesInWorld[i].y == (tilesInWorld[tilesInWorld.length-1].y-TILE_SIZE) && tilesInWorld[i].type == "water")
{
nextTile = "water";
}
}
if (nextTile == "grass")
{
tile = new Grass();
nextTile = "";
waterChain = 0;
}
else if (nextTile == "water")
{
waterTiles += 1;
waterChain += 1;
tile = new Water();
if (waterChain > 5)
{
nextTile = "";
}
else
{
nextTile = "water";
}
}
else
{
if (Math.random() * 1 >= 0.25)
{
waterChain = 0;
tile = new Grass();
nextTile = "grass";
}
else
{
waterTiles += 1;
waterChain += 1;
tile = new Water();
nextTile = "water";
}
}
}
}
else
{
if (Math.random() * 1 >= 0.5)
{
waterChain = 0;
nextTile = "grass";
tile = new Grass();
}
else
{
waterTiles += 1;
waterChain += 1;
nextTile = "water";
tile = new Water();
}
}
this is where the actual coding for placing the X and Y potion of the tiles is located, in other words, this is the place where i believe the error is at
tilesInWorld.push(tile);
tile.width = TILE_SIZE;
tile.height = TILE_SIZE;
worldTiles.x = 0-(tile.width/2);
worldTiles.y = 0+(tile.height/2);
tile.x = TILE_SIZE * tilesInWorld.length;
if (tile.x >= MAP_WIDTH)
{
tile.y += TILE_SIZE * (Math.floor(tilesInWorld.length/ROW_LENGTH));
tile.x = TILE_SIZE * (tilesInWorld.length-(ROW_LENGTH*Math.floor(tilesInWorld.length/ROW_LENGTH)));
}
worldTiles.addChild(tile);
}
protected function allowTile():Boolean
{
//if()
if (tilesInWorld.length > maxTiles)
{
return false;
}
return true;
}
protected function update(e:Event)
{
if (allowTile())
{
generateTile();
}
}
}
}
heres a link to the game for you can actually see what it is doing
http://www.fastswf.com/p13LrYA
just realized you cant see what its doing because its off the screen lolol :D
Also, if anyone could help make a better way of making random lakes in the terrain?
But thanks ahead of time for the help!
Change
protected var ROW_LENGTH = (MAP_WIDTH/TILE_SIZE); //remove the +1
Remove
if (tile.x >= MAP_WIDTH)
and you can calculate a tiles x,y from its index (which you can get using tilesInWorld.length before they are added) using:
tile.x = TILE_SIZE * (tilesInWorld.length % ROW_LENGTH);
tile.y = TILE_SIZE * Math.floor(tilesInWorld.length / ROW_LENGTH);
I have made a game on Flash CS6 using AS3. The game has a spaceship on the right hand of the screen and it shoots bullets at aliens that randomly appear on the right. The game is all working perfectly but every now and then when I play it i get this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at alien3/pulse()[/Users/Matt/Documents/DES B Assignment/Prototype/alien3.as:43]
Now here is the AS linkage for alien 3
package {
import flash.display.MovieClip;
import flash.events.Event;
public class alien3 extends MovieClip {
var yMove:Number = 15;
var changeDirectionAfterYMoves:Number = 0;
var moveCount:Number = 0;
var shootCount:Number = 0;
var shootMissilesAfterYMoves:Number = 0;
public function alien3() {
addEventListener(Event.ENTER_FRAME,pulse);
}
public function stopListening()
{
removeEventListener(Event.ENTER_FRAME,pulse);
}
public function pulse(evt:Event)
{
if (currentFrame!=1) return;
if(changeDirectionAfterYMoves == moveCount)
{
yMove=yMove*-1;
var maxMoves:Number;
if (yMove>0)maxMoves = Math.round (400-this.y)/yMove;
else maxMoves = Math.round((this.y)/Math.abs(yMove));
changeDirectionAfterYMoves = 1 + Math.round (Math.random () *maxMoves);
moveCount = 0;
}
if (shootCount==shootMissilesAfterYMoves)
{
var ao:alienMissile3 = new alienMissile3 ();
ao.x = this.x + 50;
ao.y = this.y;
parent.addChild(ao);
shootCount=0;
shootMissilesAfterYMoves = 1 + Math.round (Math.random() * 25)
}
this.y+=yMove;
moveCount = moveCount+1;
shootCount = shootCount+1;
}
}
}
So line 43 responds to the alien missile movie clip in the library
parent.addChild(ao);
Which is strange because it adds the alien missiles without a problem and they work fine
The alienMissile3 is a movie clip which was converted to a movie clip from a png which is named something different.
I have no idea what is causing this error.
The code in the main timeline is as follows.
import flash.display.MovieClip;
import flash.events.Event;
import flash.media.SoundChannel;
import flash.net.dns.AAAARecord;
var frameCount:Number = 0;
var alienCount:Number = 0;
var alienInterval:Number = 100;
var score:Number=0;
var gameOn:Boolean = false
var my_sound:laserGun = new laserGun();
var my_channel:SoundChannel = new SoundChannel();
var my_sound3:QueenAmidalaandTheNabooPalace = new QueenAmidalaandTheNabooPalace();
var my_channel3:SoundChannel = new SoundChannel();
var spaceshipMovement:Number = 0;
mcGameOverScreen.visible=false;
var scoresArray:Array = new Array();
var SO:SharedObject = SharedObject.getLocal("scores5");
if (SO.size!=0)
{
scoresArray = SO.data.scoresArray;
}
function startGame()
{
addEventListener(Event.ENTER_FRAME, pulse);
gameOn=true;
mySpaceship.livesLeft=3;
score=0;
}
function pulse(event:Event):void
{
if (mySpaceship.livesLeft<1) noLivesLeft();
addNewAlienIfNecessary();
/*addNewAlien2IfNecessary();
addNewAlien3IfNecessary();*/
checkForMissileOnAlien();
checkForMissileOnAlien2();
checkForMissileOnAlien3();
checkForMissileOnAlienMissile();
checkForMissileOnAlienMissile2();
checkForMissileOnAlienMissile3();
tidyUp();
checkForAlienMissileOnSpaceship();
checkForAlienMissile2OnSpaceship();
checkForAlienMissile3OnSpaceship();
tbScore.text = String(score);
tbLivesLeft.text = String (mySpaceship.livesLeft);
var bg:background = new background();
bg.y = 0;
bg.x = 0;
addChild(bg);
gameOn=true;
}
function addNewAlienIfNecessary()
{
if (score<=100 && alienCount<3 && frameCount%60==0)
{
var a:alien = new alien();
a.x = Math.random()*380;
a.y = Math.random()*50;
addChild(a);
alienCount=alienCount+1;
}
//frameCount++;
if (score >100 && score<500 && alienCount<3 && frameCount%80==0)
{
var aa:alien2 = new alien2();
aa.x = Math.random()*380;
aa.y = Math.random()*50;
addChild(aa);
alienCount=alienCount+1;
}
//frameCount++;
if (alienCount<3 && score>=500 && frameCount%100==0)
{
var ab:alien3 = new alien3();
ab.x = Math.random()*380;
ab.y = Math.random()*50;
addChild(ab);
alienCount=alienCount+1;
}
frameCount++;
}
/*function addNewAlien2IfNecessary()
{
if (score>100 && score<500 && alienCount<3 && frameCount%60==0)
{
var aa:alien2 = new alien2();
aa.x = Math.random()*440;
aa.y = Math.random()*50;
addChild(aa);
alienCount=alienCount+1;
}
frameCount++;
}
function addNewAlien3IfNecessary()
{
if (score>500 && alienCount<3 && frameCount%60==0)
{
var ab:alien3 = new alien3();
ab.x = Math.random()*440;
ab.y = Math.random()*50;
addChild(ab);
alienCount=alienCount+1;
}
frameCount++;
}*/
function checkForMissileOnAlien()
{
var missilez:Array = new Array;
var alienz:Array = new Array;
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alien) {alienz.push(getChildAt(i) as MovieClip)};
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
}
for (var j=0;j<alienz.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienz[j].hitTestObject(missilez[k]))
{
alienz[j].gotoAndStop (2);
//alienz[j].stopListening();
missilez[k].gotoAndStop(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienz[j].x;
e.y = alienz[j].y;
addChild(e);
alienCount = alienCount-1;
score += 20;
}
}
}
}
function checkForMissileOnAlien2()
{
var missilez:Array = new Array;
var alienz2:Array = new Array;
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alien2) {alienz2.push(getChildAt(i) as MovieClip)};
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
}
for (var j=0;j<alienz2.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienz2[j].hitTestObject(missilez[k]))
{
alienz2[j].gotoAndStop (2);
//alienz2[j].stopListening();
missilez[k].gotoAndStop(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienz2[j].x;
e.y = alienz2[j].y;
addChild(e);
alienCount = alienCount-1;
score += 50;
}
}
}
}
function checkForMissileOnAlien3()
{
var missilez:Array = new Array;
var alienz3:Array = new Array;
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alien3) {alienz3.push(getChildAt(i) as MovieClip)};
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
}
for (var j=0;j<alienz3.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienz3[j].hitTestObject(missilez[k]))
{
alienz3[j].gotoAndStop (2);
//alienz3[j].stopListening();
missilez[k].gotoAndStop(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienz3[j].x;
e.y = alienz3[j].y;
addChild(e);
alienCount = alienCount-1;
score += 100;
}
}
}
}
function checkForMissileOnAlienMissile()
{
var alienMissilez:Array = new Array();
var missilez:Array = new Array();
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile) {alienMissilez.push(getChildAt(i) as MovieClip);}
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
}
trace("alienMissilez" + alienMissilez.length);
trace("missilez" + missilez.length);
for (var j=0;j<alienMissilez.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienMissilez[j].hitTestObject(missilez[k]))
{
alienMissilez[j].gotoAndPlay(2);
alienMissilez[j].stopListening();
missilez[k].gotoAndPlay(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienMissilez[j].x;
e.y = alienMissilez[j].y;
addChild(e);
score += 10;
}
}
}
}
function checkForMissileOnAlienMissile2()
{
var alienMissilez2:Array = new Array();
var missilez:Array = new Array();
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile2) {alienMissilez2.push(getChildAt(i) as MovieClip);}
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
}
trace("alienMissilez2" + alienMissilez2.length);
trace("missilez" + missilez.length);
for (var j=0;j<alienMissilez2.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienMissilez2[j].hitTestObject(missilez[k]))
{
alienMissilez2[j].gotoAndPlay(2);
alienMissilez2[j].stopListening();
missilez[k].gotoAndPlay(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienMissilez2[j].x;
e.y = alienMissilez2[j].y;
addChild(e);
score += 15;
}
}
}
}
function checkForMissileOnAlienMissile3()
{
var alienMissilez3:Array = new Array();
var missilez:Array = new Array();
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile3) {alienMissilez3.push(getChildAt(i) as MovieClip);}
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
}
trace("alienMissilez3" + alienMissilez3.length);
trace("missilez" + missilez.length);
for (var j=0;j<alienMissilez3.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienMissilez3[j].hitTestObject(missilez[k]))
{
alienMissilez3[j].gotoAndPlay(2);
alienMissilez3[j].stopListening();
missilez[k].gotoAndPlay(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienMissilez3[j].x;
e.y = alienMissilez3[j].y;
addChild(e);
score += 20;
}
}
}
}
/*function tidyUp()
{
for (var j=numChildren-1;j>=0;j--)
{
if (getChildAt(j) is TextField) continue; //This added to help text box work
var mc:MovieClip = getChildAt(j) as MovieClip;
if (getChildAt(j) is explosion)
{
if (mc.currentFrame==15) removeChildAt(j);
continue;
}
//if (mc.x>800 || mc.x<0 || mc.currentFrame!=1 ) removeChildAt(j);
}
}
*/
function tidyUp()
{
for (var j=numChildren-1;j>=0;j--)
{
if (getChildAt(j) is TextField) continue;
if (getChildAt(j) is SimpleButton) continue;
var mc:MovieClip = getChildAt(j) as MovieClip;
if (getChildAt(j) is explosion)
{
if (mc.currentFrame==10) removeChildAt(j);
//continue;
}
if (getChildAt(j) is alienMissile)
{
if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
//continue;
}
if (getChildAt(j) is alienMissile2)
{
if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
//continue;
}
if (getChildAt(j) is alienMissile3)
{
if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
//continue;
}
if(getChildAt(j) is missile)
{
if(mc.x<0||(mc.currentFrame==2)) removeChildAt(j);
//continue;
}
if(getChildAt(j) is alien)
{
if(mc.currentFrame!=1) removeChildAt(j);
//continue;
}
if(getChildAt(j) is alien2)
{
if(mc.currentFrame!=1) removeChildAt(j);
//continue;
}
if(getChildAt(j) is alien3)
{
if(mc.currentFrame!=1) removeChildAt(j);
//continue;
}
}
}
function checkForAlienMissileOnSpaceship()
{
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile)
{
if (getChildAt(i).hitTestObject(mySpaceship))
{
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.currentFrame==1)
{
mc.gotoAndPlay(2);
mc.stopListening();
var e:explosion = new explosion();
e.x = mc.x;
e.y = mc.y;
addChild(e);
mySpaceship.isHit();
}
}
}
}
}
function checkForAlienMissile2OnSpaceship()
{
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile2)
{
if (getChildAt(i).hitTestObject(mySpaceship))
{
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.currentFrame==1)
{
mc.gotoAndPlay(2);
mc.stopListening();
var e:explosion = new explosion();
e.x = mc.x;
e.y = mc.y;
addChild(e);
mySpaceship.isHit();
}
}
}
}
}
function checkForAlienMissile3OnSpaceship()
{
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile3)
{
if (getChildAt(i).hitTestObject(mySpaceship))
{
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.currentFrame==1)
{
mc.gotoAndPlay(2);
mc.stopListening();
var e:explosion = new explosion();
e.x = mc.x;
e.y = mc.y;
addChild(e);
mySpaceship.isHit();
}
}
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, spaceshipControls);
function spaceshipControls (event:KeyboardEvent):void
{
if (event.keyCode==38 && mySpaceship.y>+62 && stage.frameRate==24) {mySpaceship.y-=20;}
if (event.keyCode==40 && mySpaceship.y<480-mySpaceship.height && stage.frameRate==24) {mySpaceship.y+=20;}
if (event.keyCode==32 && gameOn==true && stage.frameRate==24)
{
var m:missile = new missile();
m.y = mySpaceship.y;
m.x = mySpaceship.x - 80;
addChild(m);
my_channel = my_sound.play();
}
}
btn_up.addEventListener(MouseEvent.MOUSE_DOWN, goUp);
btn_up.addEventListener(MouseEvent.MOUSE_UP, stopMoving);
function goUp(evt:MouseEvent)
{
if (mySpaceship.y>+62 && stage.frameRate==24)
{
mySpaceship.y-=20;
}
else
{
spaceshipMovement=0;
}
}
function stopMoving (evt:MouseEvent)
{
spaceshipMovement=0;
}
btn_down.addEventListener(MouseEvent.MOUSE_DOWN, goDown);
btn_down.addEventListener(MouseEvent.MOUSE_UP, stopMoving2);
function goDown(evt:MouseEvent)
{
if (mySpaceship.y<480-mySpaceship.height && stage.frameRate==24)
{
mySpaceship.y+=20;
}
else
{
spaceshipMovement=0;
}
}
function stopMoving2 (evt:MouseEvent)
{
spaceshipMovement=0;
}
btn_fire.addEventListener(MouseEvent.MOUSE_DOWN, fire);
function fire(evt:MouseEvent)
{
var m:missile = new missile();
m.y = mySpaceship.y;
m.x = mySpaceship.x - 80;
addChild(m);
my_channel = my_sound.play();
}
function noLivesLeft()
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, spaceshipControls);
btn_fire.removeEventListener(MouseEvent.MOUSE_DOWN, fire);
flash.media.SoundMixer.stopAll();
my_channel3 = my_sound3.play();
removeEventListener(Event.ENTER_FRAME,pulse);
for (var i=0;i<numChildren;i++)
{
//trace(i);
if (getChildAt(i) is TextField) continue;
if (getChildAt(i) is SimpleButton) continue;
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.hasEventListener(Event.ENTER_FRAME)) mc.stopListening();
}
gameOn = false;
mcGameOverScreen.visible = true;
mcGameOverScreen.tb_score.text = String (score);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, spaceshipControls);
btn_fire.removeEventListener(MouseEvent.MOUSE_DOWN, fire);
for (var c=numChildren-1;c>=0;c--)
{
var d = getChildAt(c);
if (d is alien || d is alienMissile|| d is explosion || d is missile || d is alien2 || d is alien3 ||d is alienMissile2 ||d is alienMissile3 || d is background) removeChildAt(c);
}
}
btnResume.addEventListener(MouseEvent.MOUSE_DOWN, resumeGame);
function resumeGame(e:MouseEvent):void
{
stage.frameRate = 24
btnPause.visible=true;
btnResume.visible=false;
}
btnPause.addEventListener(MouseEvent.MOUSE_DOWN, pauseGame);
function pauseGame(e:MouseEvent):void
{
stage.frameRate = 0
btnPause.visible=false;
btnResume.visible=true;
}
function setMute(vol)
{
var sTransform:SoundTransform = new SoundTransform(1,0);
sTransform.volume = vol;
SoundMixer.soundTransform = sTransform;
}
var isMuted:Boolean = false;
muteBtn.addEventListener(MouseEvent.CLICK,toggleMuteBtn);
function toggleMuteBtn(event:Event){
if(isMuted)
{
isMuted = false;
setMute(1);
}
else
{
isMuted = true;
setMute(0);
}
}
You are getting the error because you are checking for Alien3's parent before it has been added to the display list.
Add a trace before the parent.addChild(ao) line, ie:
trace("Alien3 parent = "+parent);
I'm betting the output will be null sometimes.
The solution is to replace the ENTER_FRAME event listener (in the Alien3 constructor function) with an ADDED_TO_STAGE event listener. Try this:
public function alien3() {
addEventListener(Event.ADDED_TO_STAGE, onStage);
}
private function onStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onStage);
addEventListener(Event.ENTER_FRAME, pulse);
}
Now, when pulse is called, 'parent' will not be null because the instance is on the display list and has access to 'parent'.
I'm creating Flash "memory" game, Idea to discover 2 equal cards. But when I choose 1, if second card is wrong It discover only for half second, but this is not enough. I need to make that second card will be shown for 2-3 seconds. How can I add timer?
Here is part of code:
else
{
trace("wrong");
_message = "wrong";
message_txt.text = _message;
_firstCard.gotoAndPlay("flipBack");
event.currentTarget.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = undefined;
}
Thank you for answers.
UPDATED WITH FULL CODE
package
{
import flash.display.MovieClip;
import Card;
import Boarder;
import BlueBoard;
import flash.events.MouseEvent;
import RedBoard;
import Snow;
public class MemoryGame extends MovieClip
{
private var _card:Card;
private var _boarder:Boarder;
private var _blueBoard:BlueBoard;
private var _cardX:Number;
private var _cardY:Number;
private var _firstCard:*;
private var _totalMatches:Number;
private var _currentMatches:Number;
private var _redBoard:RedBoard;
private var _snow:Snow;
private var _cards:Array;
public var _message:String;
public function MemoryGame()
{
_cards = new Array();
_totalMatches = 4;
_currentMatches = 0;
createCards();
}
private function createCards():void
{
_cardX = 45;
_cardY = 10;
for(var i:Number = 0; i < 2; i++)
{
_card = new Card();
addChild(_card);
_boarder = new Boarder();
_card.setType(_boarder);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
_cards.push(_card);
}
for(var j:Number = 0; j < 2; j++)
{
_card = new Card();
addChild(_card);
_blueBoard = new BlueBoard();
_card.setType(_blueBoard);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
_cards.push(_card);
}
_cardX = 45;
_cardY = _card.height + 30;
for(var k:Number = 0; k < 2; k++)
{
_card = new Card();
addChild(_card);
_redBoard = new RedBoard();
_card.setType(_redBoard);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
_cards.push(_card);
}
for(var l:Number = 0; l < 2; l++)
{
_card = new Card();
addChild(_card);
_snow = new Snow();
_card.setType(_snow);
_card.x = _cardX;
_card.y = _cardY;
_cardX += _card.width + 50;
_card.addEventListener(MouseEvent.CLICK, checkCards);
_cards.push(_card);
}
randomizeCards(_cards);
}
private function checkCards(event:MouseEvent):void
{
event.currentTarget.removeEventListener(MouseEvent.CLICK, checkCards);
if(_firstCard == undefined)
{
_firstCard = event.currentTarget;
}
else if(String(_firstCard._type) == String(event.currentTarget._type))
{
trace("match");
_message = "match";
message_txt.text = _message;
_firstCard = undefined;
_currentMatches ++;
if(_currentMatches >= _totalMatches)
{
trace("YOU WIN !!!");
_message = "YOU WIN !!!";
message_txt.text = _message;
}
}
else
{
trace("wrong");
_message = "wrong";
message_txt.text = _message;
_firstCard.gotoAndPlay("flipBack");
event.currentTarget.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = undefined;
}
}
private function randomizeCards(cards:Array):void
{
var randomCard1:Number;
var randomCard2:Number;
var card1X:Number;
var card1Y:Number;
for(var i:Number = 0; i < 10; i++)
{
randomCard1 = Math.floor(Math.random() * cards.length);
randomCard2 = Math.floor(Math.random() * cards.length);
card1X = cards[randomCard1].x;
card1Y = cards[randomCard1].y;
cards[randomCard1].x = cards[randomCard2].x;
cards[randomCard1].y = cards[randomCard2].y
cards[randomCard2].x = card1X;
cards[randomCard2].y = card1Y;
}
}
}
}
You can use http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html. BTW, do note that Timer constructor's default 2nd parameter is 0 implying that the timer would run indefinitely. Be sure to mark that as 1 for your case.
You need to move code in your 'else' block to another function and setup the timer instead in the else block.
The final code would look something like this:
else
{
trace("wrong");
_message = "wrong";
message_txt.text = _message;
_secondCard = event.currentTarget;//You need to have this variable defined alongwith _firstCard
var timer:Timer = new Timer(2000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack);
timer.start();
}
and the function would have:
protected function flipBack(event:TimerEvent):void
{
_firstCard.gotoAndPlay("flipBack");
_sedondCard.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
_secondCard.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = _secondCard = undefined;
}
HTH.
If it's only to wait a certain amount of time, you can use setTimeout (flash.utils)
http://livedocs.adobe.com/flash/9.0_fr/ActionScriptLangRefV3/flash/utils/package.html#setTimeout()