我有一个基本的游戏功能,每 500 毫秒丢一个球(带有一个计时器事件),并且假设当它碰到一个在鼠标后拖动的盘子时会摆脱它。事情是; 我尝试使用该deleteChild();
功能,但它只会删除球对象的视觉外观而不会停止其功能,这会导致几个问题:
- 球继续前进,并触发撞击地板的事件。
- 落球();为落球设置动画的函数实际上并不为新球设置动画。
这是完整的脚本:
//imports:
import flash.events.Event;
import fl.transitions.Tween;
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;
//the function 'startGame, handles all basic game functions.
function startGame() {
//inisializes the variable 'Score' and gives it a value of 0.
var Score = 0;
//enter_frame listener. calls 'movePlate'
stage.addEventListener(Event.ENTER_FRAME, movePlate);
//function 'movePlate'. makes the plate follow the mouse
function movePlate(event:Event):void {
plate.x = mouseX;
}
//calls the dropBall function
dropBall()
//the function 'dropBall'. animates the droping ball
function dropBall() {
var oldpost = 0;
var randomNum:Number = Math.random() * 550;
var xAxis:int = Math.round(randomNum);
trace(randomNum);
trace(xAxis);
ball.x = xAxis;
base.x = xAxis;
var oldpost = xAxis;
var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);
oldpost = xAxis;
}
//function 'gameTime'. the timer function that controlls the intervals between falling eggs and ratio of good to bad eggs.
var gameTime1:Timer = new Timer(1000);
gameTime1.addEventListener(TimerEvent.TIMER, gameTimer1Function)
function gameTimer1Function(evt:TimerEvent):void {
dropBall();
}
gameTime1.start();
//enter frame event listener. calls 'checkCollision'
addEventListener(Event.ENTER_FRAME,checkCollision);
//function checl collision. checks if the ball hits the plate
function checkCollision(event: Event):void {
if(ball.hitTestObject(plate)) collisionDetected();
}
//function collision detected
function collisionDetected():void {
Score ++;
trace(Score);
scoreText.text = Score;
}
//enter frame event listener. calls 'checkGameOver'.
addEventListener(Event.ENTER_FRAME,checkGameOver);
//function 'checkGameOver.
function checkGameOver(event: Event):void {
if(ball.hitTestObject(floor)) gameOver();
}
//function 'gameOver'.
function gameOver():void {
trace('GAME OVER!!! Your Score Is: ' + Score + '.');
trace('Asta la Vista Baby :D');
}
}
startGame();