seconds counter

Dubbsta

Active Member
Joined
Jul 13, 2017
im trying to do something like shoot bullets every second on keydown, so far i have this

timer = Millisecs()
seconds = timer / 1000

i am using a delta timer
i dont know what else from here...help needed
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
I would just treat 'bullet-power' as a resource that you add every update - your update function might contain something like:

' How much time passed since last update, use for delta timing etc.
Local millisPassed:Int = Millisecs() - lastMillis
lastMillis = Millisecs()

' Handle bullets
bulletPower += millisPassed
If bulletPower > 1000
If KeyDown( KEY_SPACE )
FireBullet()
bulletPower -= 1000
End
bulletPower = Min( 1000, bulletPower )
End

[lastMillis and bulletPower are fields or globals]

I haven't tested this but that is the general idea. If you aren't firing at the moment, bulletPower will build up so when you do press space again the first bullet will be immediate. But you don't want it to build up indefinitely or loads of bullets will be fired - one every update until bullet power runs out - so you cap the stored power at one bullet's worth.

The above allows you to easily implement Pause or Fast Fire modes too (in Pause mode bulletPower doesn't build up; in fast Fire mode bullets cost less than 1000)
 
Last edited:

Dubbsta

Active Member
Joined
Jul 13, 2017
thanks Gerry i got it to work as a test with drawtext to see exactly whats happening. will this work with a list? also bulletpower min gave an error, what exactly is it supposed to do?
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
thanks Gerry i got it to work as a test with drawtext to see exactly whats happening. will this work with a list? also bulletpower min gave an error, what exactly is it supposed to do?
The Min( x, y ) function returns whichever value is smaller. Maybe auto-conversion fails if one is float and one is int? Anyway the purpose is that if you are not firing, bulletPower builds up to 1000 and goes no further.

Anyhoo... here's a code example. The bar shows current bulletPower, press SPACE to fire when on full power, hold SPACE to fire whenever bar fills. I'm not sure what you mean about a list. Certainly you could add bullets to a list instead of just flashing the screen like in my example.

Code:
Global theApp:BulletApp


Function Main:Int()
    New BulletApp()
    Return 0
End



Class BulletApp Extends App

    Field lastMillis:Int
  
    Field bulletPower:Int
  
    Field firedBullet:Bool
  

    Method OnCreate:Int()
        lastMillis = Millisecs()
        SetUpdateRate( 60 )
        Return 0
    End
  
  
    Method OnUpdate:Int()
        Local millisPassed:Int = Millisecs() - lastMillis
        lastMillis = Millisecs()
        bulletPower += millisPassed
        If KeyDown( KEY_SPACE )
            If bulletPower >= 1000
                firedBullet = True  
                bulletPower -= 1000
            End
        End
        bulletPower = Min( 1000, bulletPower )
        Return 0
    End
  
  
    Method OnRender:Int()
        If firedBullet
            Cls( 255, 0, 0 )
            firedBullet = False
        Else
            Cls( 0, 0, 0 )
        End
        SetColor( 255, 255, 255 )
        DrawRect( 15, 15, 410, 90 )
        SetColor( 0, 0, 0 )
        DrawRect( 20, 20, 400, 80 )
        SetColor( 255, 255, 255 )
        DrawRect( 20, 20, bulletPower * 0.4, 80 )      
        Return 0
    End

End
 

Dubbsta

Active Member
Joined
Jul 13, 2017
awesome thanks for your help! what i meant was i had a list of bullets and put this around the list and it fired the whole list at once, im sure i just put stuff in the wrong place
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
It will work fine with a list. In my code, bullets are very simplistic: I set firedBullet to True, and then the next OnRender() flashes the screen and sets it to false again. But you could do away with firedBullet (or leave it if you want the flash) and instead add a new bullet to a list. It's up to you what sort of life cycle bullets on the list will have.
 
Top Bottom