Hi All,
I am building a card game. I would like to show some fireworks when the user wins.
The way I imagined doing it was to overlay a panel with a Colors.Transparent background, and then draw particles on the panel.
So, the code below is my first naive attempt...but I'm not getting anything on the screen as expected. (There are no Designer screens, just cut/paste the code but watch out for the wrap).
So, I put a label on the Activity (to make sure the panel is covering things), and then lay a panel over it. So far, so good.
Then I initialise 100 particles, and start a timer that is supposed to update them 5 times per second (higher framerate later...I just wanted to see it working before tweaking).
With breakpoints, the timer does the tick, the particles are attempting to be drawn...it's just that they never seem to appear.
I'm sure it's me being naive...what have I done wrong here?
Thanks,
-Ken
----------------------------------------------
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type Particle(x,y,dx,dy As Int, g As Int,life,dlife As Int, Colour, dColour As Int, size As Int)
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim particles(256) As Particle
Dim particlelist As List
Dim particlecanvas As Canvas
Dim particleview As View
Dim particletmr As Timer
End Sub
Sub initps(vw As View)
Dim r As Rect
r.Initialize(0,0,vw.Width,vw.Height)
particlelist.Initialize
For i=0 To 255
particles(i).Initialize
finishparticle(i)
Next
particleview = vw
particlecanvas.Initialize(particleview)
'particlecanvas.DrawRect(r, Colors.Transparent,True,0)
End Sub
Sub newparticle(x As Int,y As Int,dx As Int,dy As Int,g As Int,life As Int,dlife As Int,c As Int,dc As Int,size As Int) As Int
Dim i As Int
i = particlelist.Get(0)
particlelist.RemoveAt(0)
particles(i).x = x
particles(i).y = y
particles(i).dx = dx
particles(i).dy = dy
particles(i).g = g
particles(i).life = life
particles(i).dlife = dlife
particles(i).Colour = c
particles(i).dColour = dc
particles(i).size = size
Return i
End Sub
Sub finishparticle(i As Int)
particles(i).x=0
particles(i).y=0
particles(i).dx=0
particles(i).dx=0
particles(i).g=0
particles(i).life=0
particles(i).dlife=0
particles(i).Colour=0
particles(i).dColour=0
particles(i).size=0
particlelist.Add(i)
End Sub
Sub runparticles
Dim p As Particle
For i = 0 To 255
If particles(i).life>0 Then
' erase the old particle
Dim r As Rect
r.Initialize(particles(i).x,particles(i).y,particles(i).size,particles(i).size)
'particlecanvas.DrawRect(r,Colors.Transparent,True,0)
'particleview.Invalidate2(r)
' age particle, draw if still alive.
particles(i).life=particles(i).life-particles(i).dlife
If particles(i).life<=0 Then
finishparticle(i)
Else
particles(i).x = particles(i).x + particles(i).dx
particles(i).y = particles(i).y + particles(i).dy
If particles(i).x<0 OR particles(i).x>100%x OR particles(i).y<0 OR particles(i).y>100%y Then
finishparticle(i)
Else
r.Initialize(particles(i).x,particles(i).y,particles(i).size,particles(i).size)
particlecanvas.DrawRect(r,particles(i).Colour,True,0)
particleview.Invalidate2(r)
End If
End If
End If
Next
DoEvents
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim pnl As Panel
Dim lbl As Label
pnl.Initialize("")
pnl.Color = Colors.ARGB(255,0,68,0)
lbl.Initialize("")
lbl.Text="Testing."
lbl.textsize=36
lbl.TextColor=Colors.White
Activity.AddView(lbl, 20%x, 40%y, 60%x, 60dip)
Activity.AddView(pnl,0,0,100%x,100%y)
initps(pnl)
particletmr.Initialize("particletmr",200)
particletmr.Enabled = True
Activity.Invalidate
For i = 0 To 100
newparticle(i,100%y,0,-1-(i/2),0,100%y,1,Colors.ARGB(Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256)),0,Rnd(2,5))
Next
End Sub
Sub particletmr_tick
runparticles
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
I am building a card game. I would like to show some fireworks when the user wins.
The way I imagined doing it was to overlay a panel with a Colors.Transparent background, and then draw particles on the panel.
So, the code below is my first naive attempt...but I'm not getting anything on the screen as expected. (There are no Designer screens, just cut/paste the code but watch out for the wrap).
So, I put a label on the Activity (to make sure the panel is covering things), and then lay a panel over it. So far, so good.
Then I initialise 100 particles, and start a timer that is supposed to update them 5 times per second (higher framerate later...I just wanted to see it working before tweaking).
With breakpoints, the timer does the tick, the particles are attempting to be drawn...it's just that they never seem to appear.
I'm sure it's me being naive...what have I done wrong here?
Thanks,
-Ken
----------------------------------------------
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type Particle(x,y,dx,dy As Int, g As Int,life,dlife As Int, Colour, dColour As Int, size As Int)
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim particles(256) As Particle
Dim particlelist As List
Dim particlecanvas As Canvas
Dim particleview As View
Dim particletmr As Timer
End Sub
Sub initps(vw As View)
Dim r As Rect
r.Initialize(0,0,vw.Width,vw.Height)
particlelist.Initialize
For i=0 To 255
particles(i).Initialize
finishparticle(i)
Next
particleview = vw
particlecanvas.Initialize(particleview)
'particlecanvas.DrawRect(r, Colors.Transparent,True,0)
End Sub
Sub newparticle(x As Int,y As Int,dx As Int,dy As Int,g As Int,life As Int,dlife As Int,c As Int,dc As Int,size As Int) As Int
Dim i As Int
i = particlelist.Get(0)
particlelist.RemoveAt(0)
particles(i).x = x
particles(i).y = y
particles(i).dx = dx
particles(i).dy = dy
particles(i).g = g
particles(i).life = life
particles(i).dlife = dlife
particles(i).Colour = c
particles(i).dColour = dc
particles(i).size = size
Return i
End Sub
Sub finishparticle(i As Int)
particles(i).x=0
particles(i).y=0
particles(i).dx=0
particles(i).dx=0
particles(i).g=0
particles(i).life=0
particles(i).dlife=0
particles(i).Colour=0
particles(i).dColour=0
particles(i).size=0
particlelist.Add(i)
End Sub
Sub runparticles
Dim p As Particle
For i = 0 To 255
If particles(i).life>0 Then
' erase the old particle
Dim r As Rect
r.Initialize(particles(i).x,particles(i).y,particles(i).size,particles(i).size)
'particlecanvas.DrawRect(r,Colors.Transparent,True,0)
'particleview.Invalidate2(r)
' age particle, draw if still alive.
particles(i).life=particles(i).life-particles(i).dlife
If particles(i).life<=0 Then
finishparticle(i)
Else
particles(i).x = particles(i).x + particles(i).dx
particles(i).y = particles(i).y + particles(i).dy
If particles(i).x<0 OR particles(i).x>100%x OR particles(i).y<0 OR particles(i).y>100%y Then
finishparticle(i)
Else
r.Initialize(particles(i).x,particles(i).y,particles(i).size,particles(i).size)
particlecanvas.DrawRect(r,particles(i).Colour,True,0)
particleview.Invalidate2(r)
End If
End If
End If
Next
DoEvents
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim pnl As Panel
Dim lbl As Label
pnl.Initialize("")
pnl.Color = Colors.ARGB(255,0,68,0)
lbl.Initialize("")
lbl.Text="Testing."
lbl.textsize=36
lbl.TextColor=Colors.White
Activity.AddView(lbl, 20%x, 40%y, 60%x, 60dip)
Activity.AddView(pnl,0,0,100%x,100%y)
initps(pnl)
particletmr.Initialize("particletmr",200)
particletmr.Enabled = True
Activity.Invalidate
For i = 0 To 100
newparticle(i,100%y,0,-1-(i/2),0,100%y,1,Colors.ARGB(Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256)),0,Rnd(2,5))
Next
End Sub
Sub particletmr_tick
runparticles
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub