2017-01-14 5 views
1

私はBouncyGameで遊んでいます。私はそれを作ったので、ゲームを始めるときにスクリーンを押す必要があります。私はあなたが新しいラウンドをプレイするたびにこれを実装したいと思います。私はこのコードをコードの一番下に再利用しようとしましたが、非常に遅いです。Cocossharp(BouncyGame)での一時停止と再開の方法を理解しようとしています

// Register for touch events 
     var touchListener = new CCEventListenerTouchAllAtOnce(); 
     touchListener.OnTouchesEnded = OnTouchesEnded; 

     touchListener.OnTouchesMoved = OnTouchesEnded; 
     AddEventListener(touchListener, this); 


    } 

    void OnTouchesEnded(List<CCTouch> touches, CCEvent touchEvent) 
    { 
     if (touches.Count > 0) 
     { 
      Schedule(RunGameLogic); 
      scoreLabel.Text = "Score: 0"; 
      paddleSprite.RunAction(new CCMoveTo(.1f, new CCPoint(touches[0].Location.X, paddleSprite.PositionY))); 
     } 
    } 

私はこれを行う方法はわかりませんが、0時間の結果で2時間試しました。どんな提案も大歓迎です。

ここに完全なコードがあります。

using System; 
using System.Collections.Generic; 
using CocosSharp; 
using Microsoft.Xna.Framework; 

namespace CocosSharpGameTest 
{ 
public class IntroLayer : CCLayerColor 
{ 

    // Define a label variable 
    CCLabel scoreLabel; 
    CCSprite paddleSprite, ballSprite; 

    public IntroLayer() : base(CCColor4B.Black) 
    { 

     // create and initialize a Label 
     scoreLabel = new CCLabel("Tap to GO!", "Arial", 80, CCLabelFormat.SystemFont); 

     // add the label as a child to this Layer 
     scoreLabel.PositionX = 50; 
     scoreLabel.PositionY = 1000; 
     scoreLabel.AnchorPoint = CCPoint.AnchorUpperLeft; 
     AddChild(scoreLabel); 

     paddleSprite = new CCSprite("paddle.png"); 
     AddChild(paddleSprite); 

     ballSprite = new CCSprite("ball.png"); 
     AddChild(ballSprite); 

    } 

    protected override void AddedToScene() 
    { 
     base.AddedToScene(); 

     // Use the bounds to layout the positioning of our drawable assets 
     CCRect bounds = VisibleBoundsWorldspace; 

     // position the label on the center of the screen 


     paddleSprite.PositionX = 100; 
     paddleSprite.PositionY = 100; 
     ballSprite.PositionX = 320; 
     ballSprite.PositionY = 640; 

     // Register for touch events 
     var touchListener = new CCEventListenerTouchAllAtOnce(); 
     touchListener.OnTouchesEnded = OnTouchesEnded; 

     touchListener.OnTouchesMoved = OnTouchesEnded; 
     AddEventListener(touchListener, this); 


    } 

    void OnTouchesEnded(List<CCTouch> touches, CCEvent touchEvent) 
    { 
     if (touches.Count > 0) 
     { 
      Schedule(RunGameLogic); 
      scoreLabel.Text = "Score: 0"; 
      paddleSprite.RunAction(new CCMoveTo(.1f, new CCPoint(touches[0].Location.X, paddleSprite.PositionY))); 
     } 
    } 
    float ballXVelocity; 
    float ballYVelocity; 
    // How much to modify the ball's y velocity per second: 
    const float gravity = 140; 

    int score = 0; 

    void RunGameLogic(float frameTimeInSeconds) 
    { 
     // This is a linear approximation, so not 100% accurate 
     ballYVelocity += frameTimeInSeconds * -gravity; 
     ballSprite.PositionX += ballXVelocity * frameTimeInSeconds; 
     ballSprite.PositionY += ballYVelocity * frameTimeInSeconds; 

     bool overlap = ballSprite.BoundingBoxTransformedToParent.IntersectsRect(paddleSprite.BoundingBoxTransformedToParent); 
     bool movingDown = ballYVelocity < 0; 
     if (overlap && movingDown) 
     { 
      ballYVelocity *= -1; 
      const float minXVelocity = -300; 
      const float maxXVelocity = 300; 
      ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); 

      score++; 
      scoreLabel.Text = "Score: " + score; 
     } 

     float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; 
     float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; 

     float screenRight = VisibleBoundsWorldspace.MaxX; 
     float screenLeft = VisibleBoundsWorldspace.MinX; 

     bool shouldReflectXVelocity = 
      (ballRight > screenRight && ballXVelocity > 0) || 
      (ballLeft < screenLeft && ballXVelocity < 0); 
     if (shouldReflectXVelocity) 
     { 
      ballXVelocity *= -1; 
     } 
     if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY) 
     { 
      ballSprite.PositionX = 320; 
      ballSprite.PositionY = 640; 

      ballXVelocity = 0; 
      ballYVelocity = 0; 
      ballYVelocity *= -1; 
      scoreLabel.Text = "Score: 0"; 
      score = 0; 
     } 
    } 
} 
} 

ありがとうございます!

答えて

0

それを実感してください! Cocossharpには「スケジュール解除」メソッドが組み込まれています。 Ref。 https://developer.xamarin.com/api/namespace/CocosSharp/

は、私はちょうどballSpriteが範囲外にあるいったんだから、私は予定何スケジューリングを解除します

if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY)

の下で非常に私のRunGameLogic方法のENで

Unschedule(RunGameLogic);

を追加しました私のOntouchesEndedメソッド。つまり、コードはタッチを聞くことに戻ります。

いくつかのエラーが発生している可能性がありますが、これはわかりやすく、うまくいきます。

関連する問題