0

首先,我真的要感谢你到目前为止给我的所有帮助,因为我对 AS3 一无所知(只有基本的 gotoAnd 东西)我来到 Stackoverflow 搜索一些已经制作的代码,但我受到一些成员的鼓励自己编写代码,现在已经过去了将近 2 周,感谢很多伟大的人,我的足球点球比赛快结束了,我真的很喜欢这个地方。

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

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

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

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




}
  }

  }

我真的很感激你们,我保证我不会再打扰很长时间了

4

1 回答 1

1

1/3。
问题 1 和 3 是同一个问题。看起来您每次点击都试图从舞台上删除光标(removeChild)(因此在第一次点击后它会出错,因为它不再是任何东西的孩子)。您将其添加回您的延迟Action2 上,除非您的命中测试为真并且仅在 3 秒后才会运行。在初始化游戏时,您会创建一个全新的光标并将其添加到舞台上,这就是为什么您在第一场游戏后会得到一个副本。

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

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

  2. 我不明白你为什么要使用计时器或需要延迟你的功能,除了可能为踢动画留出时间?

您的代码非常杂乱无章,将函数命名为“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);
}
于 2012-08-19T21:42:15.517 回答