have a code that kind of works. not to sure how to fix

(VB2010)Setting multiple moving speeds to one picture

Posted 03 January 2011 - 07:17 PM

Pretty much I'm trying to move one picture box to a set time. The time is read from a text file than is outputted by a timer. The picture(Megaman head) is supposed to travel down the page and then when it hits with the image it resets itself and starts again. The problem I am having is that the picture moves to the first time set, but then ignores all the rest and either becomes a continuous loop, or that the picture only moves very slowly.

Here's the Code I used:

Public Class Form1
Dim f_keyval As Integer
Dim beat, f_time1(), f_time2(), f_time3(), f_CurSeconds(), f_NewInterval(), f_CurBeat As Integer
Dim f_direction() As String
Dim f_score As Integer
Dim f_minsec, f_sec, f_min As Integer
Dim loc As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loc = ("\Computer Programming\gif check\gif check\Resources\kick(1).gif")[color="#006400"]'picture the Megaman hits[/color]
Me.PictureBox2.Image = Image.FromFile(loc)
FileOpen(1, "piano(loud)red-lucifer.txt", OpenMode.Input, OpenAccess.Read)[color="#006400"]'sequential file that is read[/color]
Do Until EOF(1)
ReDim Preserve f_time1(beat)[color="#006400"]'mins, sec, milisec (f_time1-f_time3) in that order[/color]
ReDim Preserve f_time2(beat)
ReDim Preserve f_time3(beat)
ReDim Preserve f_direction(beat) [color="#006400"]'what picture will be moving[/color]
ReDim Preserve f_CurSeconds(beat)[color="#006400"]'current seconds that have passed [/color]
ReDim Preserve f_NewInterval(beat)[color="#006400"]'when the next interval should begin[/color]
Input(1, f_time1(beat))
Input(1, f_time2(beat))
Input(1, f_time3(beat))
Input(1, f_direction(beat))
beat += 1
Loop
beat -= 1
For Seconds = 0 To beat
[color="#006400"]'I have it so that the time is read to the certain time. So if I say that the time in the file is 00,01,000,U then the Megaman head will move at 1sec[/color]
f_time1(Seconds) += 1
If f_time1(Seconds) = 100 Then
f_time1(Seconds) = 0
f_time2(Seconds) += 1
End If
If f_time2(Seconds) = 60 Then
f_time2(Seconds) = 0
f_time3(Seconds) += 1
End If
f_CurSeconds(Seconds) = (f_time1(Seconds) * 60) + f_time2(Seconds) + (f_time3(Seconds) * 100)
Next
For a = 1 To beat
If a = 1 Then
f_CurSeconds(a) = f_CurSeconds(a) * 100
End If
f_CurSeconds(a) = f_CurSeconds(a - 1)[color="#006400"]'makes it so that the time is simply added onto the next[/color]
Next
For i = 0 To beat
f_NewInterval(i) = (f_CurSeconds(i) * 100) / 5[color="#006400"]'represents when the next interval should be[/color]
Next
f_CurBeat = 0
timTime.Enabled = True
timBeat.Enabled = True
FileClose(1)
End Sub
[color="#006400"]'timer code[/color]
Private Sub timTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timTime.Tick
For v = 0 To beat
If f_direction(v) = "U" Then[color="#006400"]'the declaration of what pic should move[/color]
Megarmans.Top += v[color="#006400"]'Megaman head moves at the speed of beat[/color]
If Megarmans.Bounds.IntersectsWith(PictureBox2.Bounds) Then
Megarmans.Top = 5
f_CurBeat += 1
timTime.Interval = f_NewInterval(v)
[color="#006400"]'the Megaman head is reset to the top of the form at 5. Then the next time should be read. And then the cycle changes according to the next time.[/color]
End If
End If
Next
End Sub
End Class

This piece of code is the one that when the second time comes around the picture just inches forward and does not intersect with the other picture in time. I have a timer running at the side to see that the first time does intersect with the picture on time, but is everything thing else that does not.
Also I think that the way I have my text file written out, might also have something to do with it. Here is an examlpe:

Re: (VB2010)Setting multiple moving speeds to one picture

It is common to lock all game actions to 60 frames per second. Are you doing this with every entity?

I'm not really sure. How do I check to see if I am?

When people write games they normally are taught that the basic structure is:

1. Initialize.
2. Run game loop.
3. Terminate.

But to be compatible across processor speeds to run at the same rate we actually have:

1. Initialize.
2. Run game loop.
2b. Halt game loop until 1/60 second since last iteration of game loop.
3. Terminate.

so in pseudocode your code structure should look like:

DO InitialiseGame
WHILE(gameExecuting)
DO HaltFor60fps /* Wastes cycles until 1/60 second has elapst since last iteration. */
DO UpdateUserInteraction
DO UpdatePhysics
DO UpdateGraphics
DO UpdateAudio
DO TerminateGame

Re: (VB2010)Setting multiple moving speeds to one picture

It is common to lock all game actions to 60 frames per second. Are you doing this with every entity?

I'm not really sure. How do I check to see if I am?

When people write games they normally are taught that the basic structure is:

1. Initialize.
2. Run game loop.
3. Terminate.

But to be compatible across processor speeds to run at the same rate we actually have:

1. Initialize.
2. Run game loop.
2b. Halt game loop until 1/60 second since last iteration of game loop.
3. Terminate.

so in pseudocode your code structure should look like:

DO InitialiseGame
WHILE(gameExecuting)
DO HaltFor60fps /* Wastes cycles until 1/60 second has elapst since last iteration. */
DO UpdateUserInteraction
DO UpdatePhysics
DO UpdateGraphics
DO UpdateAudio
DO TerminateGame

So you may need to alter structure slightly.

I'm not quite sure how to apply this halting feature to my code I tried this:
For i = 0 To beat
f_NewInterval(i) = (f_CurSeconds(i) * i)

If f_CurSeconds(i) = 60 Then'when the current seconds equal 60secs the next interval equals when the current seconds 1/60
timTime.Interval = (f_CurSeconds(i) * 1) / 60
End If
Next

I'm not too sure if it works. The speed seems to go in a pattern from fast, medium, slow, then repeat when this code is applied.
Could you clarify where I should apply this halting part to the code? And how I could apply it?

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 05 January 2011 - 07:45 PM

I actually just rewrote some of my code to compensate for this problem. I had a timer setup to draw the screen every 1/60th of a second (eg. 60 FPS), and I also ran my animation code every time the screen was drawn. This worked great except when the computer wasn't able to do 60 FPS, so I calculated a animation-speed scale targetFPS / actualFPS and multiplied any constants in my animation code by it.