Catch the bouncing smiley

Discussion in 'Android Questions' started by Erel, Oct 18, 2010.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Attached is a small program of a bouncing (and rotating) smiley.

    As you can see in the image the background is not just a solid color.


    The code for this program:
    'Activity module
    Sub Process_Globals
    Dim x, y, vx, vy As Int
    Dim boxSize As Int
    Dim Rect As Rect
    Dim timer1 As Timer
    Dim SmileyBitmap As Bitmap
    Dim Degrees As Int
    End Sub

    Sub Globals
    Dim Canvas1 As Canvas
    Dim Bitmap1 As Bitmap
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
            vx = 
    10dip 'smiley speed
            vy = 10dip
            x = 
    50%x 'smiley first position (middle of the screen
            y = 50%y
            boxSize = 
    40dip 'smiley size
    File.DirAssets, "smiley.gif"'load the smiley file
        End If
    Activity.LoadLayout("smiley.bal"'load the designer layout (just the gradient color in this case).
        Canvas1.Initialize(Activity'Initialize the canvas (drawer) and makes it draw on the activity (form).
        Bitmap1.Initialize3(Canvas1.Bitmap) 'Save a copy of the current background. This bitmap will be used to erase the smiley each time.
    End Sub
    Sub Activity_Pause
        Timer1.Enabled = 
    End Sub
    Sub Activity_Resume
        Timer1.Enabled = 
    End Sub
    Sub Timer1_Tick
    If x  + boxSize > 99%x Then
            vx = -
    1 * Abs(vx)
    Else If x < 1%x Then
            vx = 
    End If
    If y  + boxSize > 99%y Then
            vy = -
    1 * Abs(vy)
    Else If y < 1%y Then
            vy = 
    End If
    Rect.Top = y
    Rect.Left = x
    Rect.Bottom = y + boxSize
    Rect.Right = x + boxSize
    RectRect'Erase the previous smiley
        x = x + vx
        y = y + vy
    Rect.Top = y
    Rect.Left = x
    Rect.Bottom = y + boxSize
    Rect.Right = x + boxSize
        Degrees = (Degrees + 
    10Mod 360
    NullRect, Degrees) 'Draw the new smiley
    End Sub
    Sub Activity_Touch(Action As Int, tx As Float, ty As Float)
    If Not(Action = Activity.ACTION_DOWN) Then Return
    If tx > x - boxSize AND tx < x + boxSize AND _
            ty > y - boxSize 
    AND ty < y + boxSize Then
            vx = -vx 
    'change the smiley direction if the user pressed on it
            vy = -vy
    End If
        Canvas1.DrawCircle(tx, ty, 
    5dipColors.Red, False2dip)
    Activity.Invalidate3(tx - 7dip, ty - 7dip, tx + 7dip, ty + 7dip)
    End Sub
    You are more than welcome to try to run it on your android device. :)

    Attached Files:

  2. Cableguy

    Cableguy Expert Licensed User

    Is This the first App created by Our Basic4Android IDE????
    Cool, I will try that...

    WIll it be possible to have a note sheet in the IDE?, justo note things to do, goals, and specifics descriptions like variables purpose, and so on?
  3. Erel

    Erel Administrator Staff Member Licensed User

    There are several internal test applications. This is for sure the first one with a smiley.

    I guess that the regular comments do target most of these features...
  4. Cableguy

    Cableguy Expert Licensed User

    Yes they do, but having to scroll a high numbered code line app, just to find a description, is tedious...and time consuming...
    In Basic4ppc, I use a "module" and strip it of the Globals, and just place comments, but sometimes I forget to add the " ' ", so the app misfires...
    A note sheet would be nice, but not essencial...
  5. corwin42

    corwin42 Expert Licensed User

    Works great on Vodafone 845 with 240x320 screen resolution.

    Hope we will see a first beta soon. :)
  6. derez

    derez Expert Licensed User

    I got my development environment set up and it runs nicely on "my" emulator :)

    Pressing the mouse button while on the top bar of the emulator window freeze the smiley's motion !

    Erel, keep on the good work, I'm ready for more :icon_clap:

    Attached Files:

    Last edited: Oct 19, 2010
  7. Erel

    Erel Administrator Staff Member Licensed User

    This is a Windows / Emulator phenomenon.

    Thank you!
  8. Cor

    Cor Active Member Licensed User

    is there a project to download for the smiley program?


  9. Erel

    Erel Administrator Staff Member Licensed User

    The source was added to the first post.
  10. Brad

    Brad Active Member Licensed User

    I downloaded the zip file but had a problem extracting the smiley.b4a file. Couldn't find the file even though it's visible.
  11. Erel

    Erel Administrator Staff Member Licensed User

    I've updated the zip file. Please try to download it again.
  12. Brad

    Brad Active Member Licensed User

    That worked..thanks!
  13. Widget

    Widget Well-Known Member Licensed User

    Nice animation. I did notice if the bitmap is not perfectly round, then you need to increase the sides of the erasing square to be the length of the diagonal of the bitmap. Otherwise it will leave remnants of the previously rotated bitmap.

    Rect.Top = y
    Rect.Left = x
    Rect.Bottom = y + Sqrt(boxSize*boxSize*2)   'Assuming bitmap is square
        Rect.Right = x + Sqrt(boxSize*boxSize*2)
    RectRect'Erase the previous smiley

    Attached Files:

  14. jag

    jag Member Licensed User

    Not sure if this is the right place, but... I took Erel's bounce code, combined with the transparent window from "SimpleDrawFunctions" in the Beginner's Guide to create a variation on the theme.
    Additional info:
    - random-color generator
    - array of labels used as color objects
    - view placement and sizing by % to fit various screens
    - adjust ball (window) size according to landscape or portrait mode
    - no layout, everything is created at runtime
    - lots of comments

    I hope other newbies find it useful.

    Attached Files:

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice