Android Question Quick Response Game, Won't register clicks, am I on the right track??

Ripley

Member
Licensed User
Longtime User
Rookie here. Working on a basic level design for a quick response game I want to build. Basically a proof of concept to show what I can actually make work.

I have many imageviews flashing random images as well as a target image that changes periodically. You try to click the images that match the targets and your points go up. Right now it only registers maybe one out of 10 clicks.

My code is probably bloated and ugly to you veterans but I need to know if I'm even on the right track with how to implement my idea or am I too ambitious? I know I should probably switch my image click to only one event using a SELECT as I've seen in the manuals. But don't think that is going to solve my problem.

Perhaps with all the randomizers and timers I am just over loading the system?? I'm now a licensed member so perhaps should look at game engines?

Thanks for your help!
 

Attachments

  • QuickResponse.zip
    20.3 KB · Views: 144

RandomCoder

Well-Known Member
Licensed User
Longtime User
First of all I'm no pro at B4A, and I know nothing about game animation, but I do like to tinker! I've done what I can with your original code to get it in line with what I think you had in mind. I admire you're patience as all of those ImageViews and repeated lines of code must have taken quite some time to write. It's always a good idea to try and stop yourself once you've written the same or very similar lines of code more than a couple of times, maybe its possible to write a subroutine that will do the work for you! Personally I would have been inclined to create an array of ImageViews as this would have made it easier to iterate through them all. I also think that it would be faster and better to use a canvas instead, then randomly draw filled circles. No need to load bitmaps at all. But like I said, I'm no expert and have no experience in this field.
Anyways, its been a nice distraction and I hope you like the code. All changes have been commented with ' ##### before the comment and all ImageViews in the designer have had they're event names changed.

Kind Regards,
RandomCoder
 

Attachments

  • Modified Code.zip
    19.7 KB · Views: 149
Upvote 0

Ripley

Member
Licensed User
Longtime User
Thanks RandomCoder I'll take a look and get back to you. Was hoping to do some hard-core coding this weekend and really wanted to be making progress, not bang my head against the desk like I have the last three nights....maybe this will help. Arrays and Canvas are something I still need to read and understand better...
 
Upvote 0

Ripley

Member
Licensed User
Longtime User
So 36 panels? Would that be better than bitmaps?
Also another newb question How does a person open a zipped code and put everything back where it's suppose to be? In the file RandomCoder made I have a .b4a file a .b4a.meta file, A FILE file and Objects....
 
Upvote 0

Ripley

Member
Licensed User
Longtime User
Damn it RandomCoder your code works and you have now robbed me of most of my sense of acomplishment in designing this!!! BUT you have set me on the right path so THANK YOU for that. I will study and try to wrap my head around these Loops and Lists you made use of, because they are still confusing me.

I still wonder if a game engine would be worth my time but I will continue to refine this first.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'll try to explain anything you need explaining in the code. I don't think its the most efficient way of doing what you want, but it's in keeping with your original design.

Ps. The design was all yours Ripley, I've just slightly refined the code

Regards
RandomCoder
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi Ripley,

Sorry to hear that you're still having some issues getting all touch events to trigger using the ImageView approach. As I mentioned before, I think that using a canvas would be more efficient and as I've not had any need to use Canvas's yet I thought I'd have a quick play tonight to see what I could come up with (please see code below). It's not quite complete as I didn't wanted to rob you any more of a sense of accomplishment
For starters, I'm displaying all 36 spots. This was just so that I could check the positioning of them as I tend not to use the designer, I find it faster and more flexible to do my layouts in code. It also allows you to see that they are all randomly changing, but you can choose to hide as many as you want.
New targets can be added by simply calling "setTarget(targetColour.Size)" which will add another target, to change for instance just the first target you could call "setTarget(0)" etc.
Also note that you will need to ignore touch events on the targets themselves, I'm sure you can manage this, it's just a case of ignoring any touches that are below the lowest spot positions.
The scoring should be easy enough, just compare the touched colour against the colours stored in the targetColour list.
I also didn't know if you planned to use this in portrait only or portrait and landscape screen orientations. It can handle either as is, but you will probably want to maintain the selected spot and target colours on screen orientation changes so that the play can continue unaffected by the rotation change?

Any questions, feel free to ask. I'll do my best to answer them within my abilities

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

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private pulseTimer As Timer
    Private colourList As List
    Private targetColour As List
    Private colouredSpots As Map
    Type spotDetail(x1 As Int, y1 As Int, size As Int, colour As Int)
    Private x1, y1, x2, y2 As Int ' x1,y1 = top left, x2,y2 = bottom right
    Private level As Int
End Sub

Sub Globals
    Private gameBoard As Canvas
    Private gameLabel As Label
    Private touchCount As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    initialise
    If FirstTime Then
        level = 0
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub Activity_Touch (Action As Int, X As Float, Y As Float)
    ' This event if triggered several times, were only interested in the first touch
    If Action = Activity.ACTION_DOWN Then
        ' Counter only added to enable touches to be recorded in gamelabel
        touchCount = touchCount + 1
        gameLabel.Text = touchCount
        ' Change label colour to show what colour was touched
        gameLabel.Color = gameBoard.Bitmap.GetPixel(X, Y)
    End If
