Actionscript 3 flash AS3游戏中的一些错误,经过大量的努力和帮助几乎完成了

Actionscript 3 flash AS3游戏中的一些错误,经过大量的努力和帮助几乎完成了,actionscript-3,flash,error-handling,Actionscript 3,Flash,Error Handling,首先,我真的很想感谢您迄今为止给我的所有帮助,因为我对AS3一无所知(仅限基础知识和内容),我来到Stackoverflow搜索一些已经制作的代码,但一些成员鼓励我自己制作代码,现在,在将近两周之后,感谢很多伟大的人,我的足球点球比赛几乎结束了,我真的很喜欢这个地方 我知道我必须处理一些碰撞和其他事情,因为目前的游戏不是最好的(记住我只是一个新手),但不幸的是,当通过反复玩来检查游戏功能时,我发现了以下几点: 1-当你得到3次失败时,游戏结束,在一些动画之后出现“再次播放”按钮,你点击它,一切似

首先,我真的很想感谢您迄今为止给我的所有帮助,因为我对AS3一无所知(仅限基础知识和内容),我来到Stackoverflow搜索一些已经制作的代码,但一些成员鼓励我自己制作代码,现在,在将近两周之后,感谢很多伟大的人,我的足球点球比赛几乎结束了,我真的很喜欢这个地方

我知道我必须处理一些碰撞和其他事情,因为目前的游戏不是最好的(记住我只是一个新手),但不幸的是,当通过反复玩来检查游戏功能时,我发现了以下几点:

1-当你得到3次失败时,游戏结束,在一些动画之后出现“再次播放”按钮,你点击它,一切似乎都很好,但是当你第二次继续播放时,你得到3次失败,当你点击按钮时,会出现一个新的光标???请帮忙 2-我尝试了数百万次使球以速度移动并为其轨迹设置动画,但未能成功,对此的任何帮助都将不胜感激。我有速度变量和重力,但我不知道如何使用它们

3-我遇到了一个与removeChild相关的actionscript错误,我多次尝试删除一些行,但无法修复

4-我使用的计时器太多了,我不知道这是否值得推荐

这是一个.fla文件,以防有人想尝试这个游戏(这真的很简单,因为这是我的第一个AS项目),想帮助我编写代码,帮助我改进游戏,如果有人不需要进入这个文件,这是代码(我知道这个地方充满了非常聪明的人),一旦我完成它,我知道我将能够用AS3做很多事情


