我正在尝试制作自定义动画/射击游戏,来自本教程:http://flashadvanced.com/creating-small-shooting-game-as3/
根据自定义,我的意思是定制,即我自己的版本。
在它的动作脚本中,有一个带有函数的timer事件监听器:timerHandler() 此功能可在舞台上添加和删除儿童“星”对象(用户必须拍摄):
if(starAdded){
removeChild(star);
}
和:
addChild(star);
代码效果很好,我甚至在通过google和stackflow学习这个flash文件时添加了一些代码。我也添加了场景2,并在电影时间9秒后调用了它。但是当它进入场景2时,它仍然显示星形物体,我无法从场景2中删除这些“星形”物体。
场景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");
}
场景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();
}
//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
答案 0 :(得分:0)
你最好完全丢弃场景,在AS3中它们已经被弃用了。相反,使用包含游戏中所有光标,星标和其他内容的Game
对象,而不是gotoAndPlay()
执行removeChild(game); addChild(scoreboard);
,其中“记分板”是另一个容器类显示你的分数。
关于您的代码,您停止拥有实际包含stage
个star
的{{1}}的有效句柄,因为您已更改了场景。所以,在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()
- 直到你在那里添加一些东西。此外,应该没有事件侦听器,尤其是输入框架,没有删除该侦听器的代码!您在场景2上添加了一个输入框架侦听器,并且从不删除它,而您需要该代码只运行一次。
答案 1 :(得分: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上添加的所有孩子。