在动作脚本 3.0 中发生碰撞事件后完全删除对象
我有一个基本的游戏功能,每 500 毫秒掉落一个球(带有计时器事件),并假设当它击中在鼠标后面拖动的盘子时将其摆脱。事情是;我尝试使用 deleteChild();
函数,但它仅删除了球对象的视觉外观,而没有停止其功能,这导致了几个问题:
- 球继续移动,并触发撞到地板的事件。
- dropBall();为落下的球设置动画的函数,实际上并不为新球设置动画。
这是完整的脚本:
//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();
I have a basic game function that drops a ball every 500 milliseconds (with a timer event) and suppose to get rid of it when it hits a plate that drags after the mouse. The thing is; I tried to use the deleteChild();
function, but it only removes the visual appearance of the ball object' without stopping its functionality, which causes a couple of problems:
- The ball keeps going, and triggers an event of hitting the floor.
- The dropBall(); function which animates the dropping ball, doesn't actually animate a new ball.
this is the complete script:
//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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码无法按照您的解释工作。它的工作原理是这样的:
你只有一个球,它每秒都会被抛出,参考
gameTime1
滴答调用 dropBall (),它执行以下操作:var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);
更新:
有关如何将 movieclipt 导出到 oclass 中,请按照教程进行操作:
http://www.kirupa.com/developer/flashcs3/movieclips_classes_AS3_pg1.htm
然后在您的逻辑中:
Function ( createBall )
应该如下所示:
increase...
和decrease..
完成后,一切都应该是 < code>removeBall 函数,然后在increase..
中调用createBall
,因此一切都开始重复。PS:无意冒犯,但你可以学习编程语言,在这种情况下,我看到你知道一点,但如果你缺少一些逻辑,你将无法在论坛上找到它......阅读一些有关逻辑/架构的基础知识信息应用程序等...请查看需要如何处理实例。
PSS 投资一些来了解 wtf 是 OOP。 ;)
最好的祝愿!节日快乐:)
Your'e code is not working as you explained. It is working like this:
You have only one ball, and it is beeing thorwn up, every second, refering to
gameTime1
tick calls dropBall (), which doing the following:var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);
UPDATE:
For how to export the movieclipt int oclass follow the tutorial:
http://www.kirupa.com/developer/flashcs3/movieclips_classes_AS3_pg1.htm
Then in your logics:
Function ( createBall )
Should look like this:
increase...
anddecrease..
after they done everything should beremoveBall
function, and inincrease..
then a call to thecreateBall
so everything is starting to repeat.P.S. No offence please, but you can learn the programming language, in this case i see you know a bit, but if you are missing some logics, you will not find it on forums... Read some basics info about logics / architectures of the applications etc... and please take a look how the instances needed to be handeled.
P.S.S. Invest some to to know wtf is OOP. ;)
best wishes! Happy holidays :)
您是否尝试将球设置为
null
?Did you try to set the ball to
null
?