2 votes

Essayer de comprendre comment mettre en pause et reprendre dans Cocossharp (BouncyGame)

Je m'amuse avec le BouncyGame. J'ai fait en sorte que lorsque vous démarrez le jeu, vous devez appuyer sur l'écran pour qu'il démarre. Je voudrais implémenter cela chaque fois que vous jouez un nouveau tour aussi. J'ai essayé de réutiliser cette attache au bas de mon code, mais il a fait extrêmement laggy.

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

Je ne sais pas comment faire, j'ai essayé pendant 2 heures sans résultat. Toute suggestion est la bienvenue.

Voici le code complet.

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

Merci d'avance !

1voto

Gustav Eiman Points 37

J'ai trouvé ! Il existe une méthode "Unschedule" intégrée à Cocossharp. Réf. https://developer.xamarin.com/api/namespace/CocosSharp/

Je viens d'ajouter

Unschedule(RunGameLogic);

à la toute fin de ma méthode RunGameLogic, sous la rubrique

if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY)

Ainsi, une fois que le ballSprite est en dehors des limites, il désordonnera ce que j'ai planifié dans ma méthode OntouchesEnded. Cela signifie que le code revient à l'écoute des touches.

J'ai peut-être fait quelques erreurs, mais c'est le mieux que j'ai pu faire et ça marche !

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X