2017-01-14 21 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))); 
     } 
    } 

Я понятия не имею, как это сделать, пробовал в течение 2 часов с 0 результатами. Любые предложения приветствуются.

Вот полный код.

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 встроен метод «Нераспределенный». Реф. https://developer.xamarin.com/api/namespace/CocosSharp/

Я просто добавил

Unschedule(RunGameLogic);

на очень еп моего метода RunGameLogic под

if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY)

Так как только ballSprite выходит за пределы границ будут запланированным то, что я Запланированный в мой метод OntouchesEnded. Это означает, что код возвращается к прослушиванию прикосновений.

Возможно, возникли некоторые ошибки, но это как можно лучше понять, и это работает!