【问题标题】:How to Make a Class Function Change Object Image (VB.NET)如何使类函数更改对象图像(VB.NET)
【发布时间】:2016-11-23 22:31:20
【问题描述】:

我是 Stack Overflow 和 VB.NET(来自 C)的新手,所以请多多包涵 :)
我正在为我的编程课制作一个刽子手游戏,并且我创建了一个公共类角色,具有公共函数 Death() 作为动作 玩家将能够从各种角色中进行选择,每个角色都有不同的死亡动画,所以我想让 Death() 以定格动画的方式一遍又一遍地改变角色图像,例如

Death(){
    pictureboxchar.image(1.png)
    pictureboxchar.image(2.png)
    pictureboxchar.image(3.png)
    pictureboxchar.image(4.png)
}

但我不太确定我应该指的是什么而不是“pictureboxchar”。也许我应该参考类名 Character 本身? 我想完成这个,因为为角色创建一个自定义类会给我额外的学分。

Public Class Character
    Public Function Death() As Action

    End Function
End Class

这是我目前所拥有的
谢谢!

【问题讨论】:

    标签: vb.net function class action


    【解决方案1】:

    要实现动画效果,您需要使用计时器。您的 Character 类可能如下所示。这 4 个 PNG 文件需要位于应用程序的启动目录中。使用计时器的 Interval 属性来获得所需的动画速度。它的值以毫秒为单位,因此当设置为 500 时,计时器的滴答事件将每半秒触发一次。

    Imports System.IO
    Imports System.Windows.Forms
    
    Public Class Character
    
        Private WithEvents myTimer As New Timer
        Private iCounter As Int32
        Private iNumberImages As Int32 = 4 'images must be numbered "1.png", "2.png" etc in the order of animation
        Private iAnimationSpeed As Int32 = 500 'set to lower value for faster animation
        Private myPictureBox As PictureBox
        Private sPictureFileDirectory As String = Application.StartupPath 'the directory where the image files are located
    
        Public Sub New(ByVal pictureboxchar As PictureBox)
            myPictureBox = pictureboxchar
            myTimer.Interval = iAnimationSpeed
            myTimer.Enabled = True
        End Sub
    
        Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles myTimer.Tick
            iCounter += 1 'increment the counter to display the next image in the sequence
            If iCounter > iNumberImages Then iCounter = 1 'reset the counter to 1 when the maximum number of images is exceeded
            Dim sFilePath As String = sPictureFileDirectory & "\" & iCounter.ToString & ".png"
            If File.Exists(sFilePath) = True Then myPictureBox.Image = Image.FromFile(sFilePath)
        End Sub
    
    End Class
    

    从您的表单中,创建一个具有表单级别范围的 Character 类的实例,并将图片框传递给表单的加载事件处理程序中的构造函数:

    Private myCharacter As Character
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        myCharacter = New Character(pictureboxchar)
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多