0

まず、AS3(基本的なgotoなどのみ)について何も知らなかったので、これまでに私に与えてくれたすべての助けに本当に感謝したいと思います。すでに作成されたコードを探してStackoverflowに来ましたが、一部のメンバーから次のように励まされました。自分でコードを作成します。2週間近く経ちましたが、多くの素晴らしい人々のおかげで、サッカーのペナルティーキックゲームはほぼ終了しました。この場所が大好きです。

現在のところゲームは最高ではないので(私は初心者であることを忘れないでください)、いくつかの衝突やその他の作業をしなければならないことは知っていますが、残念ながら、何度も何度もプレイしてゲームの機能を確認していると、次のことがわかりました:

1- 3回失敗するとゲームが終了し、アニメーションの後にもう一度再生ボタンが表示されます。それをクリックするとすべて問題ないように見えますが、2回目の再生を続けると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)からカーソルを削除しようとしているように見えます(したがって、最初のクリックの後で、カーソルは何の子でもなくなったため、エラーになります)。ヒットテストがtrueでない限り、3秒後にのみ実行されないdelayedAction2に追加し直します。ゲームの初期化では、まったく新しいカーソルを作成してステージに追加します。これが、最初のゲームの後に複製を取得する理由です。

カーソルを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 に答える