Skip navigation
Bombza
Currently Being Moderated

Function doesn't carry out second time of asking

Apr 2, 2013 7:27 AM

Tags: #help #flash #as3 #function #cs6

i have created a game on flash cs6 where you can retry if you die however one of my functions that creates the things they have to catch doesnt work after you play the game more than once...

 

Why is the function doing this and how can I make the function carry out an infinite number of times as long as the player is retrying it.

 

Also I am using AS3.

 

Here is the code:

import flash.utils.Timer;

import flash.events.TimerEvent;

import flash.events.Event;

import flash.events.KeyboardEvent;

 

 

 

 

stop();

 

 

var flakeArray:Array = new Array();

 

 

var snowflakeTimer:Timer = new Timer (500, 0);

snowflakeTimer.addEventListener(TimerEvent.TIMER, moveSnowflake);

snowflakeTimer.start();

 

 

var direction:Number;

var speed:Number = 20;

var score:Number = 0;

 

 

function moveSnowflake(e:Event){                                 

          var snowflake:Snow = new Snow();                                  

          var newX:Number = Math.random ()*500;                        

 

          snowflake.x = newX;

          addChild (snowflake);

          flakeArray.push(snowflake);

}

function checkForSnowInBasket():void {

          for each(var thisFlake:Snow in flakeArray){

                    if(thisFlake.hitTestObject(basket)){

                              score = score + 1;

                              scoreDisplay.text = "Score: "+score;

                              removeChild(thisFlake);

                              flakeArray.splice(flakeArray.indexOf(thisFlake), 1);

                    }

          }

}

 

 

var basketTimer:Timer = new Timer(20, 0);

basketTimer.addEventListener(TimerEvent.TIMER, moveBasket);

basketTimer.start();

 

 

function moveBasket(t:TimerEvent):void {

switch(direction){

                    case 37:

                              basket.x = basket.x - speed;

                              break;

                    case 39:

                              basket.x = basket.x + speed;

                              break;

}

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, logDirection);

function logDirection (key:KeyboardEvent): void {

          trace(key.keyCode) ;

          direction = key.keyCode;

 

 

stage.addEventListener(KeyboardEvent.KEY_UP, logStop);

function logStop (key:KeyboardEvent): void {

          trace(key.keyCode) ;

          direction = 0;

 

 

}

}

var girlTimer:Timer = new Timer(20, 0);

girlTimer.addEventListener(TimerEvent.TIMER, moveGirl);

girlTimer.start();

 

 

function moveGirl(t:TimerEvent):void {

          switch(direction){

                    case 37:

                              basket.x = basket.x - speed;

                              break;

                    case 39:

                              basket.x = basket.x + speed;

                              break;

          }

          checkForSnowInBasket();

          if(basket.hitTestObject(leftIce)){

                    snowflakeTimer.stop ();

                    girlTimer.stop();

                    stopSnow ();

                    gotoAndPlay (5);

          }

          if(basket.hitTestObject(rightIce)){

                    snowflakeTimer.stop ();

                    girlTimer.stop();

                    stopSnow ();

                    gotoAndPlay (5);

          }

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, girlDirection);

function girlDirection (key:KeyboardEvent): void {

          trace(key.keyCode) ;

          direction = key.keyCode;

}

stage.addEventListener(KeyboardEvent.KEY_UP, girlStop);

function girlStop (key:KeyboardEvent): void {

          trace(key.keyCode) ;

          direction = 0;

}

function stopSnow() :void {

          trace("FLAKEARRAY:"+flakeArray.length);

          for each(var thisFlake:Snow in flakeArray){

                              trace('removing flake');

                              removeChild(thisFlake);

          }

          flakeArray = [];

}

 

This is the Function that doesn't seem to be carried out after the user has retried the game... I'm not too sure why the function doesn't get carried out a second time... so any help would be extremely useful!

 

 

 

Thanks in advance!

 
Replies
  • kglad
    72,253 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 2, 2013 7:45 AM   in reply to Bombza

    is that code in a class file?

     

    is that function being called when you replay?  (ie, use the trace function to confirm one way or the other.)

     
    |
    Mark as:
  • kglad
    72,253 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 2, 2013 9:01 AM   in reply to Bombza

    i don't see any way to determine what happens when the game is replayed.

     

    for example, are you executing snowflakeTimer.start() when the game is replayed?

     
    |
    Mark as:
  • kglad
    72,253 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 2, 2013 11:09 AM   in reply to Bombza

    you shouldn't execute all that code again.  only execute the code that needs to execute like your timer start()'s and stage listeners (assuming you remove those somewhere).

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points