Question

I'm trying to make a custom animated/shooter, from this tutorial: http://flashadvanced.com/creating-small-shooting-game-as3/

By custom, I mean customized, ie, my own version of it.

In it's actionscript, there's a timer event listener with function: timerHandler() This function adds and removes child "star" objects on the stage (which the user has to shoot at):

if(starAdded){
removeChild(star);
}

and :

addChild(star);

Code works great, but error occurs on scene 2.

The code works great, and I even added some code while learning via google and stackflow to this flash file. I added Scene 2 to it too, and had it called after 9 seconds of movie time. But when it goes to Scene 2, it still displays the star objects and I've been unable to remove these "star" object(s) from Scene 2.

Here's the code I added:

SCENE 1:

var my_timer = new Timer(5000,0); //in milliseconds
my_timer.addEventListener(TimerEvent.TIMER, catchTimer);
my_timer.start();


var myInt:int = getTimer() * 0.001;
var startTime:int = getTimer();
var currentTime:int = getTimer();
var timeRunning:int = (currentTime - startTime) * 0.001; // this is how many seconds the game has been running.
demo_txt.text = timeRunning.toString();

function catchTimer(e:TimerEvent)
{
    gotoAndPlay(1, "Scene 2");
}

SCENE 2:

addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
    timer.stop();
    timer.removeEventListener(TimerEvent.TIMER, timerHandler);
    stage.removeChild(star);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
    my_timer.stop(); // you might need to cast this into Timer object
    my_timer.removeEventListener(TimerEvent.TIMER, catchTimer);
    Mouse.show();
}
stop();

=====================================================

I'm quite new as3, and have just created an account on StackOverflow... even though I've known and read many codes on it since quite some time.

Here's the Edited New Complete Code:

//importing tween classes
import fl.transitions.easing.*;
import fl.transitions.Tween;

//hiding the cursor
Mouse.hide();

//creating a new Star instance
var star:Star = new Star();
var game:Game = new Game();
//creating the timer
var timer:Timer = new Timer(1000);
//we create variables for random X and Y positions
var randomX:Number;
var randomY:Number;
var t:int = 0;
//variable for the alpha tween effect
var tween:Tween;
//we check if a star instance is already added to the stage
var starAdded:Boolean = false;
//we count the points
var points:int = 0;

//adding event handler on mouse move
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
//adding event handler to the timer
timer.addEventListener(TimerEvent.TIMER, timerHandler);
//starting the timer
timer.start();
addChild(game);
function cursorMoveHandler(e:Event):void{
    //sight position matches the mouse position
    game.Sight.x = mouseX;
    game.Sight.y = mouseY;
}

function timerHandler(e:TimerEvent):void{
    //first we need to remove the star from the stage if already added
    if(starAdded){
        removeChild(star);
    }

    //positioning the star on a random position
    randomX = Math.random()*500;
    randomY = Math.random()*300;
    star.x = randomX;
    star.y = randomY;
    //adding the star to the stage
    addChild(star);
    //changing our boolean value to true
    starAdded = true;
    //adding a mouse click handler to the star
    star.addEventListener(MouseEvent.CLICK, clickHandler);
    //animating the star's appearance
    tween = new Tween(star, "alpha", Strong.easeOut, 0, 1, 3, true);
t++;
if(t>=5) {
    gotoAndPlay(5);
}
}

function clickHandler(e:Event):void{
    //when we click/shoot a star we increment the points
    points ++;
    //showing the result in the text field
    points_txt.text = points.toString();
}

And on Frame 5 :

//timer.stop();
    //timer.removeEventListener(TimerEvent.TIMER, timerHandler);
    // uncomment lines above if "timer" is something you've made
    stage.removeChild(star);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
    timer.stop(); // you might need to cast this into Timer object
    timer.removeEventListener(TimerEvent.TIMER, timerHandler);
    Mouse.show();
    stop();

There's no Scene 2 now, in this new .fla file... Here's a screenshot of the library property of my flash file...: http://i.imgur.com/d2cPyOx.jpg

Was it helpful?

Solution

You'd better drop scenes altogether, they are pretty much deprecated in AS3. Instead, use Game object that contains all the cursor, stars and other stuff that's inside the game, and instead of going with gotoAndPlay() do removeChild(game); addChild(scoreboard); where "scoreboard" is another container class that will display your score.

Regarding your code, you stop having a valid handle to stage that actually contains that star of yours, because your have changed the scene. So do all of this before calling gotoAndPlay() in your catchTimer function.

function catchTimer(e:TimerEvent)
{
    //timer.stop();
    //timer.removeEventListener(TimerEvent.TIMER, timerHandler);
    // uncomment lines above if "timer" is something you've made
    stage.removeChild(star);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
    my_timer.stop(); // you might need to cast this into Timer object
    my_timer.removeEventListener(TimerEvent.TIMER, catchTimer);
    Mouse.show();
    gotoAndPlay(1, "Scene 2");
}

And the code for Scene 2 will consist of a single stop() - until you'll add something there. Also, there should be no event listeners, especially enter-frame, without a code to remove that listener! You add an enter-frame listener on Scene 2 and never remove it, while you need that code to only run once.

OTHER TIPS

Use getChildAt.


    function catchTimer(e:TimerEvent)
    {
        var childCount:int = this.numChildren - 1;
        var i:int;
        var tempObj:DisplayObject;
        for (i = 0; i < childCount; i++)
        {
        tempObj = this.getChildAt(i);
        this.removeChild(tempObj);
        }
        gotoAndPlay(1, "Scene 2");
    }

This will remove all the children you added on scene 1 before going to scene 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top