【问题标题】:Referencing resource images dynamically动态引用资源图像
【发布时间】:2017-10-20 03:50:51
【问题描述】:

我正在制作一个二十一点游戏,我很难在没有将每张图片“硬编码”为我传递的值的情况下获得一张卡片图片。

当前代码(不需要,但有效):

Public Function Return_Card(CardVal As String, CardSuit As String) As Image
    If CardVal = "Ace" And CardSuit = "Clubs" Then
        Return My.Resources.AceOfClubs
    ElseIf CardVal = "Two" And CardSuit = "Clubs" Then
        Return My.Resources.TwoOfClubs
    ElseIf CardVal = "Three" And CardSuit = "Clubs" Then
        Return My.Resources.ThreeOfClubs
    ' Need the other 49 cards to complete deck
    End If
End Function

所需代码:

Public Function Return_Card(CardVal As String, CardSuit As String) As Image
    ' Return any picture of a card based on what is being passed in CardVal and CardSuit
    Return My.Resources.CardValOfCardSuit
End Function

【问题讨论】:

  • 对于像纸牌游戏这样的游戏,您不想一遍又一遍地从资源中加载它们:每次这样做都会创建一个新对象,这会导致您的应用程序泄漏。从 ImageList 加载它们或将它们从资源加载到数组中,并一遍又一遍地使用相同的图像。 Rough Card Game
  • 请尽量不要太详细说明我是如何存储我的信息的。创建对象后,我将它们放入数组中。 @Plutonix

标签: vb.net


【解决方案1】:
Public Function Return_Card(CardVal As String, CardNum As String) As Image
    Return My.Resources.ResourceManager.GetObject(CardVal + "Of" + CardNum)
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2015-01-20
    相关资源
    最近更新 更多