将 MVC 与 libgdx scene2d 一起使用 [英] Using MVC with libgdx scene2d

查看:21
本文介绍了将 MVC 与 libgdx scene2d 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 libgdx 开发游戏.这实际上是我的第二次尝试,因为当我开始第一次尝试时,我并不了解 libgdx.在我的第一次尝试中,我有一个带有逻辑和纹理的演员子类.然后我开始阅读 Gustavo Steigerts 教程,我看到他试图将逻辑和视图分开.我尝试做同样的事情,但视图是我的演员子类,包含有关大小和位置的信息,所以它必须进行碰撞检测,这是逻辑的一部分.此外,如果您在 Google 上搜索,您会发现使用 Scene2d 无法执行 MVC.那么有没有办法使用 MVC 实现一个场景 2d 游戏,或者我应该忽略这个模式并在一个扩展 actor 的类中拥有模型和视图?

I started developing a game using libgdx. It is actually my second try because when i started the first try i did not know enaught of libgdx. In my first try i had an actor subclass with the logic and the texture in it. Then i started reading Gustavo Steigerts tutorial and i saw that he tryed to separate logic and view. I tryed to do the same, but the view is my actor subclass with informations about size and position, so it has to do the collision detection, which is part of the logic. Also if you search on Google you find that with Scene2d you cannot do the MVC. So is there a way to implement a scene2d game using MVC or should i ignore this patern and have model and view in one class which extends actor?

推荐答案

这是一个主要基于意见的问题,但在涉及到 scene2d 和 MVC 模式时却经常出现.

This is a mostly opinion-based question, but it is one that comes up very often when it comes to scene2d and the MVC pattern.

一般来说,像模型-视图-控制器模式那样分离关注点总是有意义的.使用scene2d 这并不容易,因为Actors 已经有一个act() 和一个draw() 方法.

In general it always makes sense to seperate concerns like the Model-View-Controller pattern tries to do. With scene2d this is not that easy, because Actors have already an act() and a draw() method.

在我看来,您应该始终尝试像他们一样为您的游戏世界(实体)建模.应该有一个Player,也许是一个Rocket,也许还有一个Bunny类/演员/实体,他们应该知道他们需要知道和管理的一切他们自己.这与实体系统框架相反,实体由部分"组成,而这些部分由外部系统控制.

In my opinion you should always try to model your game world (entities) like they are. There should be a Player, maybe a Rocket and maybe a Bunny class/actor/entity and they should know everything they need to know and manage themselves. This is the opposite of an Entity-System-Framework where your entities are made of "parts" and those are controlled by external systems.

大多数情况下,将所有内容放在一起也会使其更加灵活,因为您已经在一个地方拥有了所需的一切.您的游戏玩法可能会改变,您的实体也会如此.如果您发现您的实体太大,您仍然可以将它们分成几个部分,例如 PlayerModelPlayerControllerPlayerRenderer 需要相互引用,然后触发 act()render() 方法中的引用.

Most of the time keeping everything together makes it also more flexible, because you already have everything you need in one place. Your gameplay might change and so will your entities. If you find that your entities get too big, you can still split those up in several parts like a PlayerModel, PlayerController and a PlayerRenderer which need references to each other and then trigger those in your act() and render() methods.

这篇关于将 MVC 与 libgdx scene2d 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