gpt4 book ai didi

c# - 试图弄清楚如何在 Cocossharp (BouncyGame) 中暂停和恢复

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:04 25 4
gpt4 key购买 nike

所以我正在玩 BouncyGame。我做到了,当你开始游戏时,你需要按下屏幕才能开始。每当你玩新一轮时,我也想实现这一点。我试图在我的代码底部重用这个 att,但它变得非常迟钝。

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

提前致谢!

最佳答案

想通了!Cocossharp 中内置了一个“Unschedule”方法。引用https://developer.xamarin.com/api/namespace/CocosSharp/

我刚刚添加了

Unschedule(RunGameLogic);

在我的 RunGameLogic 方法的最后

if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY)

因此,一旦 ballSprite 超出范围,它将取消安排我在 OntouchesEnded 方法中安排的内容。这意味着代码返回到监听触摸。

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

关于c# - 试图弄清楚如何在 Cocossharp (BouncyGame) 中暂停和恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com