How to address a control

mhc

Member
Licensed User
Longtime User
Erel,

thanks for your answer.
I have have a Timer-Sub which checks if there is a collision between a ball and a pad:

B4X:
Sub tmBall_Tick
...

'check collision ball and pad
'1. check collision ball and players pad   
If (imgBall.Top<=pnlPlayer.Top+pnlPlayer.Height) AND (imgBall.Left>=pnlPlayer.Left-imgBall.Width-1 AND imgBall.Left<=pnlPlayer.Left+pnlPlayer.Width+1) Then
collision(pnlPlayer)      [COLOR="Green"]'we call the sub with the name of the pad[/COLOR]
End If
'2. check collision ball and computers pad  
If (imgBall.Top+imgBall.Height>=pnlComputer.Top) AND (imgBall.Left>=pnlComputer.Left-imgBall.Width-1 AND imgBall.Left<=pnlComputer.Left+pnlComputer.Width+1) Then 
collision(pnlComputer)
End If
...   
End Sub

The Sub Collision control the ongoing game depending on which kind of pad has the collision:

B4X:
Sub Collision(pad)
'we detect the position where the pad hit the ball

pos=imgBall.Left-pad.Left+imgBall.Width  [COLOR="Red"]<== doesn't work cause pad is a String[/COLOR]
If pad="pnlComputer" Then
  [COLOR="DarkOrange"] do something[/COLOR]
  [COLOR="darkorange"]... and so on[/COLOR]

End Sub

I know that I can write to subs Collision (i.e. coll1, coll2) and call them depending on the kind of pad. But how can I realize it with only one sub using parameters?

mhc
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
in B4PPC we can use the control keyword to access a control using a string instead of its name. i.e. Control("name",Panel).Width
How can we do this with B4A?
mhc

Seems, no reply to this question, how to address the existing views, if we know a name common part ?
Say, check states of "CheckBox1", "CheckBox2", "CheckBox3"....in a loop, addressing views like "CheckBox" + i.

Oops, EDIT: found reason
 
Last edited:
Upvote 0
Top