Gameview - Touch again

basil99

Active Member
Licensed User
Longtime User
Gameview - Another Touch problem - SOLVED

I have wide "background" picture ( 960x640 ) and i init it this way:


Dim GG As Bitmap
Dim Xoffset As Int
Dim Yoffset As Int

GG.Bitmap = LoadBitmap(File.DirAssets, "bkg.jpg")
GG.SrcRect.Initialize( Xoffset, Yoffset, 480 + Xoffset, 320 + Yoffset )
GG.DestRect.Initialize( 0, 0, 100%x, 100%y )

Device screen is 480x320

then i handle touch event like this

Sub gv_Touch (Action As Int, X As Float, Y As Float)

If Action = Activity.ACTION_DOWN Then

If X < 33%x Then

Xoffset = Xoffset - 1
If Xoffset < 0 Then Xoffset = 0

End If

If X > 66%x Then

Xoffset = Xoffset + 1

End If

GG.SrcRect.Initialize( Xoffset, Yoffset, 480 + Xoffset, 320 + Yoffset )
gv.Invalidate

End if
End Sub

At first glance it works fine and as expected - when i touch right side of the screen , picture shifts left, when i touch left side - picture shifts right.

BUT STOP! Seems. I need to touch screen every time when i need picture to shift.

I want to touch screen and keep touching and still get this event.
So, when i press, say, right side of the screen, picture will shift until i stop touch.

How to do it with GameView and Touch event ?
Or may be I need to use smth other then Touch to handle single touches and continuos pressing ?

Thank you
 
Last edited:

basil99

Active Member
Licensed User
Longtime User
The DOWN action is only fired once. You should enable a timer and scroll the picture. Disable the timer when the UP action fires.


Sorry, i don't get this. As I got, when i touch screen, DOWN action is fired as a single event, correct ? And never mind how long i press the touchscreen ?
If so, may be i need to use Gestures lib and remove Gameview.Touch from code ? Please, explain this

Note that you should always use percentage or 'dip' when specifying sizes.

Yes, I know, just a sample values
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
It's a one time event that triggers an event once.

Like Erel said you could enable a timer in that event to start something looped to update things.

Disable the timer again during the up event.

To be honnest, I don't see a need to use gameview just to reposition a background.
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
It's a one time event that triggers an event once.

Like Erel said you could enable a timer in that event to start something looped to update things.

Disable the timer again during the up event.

Thanks, sorex, but i still can't get it without an example (
If DOWN event is one time event, how timer can help to handle long touch, that doesn't fire UP event until release ?

To be honnest, I don't see a need to use gameview just to reposition a background.

need for parallax. If player move is, say, 16 untis. front layer of the world scrolls 16 units also, background1 - 1 units and background2 - 0.01 unit ( just an example ). This brings a sort of 3-rd dimension to 2D games, the illusion of "deep"
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, got it.

some views have the touch event that returns the x/y touch positions while moving aswell.

I think this is the case with panels, try something like this (put the background picture in a panel first)

Sub pnlBackground_Touch(Action As Int,x As Float, y As Float)
if Action=2 'I think 2 was touch&move
background.left=offset-x
end if
End Sub


if you can get that to work you could go for smooth scrolling
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
ok, got it.

some views have the touch event that returns the x/y touch positions while moving aswell.

I think this is the case with panels, try something like this (put the background picture in a panel first)

Sub pnlBackground_Touch(Action As Int,x As Float, y As Float)
if Action=2 'I think 2 was touch&move
background.left=offset-x
end if
End Sub


if you can get that to work you could go for smooth scrolling

Before you reply, Sorex, I try the timer idea. I use "direction" variable and use it this way in Touch Sub ( pseudocode )

if event is UP then direction = 0
if event is LEFT_SIDE_TOUCH then direction = 1
if event is RIGHT_SIDE_TOUCH then direction = 2

Then add background scrolling code to the Timer_Tick:

select direction
case 1

Xoffset = Xoffset -1

