Trouble getting x and y position, on a canvas

SlavaVB

Member
Licensed User
Longtime User
Hi, i wrote an app recently that tells you the x and y of the place where you touch the screen, however it doesn't work if there's a canvas on top of it. Any ideas or suggestions?

Thanks!
 

klaus

Expert
Licensed User
Longtime User
... however it doesn't work if there's a canvas on top of it.
What do you mean with this? A canvas cannot hide anything directly.
A canvas has a target view, and it's this view that could hide something. But what target view do you have?
You must be much more precise on the conditions, on what you expect and what really doesn't work. The best way would be to post your project as a zip file with more detailed explanations.

Best regards.
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
The source code and a screen capture...

Sub Process_Globals

End Sub

Sub Globals
Dim Canvas1 As Canvas

End Sub

Sub Activity_Create(FirstTime As Boolean)
canvas1.Initialize(Activity)
Globe
End Sub


Sub Globe

Dim xx(100) As Double
Dim yy(100) As Double
Dim vx(100) As Double
Dim vy(100) As Double
Dim ax(100) As Double
Dim ay(100) As Double
Dim r(100) As Double
Dim dt As Double

Dim w As Double
Dim h As Double
Dim diffx As Double
Dim diffy As Double
Dim dist1 As Double
Dim dist2 As Double

Dim xval As Double
Dim yval As Double


w = Canvas1.Bitmap.Width
h = Canvas1.Bitmap.Height

xx(0) = w/2
yy(0) = h/2
vx(0) = Rnd(-5,5)
vy(0) = Rnd(-5,5)
ax(0) = 0
ay(0) = 0
r(0) = 100

For i = 1 To 99
xx(i) = Rnd(0,w)
yy(i) = Rnd(0,h)
vx(i) = Rnd(-20,20)
vy(i) = Rnd(-20,20)
ax(0) = 0
ay(0) = 0
r(i) = Rnd(10,15)
Next

dt = .1


For a = 1 To 1000000


For i = 0 To 99

For j = 0 To 99
ax(j) = 0.0;
ay(j) = 0.0;
Next

For j = 0 To 99

If i <> j Then
diffx = xx(i) - xx(j)
diffy = yy(i) - yy(j)
dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
dist2 = r(i) + r(j)

If dist1 < dist2 Then
deltaDistance = (dist2 - dist1)

xval = (((deltaDistance * 10.0) / dist2) * diffx)
yval = (((deltaDistance * 10.0) / dist2) * diffy)

ax(i) = ax(i) + xval
ay(i) = ay(i) + yval

End If

End If
Next

vx(i) = (vx(i) + (ax(i) * dt)) *.999
vy(i) = (vy(i) + (ay(i) * dt)) *.999
xx(i) = xx(i) + (vx(i) * dt)
yy(i) = yy(i) + (vy(i) * dt)

If xx(i)-r(i) < 0 Then
vx(i) = -vx(i)
xx(i) = 0 + r(i)
End If
If yy(i)-r(i) < 0 Then
vy(i) = -vy(i)
yy(i) = 0 + r(i)
End If
If xx(i)+r(i) > w Then
vx(i) = -vx(i)
xx(i) = w - r(i)
End If
If yy(i)+r(i) > h Then
vy(i) = -vy(i)
yy(i) = h - r(i)
End If


Next

Canvas1.DrawColor(Colors.Black)

Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
For i = 1 To 99
Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
Next
Activity.Invalidate
DoEvents



Next


End Sub



screen capture on youtube... /watch?v=XpvudNE_82E
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
I tried...


Sub Activity_Touch (Action As Int, X As Float, Y As Float)
Msgbox("it works!","")
End Sub


but it doesnt get called when i touch the screen
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Another question... do you know how to make infinite loops?

i tried do while true ... loop

but it couldn't see the end sub, because of it, so instead i used..

For a = 1 To 1000000 ... next


is there another way to loop infinitly?

Thanks
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
I just redid my app to run off a timer, but now i cant get it to compile, i moved the declarations up to the top, so that they are non volatile. heres my code now....



Sub Process_Globals
Dim Timer1 As Timer

Dim xx(100) As Double
Dim yy(100) As Double
Dim vx(100) As Double
Dim vy(100) As Double
Dim ax(100) As Double
Dim ay(100) As Double
Dim r(100) As Double
Dim dt As Double

Dim w As Double
Dim h As Double
Dim diffx As Double
Dim diffy As Double
Dim dist1 As Double
Dim dist2 As Double

Dim xval As Double
Dim yval As Double


End Sub

Sub Globals
Dim Canvas1 As Canvas



End Sub

