locked
How do i clone an object in visual studio? RRS feed

  • Question

  • hello. i am trying to create a video game in Visual Basic.

    Problem:  I Cannot clone the monsters.

    How do i clone the items, and the scripts? is that possible?

    the game is almost finished.

    all is done, is exempt cloning the monsters.


    The Code Rules

    Sunday, February 4, 2018 6:38 PM

Answers

  • What have you tried?

    Here is one method.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Nave GCT Sunday, February 4, 2018 7:16 PM
    Sunday, February 4, 2018 6:42 PM
  • i know it does, but can i copy somthing like a picturebox? how would i do that?


    The Code Rules

    I don't think you can "clone" a picturebox.

    Is this what you want?

    Public Class Form3
        Private WithEvents Button1 As New Button With {.Parent = Me,
            .Location = New Point(200, 200), .Text = "Copy"}
        Private MonsterBmp As Bitmap = New Bitmap("c:\bitmaps\spaceship1.png")
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static y As Integer = 20
            y += 20
    
            Dim pic As New PictureBox With {.Parent = Me,
                .Size = New Size(50, 50),
                .Location = New Point(y, y),
                .BackgroundImage = MonsterBmp,
                .BackgroundImageLayout = ImageLayout.Zoom,
                .Name = "Picture at " & y.ToString}
    
            pic.BringToFront()
    
            AddHandler pic.Click, AddressOf OnMonsterClick
    
        End Sub
    
        Private Sub OnMonsterClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim n As String = DirectCast(sender, PictureBox).Name
            MsgBox(n & " was Clicked.")
        End Sub
    End Class


    • Edited by tommytwotrain Saturday, February 10, 2018 4:14 PM
    • Marked as answer by Nave GCT Saturday, February 10, 2018 4:33 PM
    Saturday, February 10, 2018 3:42 PM

All replies

  • What have you tried?

    Here is one method.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Nave GCT Sunday, February 4, 2018 7:16 PM
    Sunday, February 4, 2018 6:42 PM
  • wait... how do i copy the object I Want to copy

    The Code Rules

    Saturday, February 10, 2018 11:43 AM
  • wait... how do i copy the object I Want to copy

    The Code Rules

    The page I provided has a demo at the bottom of the page.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 10, 2018 12:27 PM
  • i know it does, but can i copy somthing like a picturebox? how would i do that?


    The Code Rules

    Saturday, February 10, 2018 2:38 PM
  • i know it does, but can i copy somthing like a picturebox? how would i do that?


    The Code Rules

    Hi

    Here is some code that illustrates one way to create 'new' monsters.

    Each time the button is clicked, it will add a new monster named 'MonsterX' where X is an increasing integer starting at 1

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim b As New Button
      Dim im As Image = Image.FromFile("C:\Users\lesha\Desktop\Plans\Dice 2\54.png")
      Dim r As New Random
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	' set up a Button
    	With b
    	  .Text = "Add Address"
    	  .Location = New Point(10, 220)
    	  .AutoSize = True
    	End With
    	Controls.AddRange({b})
    	AddHandler b.Click, AddressOf B_Click
      End Sub
      Private Sub B_Click(sender As Object, e As EventArgs)
    	Static monster As Integer = 1
    	Dim p As New PictureBox
    	With p
    	  .Name = "Monster" & monster.ToString
    	  .Size = New Size(60, 60)
    	  .SizeMode = PictureBoxSizeMode.Zoom
    	  .Image = CType(im.Clone, Image)
    	  .Location = New Point(5 + r.Next(1, ClientSize.Width - 10 - .Size.Width), 5 + r.Next(1, ClientSize.Height - 10 - .Size.Height))
    	End With
    	Controls.Add(p)
    	monster += 1
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by tommytwotrain Saturday, February 10, 2018 3:44 PM
    Saturday, February 10, 2018 3:32 PM
  • i know it does, but can i copy somthing like a picturebox? how would i do that?


    The Code Rules

    I don't think you can "clone" a picturebox.

    Is this what you want?

    Public Class Form3
        Private WithEvents Button1 As New Button With {.Parent = Me,
            .Location = New Point(200, 200), .Text = "Copy"}
        Private MonsterBmp As Bitmap = New Bitmap("c:\bitmaps\spaceship1.png")
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static y As Integer = 20
            y += 20
    
            Dim pic As New PictureBox With {.Parent = Me,
                .Size = New Size(50, 50),
                .Location = New Point(y, y),
                .BackgroundImage = MonsterBmp,
                .BackgroundImageLayout = ImageLayout.Zoom,
                .Name = "Picture at " & y.ToString}
    
            pic.BringToFront()
    
            AddHandler pic.Click, AddressOf OnMonsterClick
    
        End Sub
    
        Private Sub OnMonsterClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim n As String = DirectCast(sender, PictureBox).Name
            MsgBox(n & " was Clicked.")
        End Sub
    End Class


    • Edited by tommytwotrain Saturday, February 10, 2018 4:14 PM
    • Marked as answer by Nave GCT Saturday, February 10, 2018 4:33 PM
    Saturday, February 10, 2018 3:42 PM
  • yes.

    The Code Rules

    Saturday, February 10, 2018 4:32 PM
  • i will try

    The Code Rules

    Saturday, February 10, 2018 4:33 PM
  • Nave,

    In .Net the word object is not always for what it stands. 

    But it is not only in Net object has many meanings. 

    But to keep it with what we are busy. In .Net and so in VB object can be:

    1. The main class of .Net
    2. Something we can see as a picture or a box
    3. An instanced class
    4. A reference set to an instanced class. 

    Mostly an object,  even a cloned one is .Net,  is just a reference which is copied. Therefore many objects can points to just one. 

    If you want a real copy (clone is real a word with endless different meanings in .Net) than you have mostly in whatever way to create a new one. Sometimes there are special methods (never with the name Clone by the way) but mostly they don't exist. 

    Not a problem. Creating a new picturebox which holds a different image is simple to construct (using the new keyword) and set the image property to a whatever image. Constructing an imagebox is only telling that there is a new reference created. The code is in the Framework which is already on every Windows computer.


    Success Cor

    Saturday, February 10, 2018 5:19 PM