C# 渲染“玩家”来自不同类别的精灵
我想知道如何显示 Player.cs 类中的“玩家”精灵。到目前为止,一切都发生在 Program.cs 文件中,女巫拥有用于渲染的“设备”。 现在我想做同样的事情,但使用不同的类。我是否需要为每个类创建设备来呈现其中发生的内容,或者我需要以某种方式访问 Program.cs 文件内的设备?
谢谢。
i was wondering how could i display 'player' sprite from Player.cs class. So far everything was happening in Program.cs file, witch had 'device' for rendering.
Now i want to do same but using different classes. Do i need to create device for each class to render stuff thats happening within them, or i need to somehow access the device inside Program.cs file?
Thank you.
您将首先在 Program.cs 中创建设备,然后如果有另一个类需要通过该设备呈现,您将在该另一个类的构造函数中传递对该设备的引用。它将把它存储在一个私有字段中,这样它就可以在需要时使用它。
但这可能是不必要的:如果您有一个中心
Render
方法,您可能会循环遍历各种精灵对象并要求它们通过调用每个对象自己的Render
来渲染自己方法。因此,您不妨在此时向它们传递设备引用,而不是让每个对象知道它所连接的设备。You would start by create the device in Program.cs, then if there is another class that will need to render via that device you would pass a reference to the device in that other class's constructor. It would store it in a private field, so it could use it whenever it needs to.
But this may be unnecessary: if you have a central
Render
method, you would probably be looping through various sprite objects and asking them to render themselves, by calling each object's ownRender
method. So you may as well pass them the device reference at that point, rather than making each object know the device it is attached to.