我应该将对象模型与精灵合并还是进行引用
我正在制作一个游戏,我必须有一些对象,例如武器和角色,我最终希望将其视觉化为精灵或电影剪辑
我应该在几个类中编写对象的逻辑,然后引用精灵类中的对象如下所示:
public class WeaponModel
{
public damage:Int = 8;
public function Weapon(){
//Do the constructor code
}
public function get damage():int{
return this.damage;
}
}
public class WeaponSprite extends Sprite
{
var weaponModel:WeaponModel;
public function WeaponSprite(weaponModel:WeaponModel){
this.weaponModel = weaponModel;
}
public function get weaponModel():WeaponModel{
return this._weaponModel;
}
}
然后,当我需要访问这些方法时,只需调用 WeaponSprite 的方法 WeaponModel() 即可造成伤害,或者我应该将两者合并到一个类中。
I'm making a game where I have must have some objects like weapons and characters, which I in the end would like to get visual as a sprite or movieclip
Should I write the logic for the objects in several classes and then make a reference to the object in a sprite class like the following :
public class WeaponModel
{
public damage:Int = 8;
public function Weapon(){
//Do the constructor code
}
public function get damage():int{
return this.damage;
}
}
public class WeaponSprite extends Sprite
{
var weaponModel:WeaponModel;
public function WeaponSprite(weaponModel:WeaponModel){
this.weaponModel = weaponModel;
}
public function get weaponModel():WeaponModel{
return this._weaponModel;
}
}
And then when I need to do damage just call the WeaponSprite's method weaponModel() when I need to acces the methods or should I just merge the two together into one class.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会避免将对模型的引用传递给视图。一种更加解耦的方法是将模型 ID 传递给视图。通过 getter 提供对模型 id 的访问。一般来说,您希望将模型的知识排除在视图之外,并将视图的知识排除在模型之外。
下面是控制器类如何管理视图和模型之间关系的示例。
另一件需要考虑的事情是您正在构建的应用程序有多大以及将有多少人在开发它。在构建快速原型时,您可能不关心遵循严格的 OOP 模式。然而,项目越大、越复杂,框架或特定设计模式对您有价值的可能性就越大。
如果您有兴趣学习有助于一致和灵活设计的框架,考虑查看 Robot Legs。
I would avoid passing a reference to the model to a view. A more decoupled approach would be to pass a model id to the view. The provide access to the model id through a getter. In general you want to keep knowledge of models out of views and knowledge of views out of models.
Below is an example of how a controller class might manage the relationship between views and models.
Another thing to consider is how large of an application you are building and how many people will be working on it. When building a quick prototype, you may not be concerned with following strict OOP patterns. However, the larger and more complex a project gets, the more likely it is that a framework or specific design patterns will be of value to you.
If you are interested in learning a framework that facilitates consistent and flexible designs, consider looking at Robot Legs.