C# 试图找出如何在Cocossharp(BouncyGame)中暂停和恢复

C# 试图找出如何在Cocossharp(BouncyGame)中暂停和恢复,c#,cocossharp,C#,Cocossharp,所以我在玩弹跳游戏。我这样做是为了当你开始游戏时,你需要按下屏幕才能开始。每当你打新一轮比赛时,我也会执行这个。我试图在我的代码的底部重用这个,但它使它非常滞后 // Register for touch events var touchListener = new CCEventListenerTouchAllAtOnce(); touchListener.OnTouchesEnded = OnTouchesEnded; touchListen

所以我在玩弹跳游戏。我这样做是为了当你开始游戏时,你需要按下屏幕才能开始。每当你打新一轮比赛时,我也会执行这个。我试图在我的代码的底部重用这个,但它使它非常滞后

// 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)));
        }
    }
//为触摸事件注册
var touchListener=new CCEventListenerTouchAllAtOnce();
touchListener.ontouchsended=ontouchsended;
touchListener.ontouchsmoved=ontouchsended;
AddEventListener(touchListener,this);
}
void ontouchsended(列表触摸、CCEvent触摸事件)
{
如果(触摸次数>0)
{ 
附表(逻辑);
scoreLabel.Text=“分数:0”;
运行操作(新的CCMoveTo(.1f,新的CCPoint(触碰[0].Location.X,pailesprite.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;
        }
    }
}
}
使用系统;
使用System.Collections.Generic;
使用CocosSharp;
使用Microsoft.Xna.Framework;
名称空间CocosSharpGameTest
{
公共类内置层:CCLayerColor
{
//定义标签变量
CCLabel scoreLabel;
雪碧,雪碧,球雪碧;
public IntroLayer():base(CCColor4B.Black)
{
//创建并初始化标签
scoreLabel=新的CCLabel(“点击开始!”,“Arial”,80,CCLabelFormat.SystemFont);
//将标签作为子项添加到此图层
scoreLabel.PositionX=50;
scoreLabel.PositionY=1000;
scoreLabel.AnchorPoint=CCPoint.AnchorUpperLeft;
AddChild(分数标签);
paddleSprite=新的CCSprite(“paddle.png”);
AddChild(精灵);
ballSprite=新的CCSprite(“ball.png”);
AddChild(球精灵);
}
受保护的覆盖无效AddedToScene()
{
base.AddedToScene();
//使用边界布局我们可提取资产的定位
CCRect bounds=VisibleBundsWorldSpace;
//将标签放置在屏幕中央
桨叶Sprite.PositionX=100;
桨叶Sprite.PositionY=100;
ballSprite.PositionX=320;
ballSprite.PositionY=640;
//为触摸事件注册
var touchListener=new CCEventListenerTouchAllAtOnce();
touchListener.ontouchsended=ontouchsended;
touchListener.ontouchsmoved=ontouchsended;
AddEventListener(touchListener,this);
}
void ontouchsended(列表触摸、CCEvent触摸事件)
{
如果(触摸次数>0)
{ 
附表(逻辑);
scoreLabel.Text=“分数:0”;
运行操作(新的CCMoveTo(.1f,新的CCPoint(触碰[0].Location.X,pailesprite.PositionY));
}
}
浮球速度;
浮球率;
//每秒修改球的y速度的大小:
恒浮重力=140;
智力得分=0;
void RunGameLogic(float frameTimeInSeconds)
{
//这是一个线性近似值,因此不是100%准确
ballYVelocity+=帧时间不连续*-重力;
ballSprite.PositionX+=ballXVelocity*帧时间不连续;
ballSprite.PositionY+=ballYVelocity*帧时间不连续;
bool overlap=ballsrite.BoundingBoxTransformedToParent.IntersectsRect(桨叶sprite.BoundingBoxTransformedToParent);
bool movingDown=ballYVelocity<0;
if(重叠和向下移动)
{
ballYVelocity*=-1;
常量浮点最小速度=-300;
常量浮点maxXVelocity=300;
ballXVelocity=CCRandom.GetRandomFloat(minXVelocity,maxXVelocity);
分数++;
scoreLabel.Text=“分数:”+分数;
}
float ballRight=ballSprite.BoundingBoxTransformedToParent.MaxX;
float ballLeft=ballSprite.BoundingBoxTransformedToParent.MinX;
float screenRight=VisibleBundsworldSpace.MaxX;
float Screen Left=VisibleBundsworldSpace.MinX;
布尔应反射速度=
(ballRight>screenRight&&ballXVelocity>0)||
(ballLeft
提前谢谢

找到了! Cocossharp内置了一种“非计划”方法。 参考文献

我刚才补充说

计划外(RunGameLogic)

在我的RunGameLogic方法的基础上

if(ballSprite.PositionY

因此,一旦ballSprite超出边界,它将取消我在OnTouchSend方法中计划的内容。这意味着代码会回到监听触摸的状态

可能会犯一些错误,但这是我能找到的最好的方法,而且它是有效的