0

我是 cocossharp 的新手。我为 Visual Studio 安装了 cocossharp 模板,当我选择一个新的 cocossharp android 游戏并运行应用程序时,我得到的只是一个顶部带有徽标的黑屏。从代码中,我相信我应该得到一个带有标签的蓝屏

 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 时调用的方法中放置了一个断点,断点永远不会被命中。我尝试先创建 CCG​​ameView 然后注册事件处理程序,因为我认为事件在注册之前触发

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 项目,它可以完美运行。Iv 还尝试查看 Xamarin 上的各种示例,但它们都使用 Application Delegate,如果我没记错的话,这是旧的做事方式。如果有人可以提供帮助,我将不胜感激。谢谢

4

1 回答 1

0

这是一个模拟器问题,必须检查模拟器上的 Use Host GPU 选项。在我可以选择创建的模拟器的 Android 虚拟设备管理器上,我选择了我创建的模拟器,然后我没有启动它,而是首先编辑它,这就是我找到选项的地方(因为我已经创建了一些模拟器)。答案在这里

于 2017-05-03T14:43:24.153 回答