在libgdx scene2d中使用MVC [英] Using MVC with libgdx scene2d

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

问题描述

我开始使用libgdx开发游戏.实际上,这是我的第二次尝试,因为当我开始第一次尝试时,我并不了解libgdx.在我的第一次尝试中,我有一个actor子类,其中包含逻辑和纹理.然后,我开始阅读 Gustavo Steigerts教程,我发现他试图分离逻辑和视图.我尝试做同样的事情,但是视图是我的actor子类,其中包含有关大小和位置的信息,因此它必须进行碰撞检测,这是逻辑的一部分.另外,如果您在Google上搜索,则会发现使用Scene2d不能执行MVC. 那么,有没有一种方法可以使用MVC来实现Scene2d游戏,还是我应该忽略这种模式,并在一个扩展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.

通常,像Model-View-Controller模式试图做到的那样,分开关注是很有意义的.使用scene2d并不是那么容易,因为Actor已经具有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.

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

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