0

我正在尝试根据本教程制作自定义动画/射击游戏:http: //flashadvanced.com/creating-small-shooting-game-as3/

通过定制,我的意思是定制的,即我自己的版本。

在它的动作脚本中,有一个定时器事件监听器,其功能是: timerHandler() 这个函数在舞台上添加和删除子“星”对象(用户必须射击):

if(starAdded){
removeChild(star);
}

和 :

addChild(star);

代码运行良好,但场景 2 出现错误。

该代码运行良好,我什至在通过 google 和 stackflow 向这个 flash 文件学习时添加了一些代码。我也添加了场景 2,并在 9 秒的电影时间后调用它。但是当它进入场景 2 时,它仍然显示星星对象,我无法从场景 2 中删除这些“星星”对象。

这是我添加的代码:

场景一:

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");
}

场景二:

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();

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

我是 as3 的新手,刚刚在 StackOverflow 上创建了一个帐户……尽管很长一段时间以来我已经知道并阅读了许多代码。

这是编辑后的新完整代码:

//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();
}

在第 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();

现在没有场景 2,在这个新的 .fla 文件中......这是我的 flash 文件的库属性的屏幕截图......:http: //i.imgur.com/d2cPyOx.jpg

4

2 回答 2

0

使用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");
    }

这将在进入场景 2 之前删除您在场景 1 中添加的所有孩子。

于 2013-05-14T05:31:57.490 回答
0

您最好完全放弃场景,它们在 AS3 中几乎已被弃用。相反,使用Game包含游戏内所有光标、星星和其他内容的对象,而不是使用gotoAndPlay()do removeChild(game); addChild(scoreboard);where "scoreboard" 是另一个将显示您的分数的容器类。

stage关于您的代码,您不再拥有实际包含您的代码的有效句柄star,因为您已经改变了场景。所以在调用gotoAndPlay()你的catchTimer函数之前做所有这些。

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");
}

场景 2 的代码将由一个单一的stop()- 直到您在那里添加一些东西。此外,不应该有任何事件侦听器,尤其是 enter-frame,没有删除该侦听器的代码!您在场景 2 上添加了一个输入帧侦听器并且永远不会删除它,而您需要该代码只运行一次。

于 2013-05-13T12:03:52.613 回答