我是cocossharp的新手。我为visual studio安装了cocossharp模板,当我选择一个新的cocossharp安卓游戏并运行应用程序时,我得到的只是一个顶部带有徽标的黑色屏幕。从代码中,我相信我应该得到一个带有标签的蓝屏
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our game view from the layout resource,
// and attach the view created event to it
CCGameView gameView = (CCGameView)FindViewById(Resource.Id.GameView);
gameView.ViewCreated += LoadGame;
}
void LoadGame(object sender, EventArgs e)
{
CCGameView gameView = sender as CCGameView;
if (gameView != null)
{
var contentSearchPaths = new List<string>() { "Fonts", "Sounds" };
CCSizeI viewSize = gameView.ViewSize;
int width = 1024;
int height = 768;
// Set world dimensions
gameView.DesignResolution = new CCSizeI(width, height);
// Determine whether to use the high or low def versions of our images
// Make sure the default texel to content size ratio is set correctly
// Of course you're free to have a finer set of image resolutions e.g (ld, hd, super-hd)
if (width < viewSize.Width)
{
contentSearchPaths.Add("Images/Hd");
CCSprite.DefaultTexelToContentSizeRatio = 2.0f;
}
else
{
contentSearchPaths.Add("Images/Ld");
CCSprite.DefaultTexelToContentSizeRatio = 1.0f;
}
gameView.ContentManager.SearchPaths = contentSearchPaths;
CCScene gameScene = new CCScene(gameView);
gameScene.AddLayer(new GameLayer());
gameView.RunWithScene(gameScene);
}
}
public class GameLayer : CCLayerColor
{
// Define a label variable
CCLabel label;
public GameLayer() : base(CCColor4B.Blue)
{
// create and initialize a Label
label = new CCLabel("Hello CocosSharp", "Fonts/MarkerFelt", 22, CCLabelFormat.SpriteFont);
// add the label as a child to this Layer
AddChild(label);
}
protected override void AddedToScene()
{
base.AddedToScene();
// Use the bounds to layout the positioning of our drawable assets
var bounds = VisibleBoundsWorldspace;
// position the label on the center of the screen
label.Position = bounds.Center;
// Register for touch events
var touchListener = new CCEventListenerTouchAllAtOnce();
touchListener.OnTouchesEnded = OnTouchesEnded;
AddEventListener(touchListener, this);
}
void OnTouchesEnded(List<CCTouch> touches, CCEvent touchEvent)
{
if (touches.Count > 0)
{
// Perform touch handling here
}
}
}
我在触发事件ViewCreated时调用的方法中设置了一个断点,断点永远不会被触发。我首先尝试创建CCGameView,然后注册eventhandler,因为我认为事件在注册之前被触发
CCGameView gameView = new CCGameView(this);
gameView.ViewCreated += LoadGame;
gameView = (CCGameView)FindViewById(Resource.Id.GameView);
然后我尝试直接调用LoadGame方法
CCGameView gameView = (CCGameView)FindViewById(Resource.Id.GameView);
gameView.ViewCreated += LoadGame;
LoadGame(gameView, EventArgs.Empty);
但是这导致了gameView.ContentManager的空例外。
我唯一的另一个怀疑是模拟器本身,也许它需要额外安装的东西,但是对于正常的xamarin android项目它完美地工作。我也试过看Xamarin上的各种例子,但他们都使用Application Delegate,如果我没弄错的话,那就是旧的做事方式。如果有人可以提供帮助,我很感激。感谢
答案 0 :(得分:0)
这是一个模拟器问题,必须检查模拟器上的Use Host GPU选项。在我可以选择我创建的模拟器的Android虚拟设备管理器上,我选择了一个我创建的模拟器,然后我开始编辑它,而不是启动它,我找到了选项(因为我已经创建了一些选项)模拟器)。 The answer is here