闪存AS3游戏中的一些错误,几乎完成了很多努力和帮助收到

时间:2012-08-18 21:06:24

标签: actionscript-3 flash error-handling

首先,我真的要感谢你迄今为止给予我的所有帮助,因为我对AS3一无所知(基础知识只有东西)我来到Stackoverflow搜索已经制作的代码但我受到了鼓励一些成员自己制作代码,现在差不多2周后,感谢很多优秀人才,我的足球点球比赛几乎完成了,我真的很喜欢这个地方。

我知道我必须处理一些碰撞和其他事情,因为目前游戏并不是最好的(记住我只是一个新手),但不幸的是,通过一遍又一遍地重复检查游戏功能,我有发现了以下内容:

1-当你得到3失败,然后游戏结束并且在一些动画之后出现再次播放按钮,你点击它并且一切似乎都很好,但是当你第二次继续播放时你达到3失败,当你点击按钮出现一个新的光标???请帮忙 2-我尝试了数百万次让球快速移动并使其轨迹生动但却无法成功,对此的任何帮助都将受到高度赞赏。我有速度变量和重力,但我不知道如何使用它们

3-我收到与removeChild相关的动作脚本错误,我多次尝试删除一些行,但我无法修复它。

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

这是.fla文件https://rapidshare.com/files/1702748636/kicks.fla,以防任何人想要尝试游戏(这很简单,因为它是我的第一个AS项目)并想帮助我修改代码并帮助我改进游戏,如果有人不需要进入文件(这个地方充满了非常聪明的人),这里是代码,一旦我完成它,我知道我将能够用AS3做很多事情。


  var score:Number;
  var angle:Number;
  var speed:Number;
  var cursor:MovieClip;
  var failed:Number;
  var ballRotation:Boolean = false;
  function initializeGame( ):void
  { 

        ball.x = 296.35;
        ball.y = 353.35;
score=0;
failed=0;
cursor = new Cursor();

addChild(cursor);
cursor.enabled = true;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
stage.addEventListener(MouseEvent.CLICK, kick);
  }


  function dragCursor(event:MouseEvent):void
  {
cursor.x = this.mouseX;
cursor.y = this.mouseY;
  }

  initializeGame();
  var mouse = this.Mouse;





  function kick(evt:Event)
  {
    removeChild(cursor);

pateador_mc.play();

var timer:Timer = new Timer(500,1);
timer.addEventListener(TimerEvent.TIMER, delayedAction);
timer.start();
function delayedAction(e:TimerEvent)
{
    moveBall();
}
  }
  speed=-100000;
  var ease:int = 100;
  var gravity:Number = 0.5;
  function moveBall()
  { 
var targetX:Number = mouseX;
var targetY:Number = mouseY;
var angle = Math.atan2(targetY,targetX);
ball.x =  mouseX + Math.cos(angle);
ball.y =  mouseY + Math.sin(angle) ;
ballRotation = true;
stage.removeEventListener(MouseEvent.CLICK, kick);

if (ballRotation==true)
{


  keeper.gotoAndStop(1 + Math.floor(Math.random() * keeper.totalFrames));
  ball.play();


}

   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();
    var timer3:Timer = new Timer(3000,1);
    timer3.addEventListener(TimerEvent.TIMER, delayedAction3);
    timer3.start();
    function delayedAction3(e:TimerEvent)
    {
        ball.x = 296.35;
        ball.y = 353.35;
        stage.addEventListener(MouseEvent.CLICK, kick);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
                addChild(cursor);
                keeper.gotoAndStop(1);

    }

}

else
{ 
    fails_mc.play();
     failed++;
    var timer2:Timer = new Timer(3000,1);
    timer2.addEventListener(TimerEvent.TIMER, delayedAction2);
    timer2.start();
    function delayedAction2(e:TimerEvent)
    {
        ball.x = 296.35;
        ball.y = 353.35;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
            stage.addEventListener(MouseEvent.CLICK, kick);
            addChild(cursor);
            keeper.gotoAndStop(1);

    }

    trace(failed);


   if (failed==3) {
       gameFinished();


       trace("YOU LOST");



       }
}
  function showScore():void{
goles_txt.text ="" +score;

}
  trace (score);

    function gameFinished(){
 gameOver.play ();
 stage.removeEventListener(MouseEvent.CLICK, kick);
 stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
   timer2.stop();
           Mouse.show();

            this.mouseX=cursor.x ;
             this.mouseY=cursor.y;

           again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain);


  }

  function playAgain():void{
        gameOver.gotoAndPlay(31);
        fails_mc.gotoAndStop(1);
        keeper.play();


var timer4:Timer = new Timer(1000,1);
    timer4.addEventListener(TimerEvent.TIMER, delayedAction3);
    timer4.start();
    function delayedAction3(e:TimerEvent)
    {
    initializeGame();




}
  }

  }

我真的很感激他们,我保证很长一段时间不会再烦恼了

1 个答案:

答案 0 :(得分:1)

1/3。
问题1&amp; 3是同样的问题。看起来你试图在每次点击时从舞台上删除光标(removeChild)(所以在第一次点击后它会出错,因为它不再是任何东西的孩子)。你将它添加回你的delayedAction2上,除非你的命中测试是真的并且仅在3秒之后才会运行。在初始化游戏时,你创建一个全新的游标并将其添加到舞台上,这就是你在第一场比赛后得到重复的原因。

而不是removeChild光标,最好只将其可见性设置为false / true并仅创建一次。

  1. 您需要为此使用EnterFrame处理程序,计时器或补间。我可以稍后发布一个例子。

  2. 我无法弄清楚你为什么要使用计时器或需要延迟你的功能,除非是为了给动作动画留出时间?

  3. 你的代码非常混乱,命名函数像'delayedAction'这样的东西很糟糕,因为它并没有真正告诉你任何关于函数的用途。您在其他功能中也有太多功能。这是一个快速重构您的代码我已经做过希望教一些事情。我还为球动画添加了补间。

    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);
    }