End Sub

Private Sub initialise()
    ' Add canvas to avtivity
    gameBoard.Initialize(Activity)
    gameLabel.Initialize("gameLabel")
    gameLabel.Gravity = Gravity.CENTER
    gameLabel.Color = Colors.White
    Activity.AddView(gameLabel, 0, Activity.Height - 30dip, 100%x, 30dip)
    ' Set game board limits
    x1 = 50dip: y1 = 50dip
    x2 = Activity.Width - 100dip ' 50dip spacing on both sides (subtract 100dip from activity width)
    y2 = 60%y
    ' Load colour list
    SetColours
    ' Initialise and display spots
    InitialiseSpots
    RedrawSpots
    ' Start timing
    pulseTimer.Initialize("pulseTimer", 3000)
    pulseTimer.Enabled = True
    touchCount = 0
End Sub

Private Sub SetColours()
    ' Add more colours if required
    colourList.Initialize
    colourList.Add(Colors.Blue)
    colourList.Add(Colors.Cyan)
    colourList.Add(Colors.Green)
    colourList.Add(Colors.LightGray)
    colourList.Add(Colors.Magenta)
    colourList.Add(Colors.Red)
    colourList.Add(Colors.White)
    colourList.Add(Colors.Yellow)
    ' Initialise and set target colour
    targetColour.Initialize
    setTarget(0)
End Sub

Private Sub setTarget(num As Int)
    ' Randomly select a new target colour
    Dim newColour As Int
    newColour = GetRandomColour
    ' Make sure that target colour is not already in use
    Do Until targetColour.IndexOf(newColour) = -1
        newColour = GetRandomColour
    Loop
    ' If num is greater than list size add new target otherwise change existing target colour
    If num > (targetColour.Size - 1) Then
        targetColour.add(newColour)
    Else
        targetColour.Set(num, newColour)
    End If
End Sub

Private Sub GetRandomColour() As Int
    ' Randomly select a colour from the list of colours
    Return colourList.get(Rnd(0,colourList.Size))
End Sub

Private Sub InitialiseSpots()
    ' Each spot to have a fixed position in gameboard ### You could randomize if wanted?
    Dim spacingX As Int = (x2 - x1) / 5
    Dim spacingY As Int = (y2 - y1) / 5
    ' Populate the colouredSpots map with the location and colour of all 36 spots (6 x 6 grid)
    colouredSpots.Initialize
    Dim row, col, spotNumber As Int = 0
    For row = 0 To 5
        For col = 0 To 5
            ' Increment spot number and assign its position and random colour
            spotNumber = spotNumber + 1
            Dim detail As spotDetail
            detail.size = 20dip
            detail.x1 = x1 + (spacingX * col) + detail.size ' Offset by detail.size to centralise the grid
            detail.y1 = y1 + (spacingY * row) + ((spacingY/2) * (col Mod 2)) ' Offset alternate columns
            detail.colour = GetRandomColour
            ' Store details in the map
            colouredSpots.Put(spotNumber,detail)       
        Next
    Next
End Sub

Private Sub ChangeSpotColours()
    ' Randomly select a new colour for every spot
    Dim i As Int
    For i = 1 To colouredSpots.Size
        Dim detail As spotDetail
        detail = colouredSpots.get(i)
        detail.colour = GetRandomColour
        colouredSpots.Put(i, detail)
    Next
    ' Randomly select a new colour for every target
    For i = 0 To targetColour.Size - 1
        setTarget(i)
    Next
End Sub

Private Sub RedrawSpots()
    ' Update the gameBoard with the new spot colours
    Dim i As Int
    For i = 1 To colouredSpots.Size
        Dim detail As spotDetail
        detail = colouredSpots.get(i)
        gameBoard.DrawCircle(detail.x1,detail.y1,detail.size,detail.colour,True,0)
    Next
    ' Update the gameBoard with the new target colour(s) NB. New targets will be added as target list grows
    For i = 0 To targetColour.Size - 1
        gameBoard.DrawCircle(50dip + (60dip * i),Activity.Height - (gameLabel.Height + 50dip),25dip,targetColour.Get(i),True,0)
    Next
    ' Refresh the display
    Activity.Invalidate
End Sub

Private Sub pulseTimer_Tick()
    ' You can chose how the level system works
    level = level + 1
    ' This will add another target every turn up to a maximum of 5 targets (0 to 4 list indices)
    If targetColour.Size <= 4 Then
        setTarget(targetColour.Size)
    End If
    ' Update gameBoard with new coloured spots
    ChangeSpotColours
    RedrawSpots
End Sub
You can either copy and paste the code above into a new project or use the attached zip file.

Kind Regards,
RandomCoder
You can either copy and paste the code above into a new project or use the attached zip file.
 

Attachments

  • Reaction.zip
    7.5 KB · Views: 143
Upvote 0

Ripley

Member
Licensed User
Longtime User
Wow, mind = blown. This is my text book. I will study it tonight. Seems to work really well so far.
Thank you.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…