Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Game Programming > Dynamically add objects to a form


Reply
 
Thread Tools Display Modes
  #1  
Old 12-27-2011, 04:38 PM
Lotokill2 Lotokill2 is offline
Newcomer
 
Join Date: Dec 2011
Posts: 6
Default Dynamically add objects to a form


Trying to dynamically add Alien objects (Picture boxes) to my game form. The picture boxes are invisible when I run the game. All of the other controls for the picture box seen to work just fine. Movement, Alien Shots ,etc, etc. Any and all help would be most appreciated.

Const NumOfAliens As Integer = 20
Dim MoveAliens1(NumOfAliens) As Boolean
Dim Aliens(NumOfAliens) As PictureBox
Dim x As Integer


Private Sub loadAliens()
' Dynamically adds aliens

For Me.x = 1 To NumOfAliens
Aliens(x) = New PictureBox
Aliens(x).Image = My.Resources.Alien
Aliens(x).Width = 50
Aliens(x).Height = 50
Aliens(x).BackColor = Color.Transparent
Aliens(x).Left = (-50 * x) + (x * 5)
Controls.Add(Aliens(x))
Next
End Sub

Private Sub FlightPath()
' controls direction of regular alien flight
For Me.x = 1 To NumOfAliens
If MoveAliens1(x) = True Then
Aliens(x).Left -= AlienSpeed
End If
Next
End Sub
Reply With Quote
  #2  
Old 12-28-2011, 09:44 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,882
Default

Well, I don't know.
There isn't enough code there to say for sure, but it looks like you place all the Aliens outside the window at first (-50 * X) + (5*X) ??

And, assuming AlienSpeed is positive, you move them further outside the window (subtracting speed from position).
Aliens(x).Left -= AlienSpeed

I'm not sure how you could tell that the pictureboxes are not visible since, from the given code, you never position them where you can see if they are visible or not.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 12-28-2011 at 09:56 AM.
Reply With Quote
  #3  
Old 12-28-2011, 10:51 AM
Lotokill2 Lotokill2 is offline
Newcomer
 
Join Date: Dec 2011
Posts: 6
Default

Yes, the picture boxes do start outside of the window and Aliens(x).left-= AlienSpeed brings them into the playing field window. MoveAlien2 is true when the Aliens(x) is over the little man walking on the ground, MoveAlien2 moves Aliens(x).top += 2 * AlienSpeed until Aliens(x).top + Aliens(x).height = PixSprite.top, then MoveAlien2=false and MoveAlien3=true.
When MoveAlien3= true the Aliens(x)-= AlienSpeed which brings Aliens(x),carrying the pixSprite back to the top of the window. Once the Aliens(x) reaches the top of the window with the pixSprite, pixSprite.visible=false and MoveAlien4=true.
MoveAlien4 changes the Aliens(x) picture and the Aliens(x) movement to be magnetized to the pixShooter.
When I start the game up, I can see the Aliens(x) shots, so I have an idea of where Aliens(x) position is. I can also see the pixSprite move to the top of the window when MoveAlien3=true, once moveAlien4 is true, it only takes a couple of seconds for the Aliens(x) to hit the pixShooter and end the game.
Based on what I see when I start the game, the code is working how I want it to, I just can't see the dynamically created picture boxes.

Although Aliens(x) start out at the left side of the window,once they hit a certain negative position they spawn on the far right side of the map, PixSprite moves in the opposite direction as the Aliens(x) and once it hits 2000 it spawns back at 1.
I also have the background image scroll either left or right depending on pixShooter direction

Last edited by Lotokill2; 12-28-2011 at 11:14 AM.
Reply With Quote
  #4  
Old 12-28-2011, 05:38 PM
Lotokill2 Lotokill2 is offline
Newcomer
 
Join Date: Dec 2011
Posts: 6
Default

I Moved the background image half way down the form to expose the location where the Aliens(x) should be spawning at.

The Aliens(x) were in fact spawning in the right location, however, they were spawning on top of each other. I modified the code and now they spawn exactly where they should ,which brings me to my next question.

How do you " Bring To Front" a dynamically created picture box?
Reply With Quote
  #5  
Old 12-29-2011, 02:43 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,882
Default

Quote:
Originally Posted by Lotokill2 View Post
...,which brings me to my next question.
How do you " Bring To Front" a dynamically created picture box?
Uhh..., Aliens(x).BringToFront()



{after you've added it to the form's controls, of course}
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 12-29-2011 at 02:48 PM.
Reply With Quote
  #6  
Old 12-29-2011, 05:48 PM
Lotokill2 Lotokill2 is offline
Newcomer
 
Join Date: Dec 2011
Posts: 6
Default

Yes , I found that code earlier today on msdn.microsoft.com

Thank you again for all of your help
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->