Moving an image

Kamac

Active Member
Licensed User
Longtime User
Hi, so i have tried to make an image being moved using buttons, but it didn't work :(.

Here's how the code did look like:

B4X:
'Activity module
Sub Process_Globals
End Sub

Sub Globals
   Dim ImageView1 As ImageView
   Dim Down As Button
   Dim Left As Button
   Dim Right As Button
   Dim Up As Button
   Dim x1 As Int
   Dim x2 As Int
   Dim y1 As Int
   Dim y1 As Int
   x1 = 25%x
   x2 = 50%x
   y1 = 25%y
   y2 = 50%y
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main.bal") 'load the designer layout (just the gradient color in this case).
   ImageView1.Initialize("")
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "smiley.gif")
   Activity.AddView(ImageView1, x1, y1, x2, y2)
End Sub

Sub Activity_Pause
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Touch(Action As Int, tx As Float, ty As Float)

End Sub
Sub Up_Click
   'Activity.AddView(left,top,width,height)
   y1 = y1 - 5
End Sub
Sub Right_Click
   x1 = x1 + 5
End Sub
Sub Left_Click
   x1 = x1 - 5
End Sub
Sub Down_Click
   y1 = y1 + 5
End Sub

Sub Timer1_Tick
   Activity.RemoveViewAt(0)
   Activity.AddView(ImageView1, x1, y1, x2, y2)
End Sub

But it just didn't move.

Could anyone help me with that?
 

klaus

Expert
Licensed User
Longtime User
It would be easier for us if you posted your whole program as zip file.
In the IDE File/Export As Zip menu.

At a first look:
- You are using a Timer but it is not declared.
- In the timer you remove the ImageView and add a new one, in this one the image is lost.
- The Timer routine is nit necessary
- In the button_click routines you should:
B4X:
Sub Up_Click
  ImageView1.Top = ImageView1.Top - 5
End Sub
 
Sub Right_Click
  ImageView1.Left = ImageView1.Left + 5
End Sub
 
Sub Left_Click
  ImageView1.Left = ImageView1.Left - 5
End Sub
 
Sub Down_Click
  ImageView1.Top = ImageView1.Top + 5
End S
Best regards.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
B4X:
 Sub Globals
    Dim b As Button
    Dim L As Label
    Dim t As Timer
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
    L.Initialize("L")
    Activity.AddView(L, 1, 1, 200, 50)
    L.Text = "Hello world!"
 
    b.Initialize("b")
    Activity.AddView(b, 20, 100, 100, 50)
    b.Text = "Hold down"
 
    t.Initialize("t", 100)
    t.Enabled = False
End Sub
 
Sub b_Down
    t.Enabled = True
End Sub
 
Sub b_Up
    t.Enabled = False
End Sub
 
Sub t_Tick
    L.Left = L.Left + 1
    If L.Left = 200 Then L.Left = 1
End Sub
 
Upvote 0
Top