var评分:编号;
变量角度:数值;
var速度:数字;
var游标:MovieClip;
var失败:编号;
var ballRotation:布尔值=false;
函数initializeGame():void
{ 
ball.x=296.35;
球y=353.35;
得分=0;
失败=0;
游标=新游标();
addChild(游标);
cursor.enabled=true;
鼠标隐藏();
stage.addEventListener(MouseEvent.MOUSE_MOVE,dragCursor);
stage.addEventListener(MouseEvent.CLICK,kick);
}
函数dragCursor(事件:MouseEvent):void
{
cursor.x=this.mouseX;
cursor.y=this.mouseY;
}
初始化名称();
var mouse=this.mouse;
功能启动(evt:事件)
{
removeChild(光标);
帕特阿多·麦克·play();
var定时器:定时器=新定时器(500,1);
timer.addEventListener(TimerEvent.timer,delayedAction);
timer.start();
函数delayedAction(e:TimerEvent)
{
移动球();
}
}
速度=-100000;
变量:int=100;
变量重力:数值=0.5;
函数moveBall()
{ 
var targetX:Number=mouseX;
变量targetY:Number=mouseY;
变量角度=数学atan2(targetY,targetX);
ball.x=mouseX+Math.cos(角度);
ball.y=mouseY+Math.sin(角度);
球旋转=真;
stage.removeEventListener(MouseEvent.CLICK,kick);
如果(ballRotation==真)
{
gotoAndStop(1+Math.floor(Math.random()*keeper.totalFrames));
球。玩();
}
if(ball.hitTestObject(守门员)){
ball.y=keeper.x-ball.height-ball.width;
痕迹(“托梅拉”);
}
如果(球击目标(守门员)和球y>69/*&&ball.y139和球x1/3.
问题1和3是相同的问题。看起来您每次单击都试图从阶段(removeChild)中删除光标(因此在第一次单击后它将出错,因为它不再是任何对象的子对象)。您将其添加回DelayedAction 2,除非命中测试为真,并且仅在3秒后才会运行。在初始化游戏时,您创建一个全新的光标并将其添加到阶段,这就是为什么您在第一场游戏后会得到一个重复的光标

与其移除光标,不如将其可见性设置为false/true,并且只创建一次

  • 为此,您需要使用EnterFrame处理程序、计时器或tween。我可以稍后发布一个示例

  • 我不明白你为什么要用定时器,或者需要延迟你的功能,除了给踢腿动画留出时间

  • 你的代码非常杂乱无章,命名函数“delayedAction”之类的东西很糟糕,因为它不能真正告诉你函数的用途。你在其他函数中也有太多的函数。下面是对你的代码进行的快速重构,我希望能教你一些东西。我还添加了用于球动画的tween

    import flash.events.Event;
    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    
    var score:Number;
    var cursor:MovieClip;
    var failed:Number;
    var ballRotation:Boolean = false;
    
    var ballTweenX:Tween;
    var ballTweenY:Tween;
    
    var targetCursor = new Cursor(); //only want one of these and you want it to exist the whole time so keep out here.
    
    addChild(targetCursor);
    
    
    initializeGame();
    
    function initializeGame( ):void
    { 
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
        stage.addEventListener(MouseEvent.CLICK, kick);
    
        ball.x = 296.35;
        ball.y = 353.35;
        score=0;
        failed=0;
    
        targetCursor.visible = true;
        Mouse.hide();
    }
    
    
    function dragCursor(event:MouseEvent):void
    {
        targetCursor.x = this.mouseX;
        targetCursor.y = this.mouseY;
    }
    
    function kick(evt:Event)
    {
        //removeChild(targetCursor); 
        targetCursor.visible = false; 
        pateador_mc.play();
    
        stage.removeEventListener(MouseEvent.CLICK, kick); //move this here, because you don't the option kick again while already kicking
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor); //added this, you probably don't want the target moving after the click...
        setTimeout(moveBall, 500);//cleaner and more efficient than using a timer for a one time delayed call.
    }
    
    function moveBall()
    { 
        var targetX:Number = mouseX;
        var targetY:Number = mouseY;
        var angle = Math.atan2(targetY,targetX);
    
        targetX =  mouseX + Math.cos(angle);
        targetY =  mouseY + Math.sin(angle) ;
    
        ballRotation = true;
    
        ballTweenX = new Tween(ball, "x", null, ball.x, targetX, .3, true);
        ballTweenY = new Tween(ball, "y", null, ball.y, targetY, .3, true);
    
        ballTweenY.addEventListener(TweenEvent.MOTION_FINISH, ballTweenDone,false,0,true);
    
        if (ballRotation==true)
        {
            keeper.gotoAndStop(1 + Math.floor(Math.random() * keeper.totalFrames));
            ball.play();
        }
    }
    
    function stopBallTween():void {
        ballTweenX.stop();
        ballTweenY.stop();
    }
    
    function ballTweenDone(e:TweenEvent):void {
        if (ball.hitTestObject ( keeper)){
            ball.y=keeper.x-ball.height- ball.width;
            trace ("Tomela");
        }
    
        if   (ball.hitTestObject(goalie) && ball.y>69 /*&& ball.y<178 && ball.X>139 && ball.x<466*/)
        {
            gol_mc.play();
            score ++;
            showScore();
        }else
        { 
            fails_mc.play();
            failed++;
            trace(failed);
    
            if (failed==3) {
               gameFinished();
               trace("YOU LOST");  
               return; //added this because you don't want the rest of this function running if it's a game over
            }
        }
    
        setTimeout(resetShot, 3000); //you had the code I put in resetShot repeated twice
    
        trace(score);
    }
    
    function resetShot():void {
        ball.x = 296.35;
        ball.y = 353.35;
        targetCursor.visible = true;
        keeper.gotoAndStop(1);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
        stage.addEventListener(MouseEvent.CLICK, kick);
    }
    
    function showScore():void{
        goles_txt.text ="" +score;  
    }
    
    function gameFinished(){
        gameOver.play();
        stage.removeEventListener(MouseEvent.CLICK, kick);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
        Mouse.show();
    
        //this.mouseX=cursor.x ;
        //this.mouseY=cursor.y;  //These are read only properties, your can't set the mouse position...
    
        again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain);
    }
    
    function playAgain(e:Event = null):void{
        gameOver.gotoAndPlay(31);
        fails_mc.gotoAndStop(1);
        keeper.play();
    
        setTimeout(initializeGame, 1000);
    }
    
    导入flash.events.Event;
    导入fl.transitions.Tween;
    导入fl.transitions.TweenEvent;
    var评分:数字;
    var游标:MovieClip;
    var失败:编号;
    var ballRotation:布尔值=false;
    var-ballTweenX:Tween;
    var ballTweenY:Tween;
    var targetCursor=new Cursor();//您只需要其中一个,并且您希望它一直存在,所以请远离这里。
    addChild(targetCursor);
    初始化名称();
    函数initializeGame():void
    { 
    stage.addEventListener(MouseEvent.MOUSE_MOVE,dragCursor);
    stage.addEventListener(MouseEvent.CLICK,kick);
    ball.x=296.35;
    球y=353.35;
    得分=0;
    失败=0;
    targetCursor.visible=true;
    鼠标隐藏();
    }
    函数dragCursor(事件:MouseEvent):void
    {
    targetCursor.x=this.mouseX;
    targetCursor.y=this.mouseY;
    }
    功能启动(evt:事件)
    {
    //removeChild(targetCursor);
    targetCursor.visible=false;
    帕特阿多·麦克·play();
    stage.removeEventListener(MouseEvent.CLICK,kick);//将其移到此处,因为您不会在已经启动时再次启动该选项
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragCursor);//添加了这个,您可能不希望在单击后移动目标。。。
    setTimeout(moveBall,500);//比使用计时器进行一次延迟调用更干净、更高效。
    }
    函数moveBall()
    { 
    var targetX:Number=mouseX;
    变量targetY:Number=mouseY;
    变量角度=数学atan2(目标,ta
    
    import flash.events.Event;
    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    
    var score:Number;
    var cursor:MovieClip;
    var failed:Number;
    var ballRotation:Boolean = false;
    
    var ballTweenX:Tween;
    var ballTweenY:Tween;
    
    var targetCursor = new Cursor(); //only want one of these and you want it to exist the whole time so keep out here.
    
    addChild(targetCursor);
    
    
    initializeGame();
    
    function initializeGame( ):void
    { 
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
        stage.addEventListener(MouseEvent.CLICK, kick);
    
        ball.x = 296.35;
        ball.y = 353.35;
        score=0;
        failed=0;
    
        targetCursor.visible = true;
        Mouse.hide();
    }
    
    
    function dragCursor(event:MouseEvent):void
    {
        targetCursor.x = this.mouseX;
        targetCursor.y = this.mouseY;
    }
    
    function kick(evt:Event)
    {
        //removeChild(targetCursor); 
        targetCursor.visible = false; 
        pateador_mc.play();
    
        stage.removeEventListener(MouseEvent.CLICK, kick); //move this here, because you don't the option kick again while already kicking
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor); //added this, you probably don't want the target moving after the click...
        setTimeout(moveBall, 500);//cleaner and more efficient than using a timer for a one time delayed call.
    }
    
    function moveBall()
    { 
        var targetX:Number = mouseX;
        var targetY:Number = mouseY;
        var angle = Math.atan2(targetY,targetX);
    
        targetX =  mouseX + Math.cos(angle);
        targetY =  mouseY + Math.sin(angle) ;
    
        ballRotation = true;
    
        ballTweenX = new Tween(ball, "x", null, ball.x, targetX, .3, true);
        ballTweenY = new Tween(ball, "y", null, ball.y, targetY, .3, true);
    
        ballTweenY.addEventListener(TweenEvent.MOTION_FINISH, ballTweenDone,false,0,true);
    
        if (ballRotation==true)
        {
            keeper.gotoAndStop(1 + Math.floor(Math.random() * keeper.totalFrames));
            ball.play();
        }
    }
    
    function stopBallTween():void {
        ballTweenX.stop();
        ballTweenY.stop();
    }
    
    function ballTweenDone(e:TweenEvent):void {
        if (ball.hitTestObject ( keeper)){
            ball.y=keeper.x-ball.height- ball.width;
            trace ("Tomela");
        }
    
        if   (ball.hitTestObject(goalie) && ball.y>69 /*&& ball.y<178 && ball.X>139 && ball.x<466*/)
        {
            gol_mc.play();
            score ++;
            showScore();
        }else
        { 
            fails_mc.play();
            failed++;
            trace(failed);
    
            if (failed==3) {
               gameFinished();
               trace("YOU LOST");  
               return; //added this because you don't want the rest of this function running if it's a game over
            }
        }
    
        setTimeout(resetShot, 3000); //you had the code I put in resetShot repeated twice
    
        trace(score);
    }
    
    function resetShot():void {
        ball.x = 296.35;
        ball.y = 353.35;
        targetCursor.visible = true;
        keeper.gotoAndStop(1);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
        stage.addEventListener(MouseEvent.CLICK, kick);
    }
    
    function showScore():void{
        goles_txt.text ="" +score;  
    }
    
    function gameFinished(){
        gameOver.play();
        stage.removeEventListener(MouseEvent.CLICK, kick);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
        Mouse.show();
    
        //this.mouseX=cursor.x ;
        //this.mouseY=cursor.y;  //These are read only properties, your can't set the mouse position...
    
        again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain);
    }
    
    function playAgain(e:Event = null):void{
        gameOver.gotoAndPlay(31);
        fails_mc.gotoAndStop(1);
        keeper.play();
    
        setTimeout(initializeGame, 1000);
    }