在动作脚本 3.0 中发生碰撞事件后完全删除对象

发布于 2024-12-22 17:16:14 字数 2352 浏览 1 评论 0原文

我有一个基本的游戏功能,每 500 毫秒掉落一个球(带有计时器事件),并假设当它击中在鼠标后面拖动的盘子时将其摆脱。事情是;我尝试使用 deleteChild(); 函数,但它仅删除了球对象的视觉外观,而没有停止其功能,这导致了几个问题:

  1. 球继续移动,并触发撞到地板的事件。
  2. 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:

  1. The ball keeps going, and triggers an event of hitting the floor.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-12-29 17:16:15

您的代码无法按照您的解释工作。它的工作原理是这样的:

你只有一个球,它每秒都会被抛出,参考 gameTime1 滴答调用 dropBall (),它执行以下操作:

  • 它替换你拥有的单个球到 0 位置,并在 1.2 秒内将其动画到底部。 var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);
  • 然后,score++ 会独立于球运动而触发,因为它位于 Enterframe 事件处理程序上,并且球击中板的每一帧得分都会增加
  • 您真的应该再次查看您的代码,并重新设计架构,它应该如何工作以及何时工作(基本上不是只有您需要知道该软件应该做什么,但您需要向软件解释这一点

更新:

有关如何将 movieclipt 导出到 oclass 中,请按照教程进行操作:
http://www.kirupa.com/developer/flashcs3/movieclips_classes_AS3_pg1.htm

然后在您的逻辑中:

Function ( createBall )

  • 创建球的实例。 (如果您遵循了教程,那么您知道如何)
  • 启动 EnterFrame,它会检查球是否击中面板或地面(如果确实停止了 EnterFrame 事件)。
  • 停止后根据所击中的内容决定要做什么

应该如下所示:

if ( hitPanel )
{
    increaseScrore ()
}
if ( hitFloor )
{
    decreaseLive () 
}
  • 在每个函数中 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:

  • it replaces the single ball you have to the 0 possition, and animating it withhin 1.2 seconds to the bottom. var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);
  • then the score++ is triggered independantly from the ball movement because it is on the enterframe event handler, and each frame the ball is hitting the plate the score increases
  • YOU SHOULD REALLY LOOK INTO YOUR CODE AGAIN, and rething the architecture, how what and when it should work ( basicaly not only you need to know, what the software should do, but you need to explain this to the software.

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 )

  • Create a instance of the ball. ( if you followed tutorial then you know how to )
  • start the enterFrame which checks if the ball hitted the panel or the ground if it did stop the enterFrame event.
  • After stop deside what to do depending on what was hitted

Should look like this:

if ( hitPanel )
{
    increaseScrore ()
}
if ( hitFloor )
{
    decreaseLive () 
}
  • in each of those functions increase... and decrease.. after they done everything should be removeBall function, and in increase.. then a call to the createBall 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 :)

︶葆Ⅱㄣ 2024-12-29 17:16:15

您是否尝试将球设置为 null

Did you try to set the ball to null ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文