Problems with countdown routine - win7

This forum is meant for requesting technical support or reporting bugs.

Moderators: time-killer-games, Vengeance66, Candle, reneuend, GM-Support

Problems with countdown routine - win7

Postby reneuend » Wed Dec 22, 2010 5:55 am

With the code below, the screen correctly displays "60" for the start, but then it changes to "-1" in the next second. What am I doing wrong? :shock:


Code: Select all
  LoadControl Text(4)
  Text(4).Move 712*15,566*15,100*15,30*15
  Text(4).Refresh
  Counter = 60
  Text(4) = Counter
  Text(4).ForeColor = RGB(255,255,255)
  Text(4).FontSize = 10
  Text(4).Visible = True
  Text(4).Refresh

  ctimer=Action.CreateTimedEvent (1,"UpdateCounter(counter)",true)



Frame Properties Code


Code: Select all
Sub UpdateCounter(counter)

   counter=counter-1

   If Counter = 10 Then
     PlaySoundWin7 GetPath(4) & "red_tail_hawk.mp3", 1
       End If

   If counter < 10 Then
      Text(4).ForeColor = RGB(255,0,0) 
   End If

   If counter=0 then 
      RemoveTimedEvent (ctimer)
          Text(4) = "0"
              Action.GoToFrame("score")
         Exit Sub

   End If

   Text(4) = counter

End Sub

Subroutine for countdown
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2690
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby juhuwoorps » Wed Dec 22, 2010 8:02 am

Do you have a variable called counter?
If so, I would try to call the subroutine just with

Code: Select all
UpdateCounter


and rename the subroutine to

Code: Select all
Sub UpdateCounter


If you dont have a variable called counter, the problem in your code is that the counter var is not given correctly to the sub. If I remember right there is a special way to use quotes in the timedevent to handle the var.

So I would prefer the way with the variable.
juhuwoorps
Code Master
 
Posts: 622
Joined: Tue Jul 08, 2008 3:13 pm

Postby reneuend » Wed Dec 22, 2010 2:41 pm

I found the issue. I forgot to define counter under the variables tab! (blush)

Thanks for your help!
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2690
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby juhuwoorps » Wed Dec 22, 2010 10:22 pm

So if you define the counter variable I'm wondering why you are tranfering the variable with the timed event to the sub.
In my eyes there is no need for it.
juhuwoorps
Code Master
 
Posts: 622
Joined: Tue Jul 08, 2008 3:13 pm

Postby reneuend » Wed Dec 22, 2010 10:30 pm

There isn't a need for it, except that it makes the subroutine stand on its own and not be dependent for a global var. It's just good programming practice.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2690
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby juhuwoorps » Wed Dec 22, 2010 10:35 pm

Ah ok. But couldn't it be a source of errors when you are using a var twice for the same thing?
juhuwoorps
Code Master
 
Posts: 622
Joined: Tue Jul 08, 2008 3:13 pm

Postby reneuend » Wed Dec 22, 2010 11:52 pm

no. because if the parameter var has the same name then it by default is a reference variable in VB6. If it were local and had the same name, it would still work because it becomes a copy of the incoming var even if its a global.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2690
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby juhuwoorps » Wed Dec 22, 2010 11:59 pm

Ah cool. Learned another piece of VB. Thanks! :)
juhuwoorps
Code Master
 
Posts: 622
Joined: Tue Jul 08, 2008 3:13 pm

Postby reneuend » Thu Dec 23, 2010 7:38 am

Actually what I should have done was to make the parameter a local variable so it's a copy of anything that is coming into the subroutine. This is the safest thing to do. You would make it a reference if you want to allow the variable's value to remained changed (if it's changed in the routine) when the subroutine was finished.

Shown below, I use the keyword, "byval" to make a copy of the variable, counter, coming into the routine.

Code: Select all
Sub UpdateCounter( byval counter)

   counter=counter-1

   If Counter = 10 Then
     PlaySoundWin7 GetPath(4) & "red_tail_hawk.mp3", 1
       End If

   If counter < 10 Then
      Text(4).ForeColor = RGB(255,0,0) 
   End If

   If counter=0 then 
      RemoveTimedEvent (ctimer)
          Text(4) = "0"
              Action.GoToFrame("score")
         Exit Sub

   End If

   Text(4) = counter

End Sub
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2690
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA


Return to Adventure Maker Technical Support and Bug Reports

Who is online

Users browsing this forum: No registered users and 2 guests

cron