none
Display Healthpoints and Attack points of tanks into a label

    Question

  • Hi there all I thought this would be relatively straightforward for me but it is not. 

    What I am trying to do here is display the healthpoints of one sprite then display in another label the attack points of the other sprite. 

    I put this code in the form1_load feature 

    it says 

    label1.text = sprites(1).HP 

    label2.text = sprites(2).AP

    it shows red squiggly lines which says "Option strict disallows conversion of type integer into string"

    Could someone indicate where I could be going wrong here which could help me get on the right track. 


    WRA

    Sunday, March 19, 2017 2:11 PM

Answers

  • Sure, just write a method to take an instance of Sprite and set the label text from it, and then call that function passing the appropriate sprite when you select it.  Something like:

    Public Sub UpdateLabels(sprite As Sprite)
        Label1.Text = sprite.HP.ToString()
        Label2.Text = sprite.AP.ToString()
    End Sub

    I'm not sure what your Sprite object actually is or how you select it, but when you select the sprite you then call UpdateLabels() and pass sprites(1) or sprites(2) as appropriate.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 9:26 AM
    Sunday, March 19, 2017 2:56 PM
    Moderator

All replies

  • label1.text = sprites(1).HP.ToString()

    With Option String On you cannot assign an Integer directly to a String variable.  You need to call ToString on the Integer instance first.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, March 19, 2017 2:33 PM
    Moderator
  • Ahh I see, why do I not remember things like this like seriously.

    Quick question Reed, Is it possible to make it as a select case for instance I select Sprite 1, it will show it lbl1 its hp and lbl2 is ap and in the second case i select Sprite 2 and it does the same feature to sprite 1.


    WRA

    Sunday, March 19, 2017 2:45 PM
  • Sure, just write a method to take an instance of Sprite and set the label text from it, and then call that function passing the appropriate sprite when you select it.  Something like:

    Public Sub UpdateLabels(sprite As Sprite)
        Label1.Text = sprite.HP.ToString()
        Label2.Text = sprite.AP.ToString()
    End Sub

    I'm not sure what your Sprite object actually is or how you select it, but when you select the sprite you then call UpdateLabels() and pass sprites(1) or sprites(2) as appropriate.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 9:26 AM
    Sunday, March 19, 2017 2:56 PM
    Moderator
  • Alright cheers. Ill be doing more coursework around tomorrow 1pm UK time during my lesson. 

    I have a sociology mock exam coming up so gotta revise. 

    Wish me luck I do better than the last test. 


    WRA

    Sunday, March 19, 2017 3:23 PM
  • @Reed

    Should I put this within form1 or should I create a class which interits it?


    WRA

    Sunday, March 19, 2017 4:25 PM
  • @Reed

    Should I put this within form1 or should I create a class which interits it?


    WRA


    It would likely be in the Form - it needs to be accessible from the code which responds to selecting a tank.  Its really up to how you want to organize though.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, March 19, 2017 6:29 PM
    Moderator
  • Is it possible to make it as a select case for instance I select Sprite 1, it will show it lbl1 its hp and lbl2 is ap and in the second case i select Sprite 2 and it does the same feature to sprite 1.

    That's why you are using a collection of sprites.  The index '(n)' is used to indicate exactly which sprite the code refers to, removing the need for a Select Case.  Re-do that code so that it uses a variable instead of a literal for the index, and set the variable value to the sprite you wish to refer to.

    After, of course, you have confirmed exactly how the numbering for the list indexes works.

    Sunday, March 19, 2017 10:43 PM
  • Alrighty cheers Ill do what I can now then I'm going full on coursework from Wednesday 

    WRA

    Monday, March 20, 2017 1:26 PM
  • @Reed

    Something is going wrong here with it:

    Severity Code Description Project File Line Suppression State

    Error BC30508 'Sprites' cannot expose type 'Form1.Sprite' in namespace 'Move_PNG' through class 'Form1'. Move PNG N:\Documents\Visual Studio 2015\Projects\Most Recent Testings\Move PNG\Move PNG\Form1.vb 21 Active

    This is exactly what the error says.


    WRA

    Monday, March 20, 2017 1:46 PM