Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem
Control Array and Timer problem Control Array and Timer problem
Control Array and Timer problem
Go Back  Xtreme Visual Basic Talk > > > Control Array and Timer problem


Reply
 
Thread Tools Display Modes
  #1  
Old 03-24-2010, 02:52 PM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Talking Control Array and Timer problem


After scouring the internet for days and getting nowhere with my game, I finally decided to submit a post.....so fingers crossed!

I have just completed a game for my uni course which all works fine but have now been told it might be better to use an array for all my images. The game features a set of toys that move across the screen on a conveyor, the toys/images have to be picked up with the mouse and dropped into toy boxes. Before I added the control array I had a timer and each toy had a flag which when set to true made it move across the conveyor. When a toy was picked up the flag set to false and stopped it moving.

The problem I am now having....I loop through the array in the timer, all the images move as necessary, when I pick up an image how do I stop it from moving? I tried img(index).Left = img(index).Left in the MouseDown event but that didn't do anything.

I am thinking that there is probably a very simple solution that I am just overlooking but I have fretted over this problem for so long that my brain is no longer functioning properly!

It doesn't matter if I use a control array or not but I really want to know the answer.

Please, Please somebody help me!
Much Thanks
Reply With Quote
  #2  
Old 03-24-2010, 05:11 PM
FireBot FireBot is offline
Centurion
 
Join Date: Mar 2008
Posts: 100
Default

Instead of your previous flags, you could use:

Code:
img(index).Tag = "1" 'move
img(index).Tag = "0" 'don't move

if img(Index).tag = "1" then
'move it
end if

'...

'On MouseDown set the tag to 0 ...
Tag is a property common on many VB controls and is there merely to store any extra data you may need related to the control. It's a string type.
Reply With Quote
  #3  
Old 03-24-2010, 05:53 PM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

Thanks I thought it was something along those lines but I'm still confused, this is my code,

Private Sub tmrConveyor_Timer()

If img(Index).Tag = "1" Then
img(Index).Left = img(Index).Left - 5
End If

This is not working correctly....none of the toys are moving now, am I doing something wrong? I really haven't got the hang of arrays.
Reply With Quote
  #4  
Old 03-24-2010, 06:03 PM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

Ok this is the code I've got now but only one image in the array moves....

Private Sub tmrConveyor_Timer()

For i = 0 To img.Count - 1
If img(i).Tag = "1" Then
img(i).Left = img(i).Left - 5
End If
Next

Any ideas?
Much appreciated
Reply With Quote
  #5  
Old 03-24-2010, 06:53 PM
vb5prgrmrControl Array and Timer problem vb5prgrmr is offline
Senior Contributor

* Expert *
 
Join Date: Mar 2009
Posts: 1,087
Default

While I agree with using the tag property to test to see if the image should move, and I see your testing that property for a specific value, where do you set it so you know it can move?



Good Luck
__________________
Just because I'm an expert does not mean I know it all and just because I know it all does not mean I'm an expert
Reply With Quote
  #6  
Old 03-24-2010, 07:21 PM
FireBot FireBot is offline
Centurion
 
Join Date: Mar 2008
Posts: 100
Default

Wherever you are creating the control array(if by code). Or if by hand, you'll need to initialize the .tag property:

Code:
'bulk init
For i = 0 to img.count -1
   img(i).Tag = "1"
