Android Question Call Sub with a value

Douglas Farias

Expert
Licensed User
Longtime User
hi how can i make a button to click and return a value to my sub?
exemple when i press botao1 it call the sub Surface_Draw and the gamerunning = false

i need to make when button1 is click it show me a msgbox("laalla","lalal")

B4X:
Sub Surface_Draw(AC As AS_Canvas)
if gamerunning = false then
msgbox("laalla","lalal")
else if gamerunning = true then
msgbox("lelele","lele")
end sub

Sub botao1_Click

'What i use here to call the Surface_Draw with gamerunning = false?

End Sub
 

Douglas Farias

Expert
Licensed User
Longtime User
dont work =( i tryed this too i realy dont know *-*
here the error image and the sub image
 

Attachments

  • 777.png
    777.png
    119.8 KB · Views: 292
  • 888.png
    888.png
    131.4 KB · Views: 272
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
i need call gameruning = false
for start the begin menu
have other way to call only gameruning = false in
Sub Surface_Draw(AC As AS_Canvas) ?
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
in the flappy birds exemple
when you start the app show u a menu with

'draw title text
shadowText("CloneyBird",50%x,20%y,AC)
shadowText("Tap to start",50%x,30%y,AC)
shadowText("Get Ready!",50%x,50%y,AC)

and when you die show u

shadowText("Total Score: " & score,50%x,20%y,AC)
shadowText("High Score: " & highscore,50%x,30%y,AC)
If restartCD < 0 Then
shadowText("Flap to Restart",50%x,40%y,AC)

i need to make when you die back to start app
'draw title text
shadowText("CloneyBird",50%x,20%y,AC)
shadowText("Tap to start",50%x,30%y,AC)
shadowText("Get Ready!",50%x,50%y,AC)

when you flap to restart back to begin of app, bird flying and
this mensage
'draw title text
shadowText("CloneyBird",50%x,20%y,AC)
shadowText("Tap to start",50%x,30%y,AC)
shadowText("Get Ready!",50%x,50%y,AC)

Sub Surface_Draw(AC As AS_Canvas)
If gamerunning = False Then

i need call surface_drawn
gameruning = false
when you die to show the begin of app

sorry for my english


B4X:
#Region  Project Attributes
    #ApplicationLabel: Cloney Bird
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: false
    #IncludeTitle: false
#End Region

Sub Process_Globals
    Type typbird(x As Float, y As Float, width As Int, height As Int, rotation As Float, my As Float, rotationspeed As Float, bmp As Bitmap)
    Type typpipe(x As Float, y As Float, scored As Boolean)
End Sub

Sub Globals
    Dim highscore As Int
    Dim speed As Float = 3
    Dim restartCD As Int = 60
    Dim score As Int = 0
    Dim birdColour As Int = Rnd(0,4)
    Dim pipeCD As Int = 50
    Dim birddead As Boolean = False
    Dim bg As Int = Rnd(0,2)
    Dim scaleX As Float
    Dim scaleY As Float
    Dim pipes As List
    Dim scorefile As RandomAccessFile
    Dim img_ground As Bitmap
    Dim img_pipe As Bitmap
    Dim mySurface As AcceleratedSurface
    Dim img_BirdStrip As Bitmap
    Dim IU As AS_ImageUtils
    Dim Secs As Double
    Dim gameBackground(2) As Bitmap
    Dim img_Bird(12) As Bitmap
    Dim img_BirdStrip As Bitmap
    Dim Backing As Bitmap
    Dim myBird As typbird
    Dim screenX As Int
    Dim screenY As Int
    Dim gamerunning As Boolean = False
    Dim birdAngle As Int = 0
    Dim sp As SoundPool
    Dim snd_flap As Int
    Dim snd_hit As Int
    Dim wRect As Rect
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
  
    If File.Exists(File.DirInternal,"scores.dat") Then
        scorefile.Initialize(File.DirInternal,"scores.dat",True)
        highscore = scorefile.ReadInt(0)
        scorefile.Close
    Else
        highscore = 0
    End If
  
    'Initializes the accelerated surface
    mySurface.Initialize("Surface", True)
  
    sp.Initialize(8)
    snd_flap = sp.Load(File.DirAssets, "select.wav")
    snd_hit = sp.Load(File.DirAssets, "shoot.wav")
    'Loads background images
    Backing = IU.LoadscaledBitmap(File.DirAssets, "day_background.png",-1,-1,True)
    gameBackground(0) = IU.Crop(Backing,0,0,400,500)
    gameBackground(1) = IU.Crop(Backing,400,0,400,500)
    pipes.Initialize
    img_ground = IU.LoadscaledBitmap(File.DirAssets,"ground.png",20%x,20%y,True)
    img_pipe = IU.LoadScaledBitmap(File.DirAssets,"pipe.png",-1,-1,True)
    myBird.Initialize
  
    'add view to activity
    Activity.AddView(mySurface, 0, 0, 100%x, 100%y)
    screenX = mySurface.Width
    screenY = mySurface.Height
  
    scaleX = (screenX/400)
    scaleY = (screenY/600)
    speed = speed * scaleX
  
    'scale pipe image
    img_pipe = IU.CreateScaledBitmap(img_pipe,12.5%x,133%y,True)
  
    'scale background images and copy into array
    gameBackground(0) = IU.CreateScaledBitmap(gameBackground(0),100%x,80%y,True)
    gameBackground(1) = IU.CreateScaledBitmap(gameBackground(1),100%x,80%y,True)
  
    'load bird frames
    img_BirdStrip = IU.LoadscaledBitmap(File.DirAssets, "bird.png",-1,-1,True)
    For x = 0 To 11
        img_Bird(x) = IU.Crop(img_BirdStrip,x*34,0,34,24)
        img_Bird(x) = IU.CreateScaledBitmap(img_Bird(x),8.5%x,4%y,True)
    Next
  
    'set bird start position
    myBird.x = 20%x
    myBird.y = 50%y
  
    'initialise rect for screen flash on death
    wRect.Initialize(0,0,screenX,screenY)
  
End Sub

Sub Activity_Resume
    'Starts the timer
    mySurface.StartRegularDraw(30) '30 fps
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    mySurface.StopRegularDraw
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
'Return True
End Sub

Sub Surface_Draw(AC As AS_Canvas)
    'TITLE SCREEN
    If gamerunning = False Then
        'increment secs variable
        Secs = Secs + speed
        'set bird image
        myBird.bmp = IU.createscaledbitmap(img_Bird((birdColour*3)+(Secs/8 Mod 3)),img_Bird(0).Width,img_Bird(0).Height,True)
      
        'draw background
        AC.DrawBitmapAt(gameBackground(bg),0,0)
      
        'draw ground
        For x = 0 To (mySurface.Width/img_ground.Width)+5
            AC.DrawBitmapAt(img_ground,(x*img_ground.Width-((Secs) Mod (img_ground.Width))),80%y)
        Next
      
        'draw bird
        AC.DrawBitmapAt(myBird.bmp,myBird.x,myBird.y+(10*Sin(Secs/10)))
      
        'draw title text
        shadowText("CloneyBird",50%x,20%y,AC)
        shadowText("Tap to start",50%x,30%y,AC)
        shadowText("Get Ready!",50%x,50%y,AC)
    End If
  
  
    'MAIN GAME LOOP
    If gamerunning = True Then
        'increment secs variable
        Secs = Secs + speed
      
        'draw background
        AC.DrawBitmapAt(gameBackground(bg),0,0)
      
        'decrease pipeCD
        pipeCD = pipeCD - speed
        'create pipe
        If pipeCD < 0 Then
            pipeCD = 40%x
            Dim Pipe As typpipe
            Pipe.Initialize
            Pipe.x = 120%x
            Pipe.Y = Rnd(30%y,60%y)-(img_pipe.Height*0.5)
            Pipe.scored = False
            pipes.add(Pipe)
        End If
        'update pipes
        Dim Pipe As typpipe
        For i = 0 To pipes.Size - 1
        Pipe = pipes.Get(i)
            Pipe.x = Pipe.x - speed
            'check for collision
            If (myBird.x+img_Bird(0).Width > Pipe.x) AND (myBird.x < (Pipe.x+img_pipe.Width)) Then
                If (myBird.y < Pipe.y+(img_pipe.Height*0.4375)) OR (myBird.y+img_Bird(0).Height > Pipe.y+(img_pipe.Height*0.5625)) Then
                    If birddead = False Then
                        sp.Play(snd_hit,1,1,1,0,1)
                        AC.DrawRect(wRect,Colors.White,True,1,True)
                      
                    End If
                    birddead = True
                    speed = 0
                    Exit
                Else
                    If Pipe.scored = False Then
                        Pipe.scored = True
                        score = score + 1
                    End If
                End If
            End If
            If Pipe.x < -100 Then
            'delete pipe
                pipes.RemoveAt(i)
                Exit
            End If      
        Next
      
      
      
        'stop bird going too fast
        If myBird.my < (10*scaleY) Then
            myBird.my = myBird.my + (0.5*scaleY)
        End If
        myBird.y = myBird.y + (myBird.my)
      
        'stop bird going off bottom of screen
        If myBird.y > 80%y-img_Bird(0).height Then
            speed = 0
            birddead = True
            myBird.my = 0
            myBird.y = 80%y-img_Bird(0).height
        End If
      
        'set birdAngle
        'mybird my goes from -6 to 10
        'range of 16
        'angle range of 335 to 70 = 95
        '95 / 16 = 5.9 degrees + 335 per my over -6
        '=(((my/scaley)+6)*5.9)+335
        birdAngle = (((myBird.my/scaleY)+6)*5.5)+335
      
        'set bird image
        myBird.bmp = img_Bird((birdColour*3)+(Secs/8 Mod 3))
      
      
        'Draw pipes
        For i = 0 To pipes.Size - 1
        Dim Pipe As typpipe = pipes.Get(i)
            AC.DrawBitmapAt(img_pipe, Pipe.x,Pipe.Y)
        Next
  
        'draw ground
        For x = 0 To (mySurface.Width/img_ground.Width)+5
            AC.DrawBitmapAt(img_ground,(x*img_ground.Width-((Secs) Mod (img_ground.Width))),80%y)
        Next
      
        'draw bird
        'AC.DrawBitmapAt(myBird.bmp,myBird.x,myBird.y)
      
        AC.MatrixSetRotate2(birdAngle, Bit.ShiftRight(myBird.bmp.Width, 1), Bit.ShiftRight(myBird.bmp.Height, 1))
        AC.DrawBitmapWithMatrixAt(myBird.bmp, myBird.x, myBird.Y, True)
  
      
      
        'draw score if bird Alive or end game if birddead
        If birddead = False Then
            shadowText(score,50%x,10%y,AC) ' draw score on screen
        Else
            restartCD = restartCD - 1 'decrease restartCD
            'write highscore file if new highscore
            If score > highscore Then
                highscore = score
                'write highscore to file
                scorefile.Initialize(File.DirInternal,"scores.dat",False)
                scorefile.writeInt(highscore,0)
                scorefile.Close
            End If
            shadowText("Total Score: " & score,50%x,20%y,AC)
            shadowText("High Score: " & highscore,50%x,30%y,AC)
            If restartCD < 0 Then
                shadowText("Flap to Restart",50%x,40%y,AC)
            End If
        End If
    End If
End Sub

Sub shadowText(text As String, x As Int, y As Int, mac As AS_Canvas)
    mac.DrawText(text,x,y,Typeface.DEFAULT_BOLD,24dip,Colors.Black,mac.ALIGN_CENTER)
    mac.DrawText(text,x-2dip,y-2dip,Typeface.DEFAULT_BOLD,24dip,Colors.white,mac.ALIGN_CENTER)
End Sub

Sub Surface_Touch(Action As Int, X As Int, Y As Int, Event As Object)

    If Action = 0 AND gamerunning = True Then
        If birddead = False Then
            myBird.my = -6*scaleY
            sp.Play(snd_flap,1,1,1,0,1)
            'sp.Play(LaserSound, 1 - (LaserShot.X / 100%x), LaserShot.X / 100%x, 1, 0, 1)
        Else If birddead = True AND restartCD < 0 Then
            birddead = False 'revive bird
            pipes.Clear 'remove all pipes
            speed = 3 'reset speed
            speed = speed * scaleX
            myBird.Y = 50%y
            myBird.my = -6*scaleY
            restartCD = 60
            pipeCD = 100
            birdColour = Rnd(0,4)
            bg = Rnd(0,2)
            Secs = 0
            score = 0
        End If
    Else
        gamerunning = True
        myBird.my = -6*scaleY
    End If
End Sub


'to do
'add music
'add sound toggle
'add screen flash on death - done
'add sound effects - done
'add rotation to bird sprite based on my value - done
'add title screen - done
'add icon - done
'draw title text
shadowText("CloneyBird",50%x,20%y,AC)
shadowText("Tap to start",50%x,30%y,AC)
shadowText("Get Ready!",50%x,50%y,AC)
'draw title text
shadowText("CloneyBird",50%x,20%y,AC)
shadowText("Tap to start",50%x,30%y,AC)
shadowText("Get Ready!",50%x,50%y,AC)
 
Last edited:
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
nop =(
dowload this exemple
the game go begin with a bird flying, and a mensage for you , ready ? tap for begin .
OK you tap and the game begins , flappy birds normal.

but when you die show you tap for try again, and it begins the game again
no with bird flying and mesagem the ready ? tap for begin . *-*

Surface_Draw start only in the begin of the app with value gameruning = false
and in Surface_Draw
gameruning = false is what i need *-*
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
when i try your code come this error

Sub botao1_Click
gamerunning = false
Surface_Draw(AC)End Sub


the ac is not public *-* i dont know why
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As I already told you in post #5 you must replace AC by the Canvas you use.
I don't know what Canvas you have defined, and I still don't know what exactly what you want to do.
In the code snippets you posted you never call Surface_Draw(AC), so I don't know if you already have used it and where.
Without more information we must try to guess what you are doing.
 
Upvote 0
Top