使用另一种形式的一种形式的变量 [英] Using variables from one form in another form

查看:81
本文介绍了使用另一种形式的一种形式的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一种RPG风格的游戏,其中有一个怪物的健康栏,以及一个玩家的健康栏(使用进度栏)。我已经完成了form1的战斗布局,即起始形式。有5个按钮,攻击,药水/库存,防守,技能,跑步。

我需要做的是当玩家点击药水/库存按钮时,另一个表格/弹出窗口打开他们的库存,显示他们拥有的每种药水的数量(有5个级别的药水)然后他们可以点击每个药水旁边的按钮来使用它。但我的问题是,我需要将玩家健康变量转移到第二个库存表格。

我已经尝试了很多东西,你会在我的代码中看到,但没有工作并给我错误。谢谢您的时间

Form1

i am trying to make a sort of rpg-style game, where there is a health bar for a monster, and a health bar for a player(using progress bars). i have done the battle layout in form1, the starting form. there are 5 buttons, Attack, Potion/inventory, Defend, Skill,Run.
What i need to happen is for when the player clicks "Potion/Inventory" button, another form/ popup opens with their inventory, displaying how many of each potion they have(there are 5 ranks of potions) then they can click a button beside each potion to "use" it. but my problem is, i need to carry over the players Health variable to the second inventory form.
i have tried many things, that you will see in my code below, but none work and give me errors. thank you for your time
Form1

Public Class lblMobH

    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Shared attack As Integer = 5
    Public Shared crit As Integer = 0
    Dim mhealth As Integer = 25

    Dim mattack As Integer = 3
    Dim mcrit As Integer = 0
    Dim health As Integer = 100
    Public Shared fattack As Integer



    Private Sub btnAttack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttack.Click

        'you attacking mob
        crit = (10 * Rnd()) * Now.Millisecond / 1000
        If crit = 6 Then
            attack += 25%
        End If
        If pbMobHealth.Value > 0 Then
            pbMobHealth.Value += -attack
            mhealth += -attack
            attack = 5
        ElseIf pbMobHealth.Value < attack Then
            pbMobHealth.Value = 0
        End If
        lblmhealth.Text = mhealth
        'mob attacking you
       

        mcrit = (10 * Rnd()) * Now.Millisecond / 1000

        If mcrit = 6 Then
            mattack += 25%
        End If
        If pbHealth.Value > 0 Then
            pbHealth.Value += -mattack
            health += -mattack
            mattack = 5
        ElseIf pbHealth.Value < mattack Then
            pbHealth.Value = 0
        End If
        lblhealth.Text = health
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        SendMessage(pbMobHealth.Handle, 1500, 2, 0)
        SendMessage(pbHealth.Handle, 1040, 2, 0)
        SendMessage(ProgressBar3.Handle, 1040, 3, 0)
    End Sub

    Private Sub btnPotion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPotion.Click
        Dim Form1 As Form2
        Form1 = New Form2()
        Form1.Show()
        Form1 = Nothing
        Form2.Invhealth = health
    End Sub
End Class



Form2(库存)


Form2(Inventory)

Public Class Form2
    Dim P1 As Integer = 1
    Dim P2 As Integer = 1
    Dim P3 As Integer = 1
    Dim P4 As Integer = 1
    Dim P5 As Integer = 1
    Public Shared Invhealth As Integer = Form.variable



    Private Sub btnP1Use_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnP1Use.Click
        If P1 > 0 Then
            P1 += -1
            Invhealth = +15
        End If
    End Sub
End Class



*只是一张纸条,P1-5是药水等级。

此外,我会知道如何创造一种暂停在攻击怪物的玩家与攻击玩家后的怪物之间,这一切都发生在按下攻击按钮时,但它们都发生在同一时间(或至少看起来)

i会像这样发生

- 按下按钮

- 怪物失去健康

- 2秒等待

- 玩家失去健康


*just a note, P1-5 are potion ranks.
Furthermore, i would slsolike to know how to create a sort of "pause" between the player attacking the monster, and the monster after attacking the player, this all happens when the "attack" buttonis pressed, but they both happen at the same time(or at least seem to)
i would like it to happen like this
-Attack button pressed
- monster loses Health
- 2 Second wait
- Player loses Health

推荐答案

在您的广告资源表单中声明一个新事件:PotionUs在你的第一张表格中编辑和消费这些活动。



样本:



In your inventory form declare a new event: PotionUsed and consume that events on your first form.

Sample:

Public Class Form1

    Dim WithEvents InventoryForm As New Form2()
    Dim health As Integer = 100

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.InventoryForm.Show()
    End Sub

    Private Sub InventoryForm_Potion1Used(sender As Object, e As EventArgs) Handles InventoryForm.Potion1Used
        Me.health += 15
    End Sub

    Private Sub InventoryForm_Potion2Used(sender As Object, e As EventArgs) Handles InventoryForm.Potion2Used
        Me.health += 50
    End Sub

End Class







Public Class Form2

    Public Event Potion1Used As EventHandler
    Public Event Potion2Used As EventHandler

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent Potion1Used(Me, EventArgs.Empty)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RaiseEvent Potion2Used(Me, EventArgs.Empty)
    End Sub

End Class


这是关于表单协作的热门问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


你可以使用查询字符串yo使用变量1到另一个。
you can use the query string yo use variable one from to another.


这篇关于使用另一种形式的一种形式的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