case 2

Xoffset = Xoffset + 1

endif

Briefly. it works, but i got the sort of scrolling inertia - background continue to scroll when i stop to press screen.

Anyway, will try the panel idea, cause later i need to handle more complex events.

Thank you
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
The timer solution is better. Your solution requires the user to move his finger and the result will not be consistent.

I'm still in doubt.
1. Inertia. Looks like, timer_tick processed several shifts after i stop to touch screen.
2. Multitouch. Like User press screen by both fingers from left and right simultaneously - i want to handle this event but GameView Touch seems can't handle it

Another words, what i want:

A) If user press or touch screen from the right it will move game character right ( X > 66%x )

B) If user press or touch screen from the left it will move game character left ( X < 33%x )

C) If User press screen from both left and right - game character will jump/fire

Single press on left/right - means move one step. Continuos touch - walk/run.
Mutitiuch - action
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
with "press or touch screen from the right" do you mean that you have an area at the right that acts as a hotspot or do you mean sliding from right to left ?

last case can be done with the gesture or with the touch event checking at both down and up events.

the reason why you scrolling kept going is because you forgot to add mytimer.enable=false in this line

if event is UP then
direction = 0
mytimer.enable=false
end if

unless you did it where you "case" through your directions somewhere else in the code.
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
with "press or touch screen from the right" do you mean that you have an area at the right that acts as a hotspot or do you mean sliding from right to left ?

Yes. Exactly the hotspot. Imagine phone in a landscape mode. Right thumb for move right, left thumb to move left, both pressed - jump/fire/action etc.
Hotspot area is at the screen bottom.

Another hotspots may be at the very top of the screen - to call game menu or inventory

last case can be done with the gesture or with the touch event checking at both down and up events.

I think you're right, start to learn Erel's Gameview GamepAd example for that

the reason why you scrolling kept going is because you forgot to add mytimer.enable=false in this line

if event is UP then
direction = 0
mytimer.enable=false
end if

unless you did it where you "case" through your directions somewhere else in the code.

My timer sub is planned to handle "items" and "enemises" also, if I disable timer all will freese, or may be I got you wrong ?
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
well, you can have a main timer for your sprites.

and another one for the scrolling part.
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Finally, I got it to work in a way i want.

Thanks everybody for help !

Basil99,

Could you share your scrolling code? I have my app scrolling, but it seems to stutter a bit. I'd like to see how you implemented your version.

-Sterling
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
By some reasons I moved into another non-game project, so my code is still in "carcass" mode, but what i had:

1. 2 semaphore variables: MovingLeft and MovingRight

2. Touch events Sub ( Gestures based ) sets them to 1 if a dedicated hotspot got user input

3. Timer Sub follows:

B4X:
Sub GameTimer_Tick


         If MoveLeft = 1 Then

                   MoveLeft = 0
                   Xoffset = Xoffset - ScrollStep
                   If Xoffset < 0 Then Xoffset = 0
      
         End If

         If MoveRight = 1 Then

                    MoveRight = 0
                    Xoffset = Xoffset + ScrollStep

         End If

         'scroll big background picture
         Bkg.SrcRect.Initialize( Xoffset, Yoffset, ScreenWidth + Xoffset, ScreenHeight + Yoffset )

         'refresh gameview
         gv.Invalidate 
   
End Sub

You may reset semaphore vars in timer or in touch sub, think the same result but i need to check, as I said this project is postponed

Hit me if you get more questions
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
by some reason i got syntax error on my phone ever if i remove hardware acc code from your sample
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
by some reason i got syntax error on my phone ever if i remove hardware acc code from your sample

What phone and version of android are you using?
 
Upvote 0

basil99

Active Member
Licensed User
Longtime User
My phone here is pretty old (2.2) so animation is not smooth at all. But all Gameview samples have this effect on my phone, so hard to decide. Hope to get new phone later today or tomorrow

You mean static car on the left side ? looks like it slide up down a bit ?
 
Upvote 0
Top