Joystick

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hello everyone,

I'd like to make my first topdown game but without the typical arrow direction pad. I'd like to use a joystick to move the main character around in different directions.

Does anyone has an example or example code on how to do this?
I prefer using a sprite (image) as the joystick pad.

So when touching the 'circle' at the left, the main character walks to the left.

I'm sure this will help a lot of other people.

here's an example screenshot:
Tank-Hero-in-Game-Play-6.jpg


XverhelstX
 

thedesolatesoul

Expert
Licensed User
Longtime User
The joystick has the addition advantage of being able to sense magnitude as well as direction.

This should be fairly easy to program. I do not have an example but I can give some ideas.

If you have a rectangle for the joystick, you will need to sense the Touch coordinates and find the distance and angle from origin (centre of the rectangle).

The sprite for the joypad will probably always be centred at Touch coordinates.

Have a look at how Gameloft's NOVA does it.
 

Attachments

  • 23966.jpg
    23966.jpg
    92.2 KB · Views: 427
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Hi,

i've attached a quick and dirty example how to make a joystick control in b4a.
 

Attachments

  • joystickdemo.zip
    24.5 KB · Views: 567
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Thanks Djembfola
I havent seen Kamac's modification yet
Here is some more modifications which detects both magnitude and angle (before it was just an 8 directional D-PAD, now it can move in any direction almost)
EDIT: btw, my code is really bad and hacky!
 

Attachments

  • test(Mag,Angle).zip
    24.5 KB · Views: 426
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Thanks a lot thedesolatesoul, kamac and Djembefola!
However, i'm searching more to move in all directions.
Now it is 8 directional movement.

Is it also possible to add a joystick as i tried it, but I couldn't figured it out. :S


Thanks!

XverhelstX

EDIT: Oh, I just saw thedesolatesoul's one that support all directional movement.
Now I only need the moving joystick :)

there's a small bug in the one from thedesolatesoul though. sometimes moving up and down switches :s

It's very helpfull for me and for others that will use it

Thanks a lot guys!
 
Last edited:
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
smooth, thedesolatesoul !!

works perfect, you just forgot to comment out two lines of code.
 
Last edited:
Upvote 0

maleche

Active Member
Licensed User
Longtime User
Tank Style Steering

Hello,

I am working on an application to control a robot with Tank Steering. I have programmed an interface and the vertical sliders (throttles) work individually but not at the same time to control both motors. The vertical sliders are ImageView inside two separate (vertical) Panels. The Slider Knob (ImageView) moves vertically and returns min and max values. The camera joystick is an ImageView (Ball) inside another Panel to control the camera x & Y Pan/Tilt and works well.

What is the best way to program the Vertical Sliders to operate at the same time?

See attached Graphic.

Thank you!
 

Attachments

  • Test1.jpg
    Test1.jpg
    20.6 KB · Views: 556
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
Two lines of code has caused the bug which switched the controls up/down randomly.
Aaand....I have made the joystick move now :)
It runs all smooth here.

Project attached ;)
 

Attachments

  • Joystick_Moving.zip
    12.5 KB · Views: 423
Last edited:
Upvote 0
Top