Next i
VB does not use the .Tag property for anything(internally, changing it has no effect on the control's other functions, it is essentially just a string variable). VB will not change it or assign it any value 'automatically'. You have to do it manually, like in the example above.
Reply With Quote
  #7  
Old 03-24-2010, 08:15 PM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

Thank you so much Firebot....I set the Tag property to '1' on all my images and now it's working perfectly!

Thanks again....I really appreciate it.
Reply With Quote
  #8  
Old 03-25-2010, 10:07 AM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

I am completely stumped again, I was using this code to place the toys back in the correct postion on the conveyor...basically if they don't go into the right boxes they are repositioned back in their initial positons.

This is the code I was using:

If imgDoll.Top + imgDoll.Height < 288 Then
tmr.Enabled = False
Else: imgDoll.Left = imgPinkBear.Left + imgPinkBear.Width + 50
imgDoll.Top = (frmToySort.ScaleHeight - imgDoll.Height) - 14
End If

I am now using a control array and trying to make it more generic:


If imgGirl(g).Top + imgGirl(g).Height < 288 Then
tmr.Enabled = False
Else: imgGirl(g).Left = imgGirl(g).Left + imgGirl(g).Width + 50
imgGirl(g).Top = (frmHeight - imgGirl(g).Height) - 14
End If

I know the 2nd block of code isn't going to work, I've undelrined where the problem is but I just can't get it right....am thinking of going back to my original game and not using a control array.

Last edited by Quackers75; 03-25-2010 at 10:28 AM.
Reply With Quote
  #9  
Old 03-25-2010, 06:37 PM
vb5prgrmrControl Array and Timer problem vb5prgrmr is offline
Senior Contributor

* Expert *
 
Join Date: Mar 2009
Posts: 1,087
Default

I don't see where it is incorrect as it is pretty much the same code...
__________________
Just because I'm an expert does not mean I know it all and just because I know it all does not mean I'm an expert
Reply With Quote
  #10  
Old 03-25-2010, 08:58 PM
FireBot FireBot is offline
Centurion
 
Join Date: Mar 2008
Posts: 100
Default

It appears you're requiring a tmr control for each img control, with the way you've coded it. Could that be the problem?

Or you could again utilize the Tag value to store whether or not the timer should process it. For instance, zero could be 'disabled' (or does the timer need to run while holding a piece?).

Then in the timer code check if the .Tag is not zero to perform the actions, rather than disabling/enabling the timer(just leave it enabled).
Reply With Quote
  #11  
Old 03-26-2010, 03:17 AM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

The problem is I don't know how to position the toys back in their previous positions....when I first coded it without the array, each image/toy was placed at a set distance from another image/toy, this worked perfectly:

imgDoll.Left = imgPinkBear.Left + imgPinkBear.Width + 50

I tried something like this:

imgToy(1).Left = imgToy(0).Left + imgToy(0).Width + 50

but it doesn't work....it goes a bit crazy in fact!

And yes Firebot, originally I had a timer for every image but was trying to get away from that, now I've just got tmrConveyor which moves all the images and tmrToy which is enabled when a toy is released from a mouseDown event. Basically the mouse is used to pick up a toy and drop it in a box. It all works ok now accept for putting the toys back on the conveyor....I have no problem with repositioning it on the Y-axis but as for the X-axis....at the moment the toys are all ending up on top of each other instead of spaced evenly apart.
Reply With Quote
  #12  
Old 03-26-2010, 07:07 AM
vb5prgrmrControl Array and Timer problem vb5prgrmr is offline
Senior Contributor

* Expert *
 
Join Date: Mar 2009
Posts: 1,087
Default

Ahhh... I see...

So, lets say for arguements sake that you have 4 controls (0 to 3) and as they come up/down/left/right on the conveyer they are ordered as (say coming from the left to the right)...

3 2 1 0

User then grabbs 0 and places it in a box where it "disappears" and goes back to the conveyor belt and then "reappears", so now your order would be...

0 3 2 1

and so on and so forth...

Okay, there are two ways in which to skin this problem (and probably more...)...

The easiest way might to be to use a control array reference (for lack of better words)

For a simple demo of what I am talking about, start a new standard exe project and create an array of text boxes (text1(0 to 3)) and add a timer control, add the code, run...
Code:
Option Explicit

Dim MyTextboxArray(0 To 3) As TextBox

Private Sub Form_Load()
Dim I As Integer

For I = 0 To 3
  Text1(I).Left = (0 - Text1(I).Width)
  Text1(I).Top = 1000
  Text1(I).Text = "text1" & CStr(I)
  Set MyTextboxArray(I) = Text1(I)
Next I

Timer1.Enabled = True
Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
Dim I As Integer, T As TextBox
MyTextboxArray(0).Left = MyTextboxArray(0).Left + 30
For I = 1 To 3
  MyTextboxArray(I).Left = (MyTextboxArray(I - 1).Left - (MyTextboxArray(I - 1).Width + 300))
Next I

If MyTextboxArray(0).Left >= Me.Width Then
  Set T = MyTextboxArray(0)
  For I = 1 To 3
    Set MyTextboxArray(I - 1) = MyTextboxArray(I)
  Next I
  T.Left = (MyTextboxArray(0).Left - (MyTextboxArray(0).Width + 300))
  Set MyTextboxArray(3) = T
End If
End Sub

Good Luck
__________________
Just because I'm an expert does not mean I know it all and just because I know it all does not mean I'm an expert
Reply With Quote
  #13  
Old 03-26-2010, 01:58 PM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

Thanks vb5prgrmr, your help is much appreciated.

I tried out your example which was brilliant but really can't get it working properly with my own game. Have been on it all day again....the code is beginning to take on a life of its own! I need to grips with control arrays though so will keep on perservering, they are definitely my weakness.
Reply With Quote
  #14  
Old 03-26-2010, 02:03 PM
FireBot FireBot is offline
Centurion
 
Join Date: Mar 2008
Posts: 100
Default

Are you just trying to place them on the right of all the others?

Code:
Dim MaxLeft as Long
Dim MaxWidth as Long

'TODO: replace index_to_be_placed with your own variable

For i = 0 to imgToy.count -1 'loop through all images to find the right-most
 If i <> index_to_be_placed Then 'don't include the one we're moving!
  If imgToy(i).Left > MaxLeft Then 'store the left/width for placement
    MaxLeft = imgToy(i).Left
    MaxWidth = imgToy(i).Width
  End If
 End if
Next i

ImgToy(index_to_be_placed).Left = MaxLeft + MaxWidth + 50 'this should look familar
Reply With Quote
  #15  
Old 03-27-2010, 09:17 AM
Quackers75 Quackers75 is offline
Newcomer
 
Join Date: Mar 2010
Posts: 8
Default

Thanks again Firebot, I've added the coding you suggested but it doesn't work in the way it's supposed to. When I pick up and drop the first element in the array it goes to the right of all the others....brilliant! but if I pick up drop any of the others they drop out of the screen and the first element goes to the right again.

Here is all of the code that I have so far:

Option Explicit

Dim g As Integer
Dim Moving As Boolean
Dim MaxLeft As Long
Dim MaxWidth As Long
Dim t As Integer

Private Sub imgToy_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

Moving = True
imgToy(Index).Tag = 0

End Sub

Private Sub imgToy_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

If Moving Then
Dim xPixel As Single
xPixel = X / 15
Dim yPixel As Single
yPixel = Y / 15
imgToy(Index).Left = imgToy(Index).Left + xPixel - 15
imgToy(Index).Top = imgToy(Index).Top + yPixel - 25
End If

End Sub

Private Sub imgToy_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

Moving = False
tmrToy.Enabled = True
imgToy(Index).Tag = 3

End Sub

Private Sub tmrConveyor_Timer()

For g = 0 To imgToy.Count - 1
If imgToy(g).Tag = 1 Then
imgToy(g).Left = imgToy(g).Left + 5
ElseIf imgToy(g).Tag = 0 Then
imgToy(g).Left = imgToy(g).Left
End If
Next g

For g = 0 To imgToy.Count - 1
If imgToy(g).Left > Me.ScaleWidth Then
imgToy(g).Left = Me.ScaleLeft - imgToy(g).Left
End If
Next g

End Sub

Private Sub tmrToy_Timer()

For g = 0 To imgToy.Count - 1
If imgToy(g).Tag = 3 Then
imgToy(g).Top = imgToy(g).Top + 11
If imgToy(g).Top > 288 Then
Call ToyPlacement
End If
End If
Next g

End Sub


Private Sub ToyPlacement()

For g = 0 To imgToy.Count - 1
If g <> t Then
If imgToy(g).Left > MaxLeft Then
MaxLeft = imgToy(g).Left
MaxWidth = imgToy(g).Width
End If
End If
Next g

imgToy(t).Left = MaxLeft + MaxWidth + 50
imgToy(t).Top = (Me.ScaleHeight - imgToy(t).Height) - 14

End Sub



I can't see where the problem is, am I missing something?
Reply With Quote
  #16  
Old 03-27-2010, 07:42 PM
ZaCkOX's Avatar
ZaCkOX ZaCkOX is offline
Contributor
 
Join Date: May 2006
Location: CA
Posts: 645
Default t?

Is t always supposed to be zero? You declared it as integer but all I see is g <> t and then object(t)
__________________
ZaCkO ... Who is your attitude?
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
Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem Control Array and Timer problem
Control Array and Timer problem
Control Array and Timer problem
 
Control Array and Timer problem
Control Array and Timer problem
 
-->