我应该将对象模型与精灵合并还是进行引用

发布于 2025-01-07 14:30:56 字数 678 浏览 0 评论 0原文

我正在制作一个游戏,我必须有一些对象,例如武器和角色,我最终希望将其视觉化为精灵或电影剪辑

我应该在几个类中编写对象的逻辑,然后引用精灵类中的对象如下所示:

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;
    }
}

然后,当我需要访问这些方法时,只需调用 Wea​​ponSprite 的方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2025-01-14 14:30:56

我会避免将对模型的引用传递给视图。一种更加解耦的方法是将模型 ID 传递给视图。通过 getter 提供对模型 id 的访问。一般来说,您希望将模型的知识排除在视图之外,并将视图的知识排除在模型之外。

下面是控制器类如何管理视图和模型之间关系的示例。

class View
{
   public function View( modelId:int )
   {
      this._id = modelId;
   }

   public function get modelId():int { return this._id; }

   protected function doSomething():void 
   {
        dispatchEvent( new Event(Event.DOING_SOMETHING) );
   }
}

class Controller 
{
    function init()
    {
        for each( model in models )
        {
            var view = new View( model.id );
            view.addEventListener( Event.DOING_SOMETHING, onViewDoingSomething );
        }
    }

    protected function onViewDoingSomething( event:Event ):void 
    {
       if( event.target is View )
       var view:View = event.target as View;
       // -- do something with view.modelId here
    }
}

另一件需要考虑的事情是您正在构建的应用程序有多大以及将有多少人在开发它。在构建快速原型时,您可能不关心遵循严格的 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.

class View
{
   public function View( modelId:int )
   {
      this._id = modelId;
   }

   public function get modelId():int { return this._id; }

   protected function doSomething():void 
   {
        dispatchEvent( new Event(Event.DOING_SOMETHING) );
   }
}

class Controller 
{
    function init()
    {
        for each( model in models )
        {
            var view = new View( model.id );
            view.addEventListener( Event.DOING_SOMETHING, onViewDoingSomething );
        }
    }

    protected function onViewDoingSomething( event:Event ):void 
    {
       if( event.target is View )
       var view:View = event.target as View;
       // -- do something with view.modelId here
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文