Sub Activity_Create(FirstTime As Boolean)
canvas1.Initialize(Activity)





Globe
End Sub


Sub Globe





w = Canvas1.Bitmap.Width
h = Canvas1.Bitmap.Height

xx(0) = w/2
yy(0) = h/2
vx(0) = Rnd(-5,5)
vy(0) = Rnd(-5,5)
ax(0) = 0
ay(0) = 0
r(0) = 100

For i = 1 To 99
xx(i) = Rnd(0,w)
yy(i) = Rnd(0,h)
vx(i) = Rnd(-20,20)
vy(i) = Rnd(-20,20)
ax(0) = 0
ay(0) = 0
r(i) = Rnd(10,15)
Next

dt = .1


'For a = 1 To 1000000

'Next


Timer1.Initialize("MainEvent", 10)
Timer1.Enabled = True



End Sub

Sub MainEvent

For i = 0 To 99

For j = 0 To 99
ax(j) = 0.0;
ay(j) = 0.0;
Next

For j = 0 To 99

If i <> j Then
diffx = xx(i) - xx(j)
diffy = yy(i) - yy(j)
dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
dist2 = r(i) + r(j)

If dist1 < dist2 Then
deltaDistance = (dist2 - dist1)

xval = (((deltaDistance * 10.0) / dist2) * diffx)
yval = (((deltaDistance * 10.0) / dist2) * diffy)

ax(i) = ax(i) + xval
ay(i) = ay(i) + yval

End If

End If
Next

vx(i) = (vx(i) + (ax(i) * dt)) *.999
vy(i) = (vy(i) + (ay(i) * dt)) *.999
xx(i) = xx(i) + (vx(i) * dt)
yy(i) = yy(i) + (vy(i) * dt)

If xx(i)-r(i) < 0 Then
vx(i) = -vx(i)
xx(i) = 0 + r(i)
End If
If yy(i)-r(i) < 0 Then
vy(i) = -vy(i)
yy(i) = 0 + r(i)
End If
If xx(i)+r(i) > w Then
vx(i) = -vx(i)
xx(i) = w - r(i)
End If
If yy(i)+r(i) > h Then
vy(i) = -vy(i)
yy(i) = h - r(i)
End If


Next

Canvas1.DrawColor(Colors.Black)

Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
For i = 1 To 99
Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
Next
Activity.Invalidate
DoEvents

End Sub


When i complile the code i get an error: "Signing package file (debug key). Error"
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
PHP:
Sub Process_Globals
   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   canvas1.Initialize(Activity)
   
   
   
   
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-20,20)   
      vy(i) = Rnd(-20,20)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
   
   'For a = 1 To 1000000
         
   'Next

  
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

  
  
End Sub

Sub MainEvent
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt)) *.999
         vy(i) = (vy(i) + (ay(i) * dt)) *.999
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub



I cant change that setting because i dont have a full version of basic4android
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
I have an unregistered version, so i cant change the Private Sign Key... could the be the issue? All i changed was putting my variable declarations into global... then it started giving the error.


B4X:
Sub Process_Globals
   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   canvas1.Initialize(Activity)
   
   
   
   
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-20,20)   
      vy(i) = Rnd(-20,20)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
   
   'For a = 1 To 1000000
         
   'Next

  
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

  
  
End Sub

Sub MainEvent
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt)) *.999
         vy(i) = (vy(i) + (ay(i) * dt)) *.999
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Does the following code compile on your machine? I added the Touch event handler....

B4X:
Sub Process_Globals
   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   canvas1.Initialize(Activity)
   
   
   
   
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-20,20)   
      vy(i) = Rnd(-20,20)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
   
   'For a = 1 To 1000000
         
   'Next

  
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

  
  
End Sub

Sub MainEvent
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt)) *.999
         vy(i) = (vy(i) + (ay(i) * dt)) *.999
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   xx(0) = x
   yy(0) = y
End Sub
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Signing package file (debug key). Error


Do you have the full version of the compiler?

Should both have the word Tick in them?

ie. "Timer1.Initialize("MainEvent_Tick", 10)" ?
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Compiling code. 0.03
Generating R file. 0.00
Compiling generated Java code. 0.91
Convert byte code - optimized dex. 0.52
Packaging files. 0.47
Signing package file (debug key). Error


Are you able to manipulate the large ball by touching the screen?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Signing package file (debug key). Error


Do you have the full version of the compiler?
I have the full version. But I think it should still work for you, afterall you were able to compile before?

Should both have the word Tick in them?
ie. "Timer1.Initialize("MainEvent_Tick", 10)" ?
No. Just the Sub name.
 
Upvote 0
Top