-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only have 4 directions #6
Comments
Hi ChopsKingsland! Instead: if ( joystick.up ) text += 'up ';
if ( joystick.down ) text += 'down ';
if ( joystick.left ) text += 'left ';
if ( joystick.right ) text += 'right'; You can do: if (joystick.up)
text = 'up'
else if (joystick.down)
text = 'down'
else if (joystick.left)
text = 'left'
else if (joystick.right)
text = 'right' Let me know if this this helps you :D |
That works, apart from the way that I control the car sends a request to a URL, and I don't what to sending requests hundreds of times per second. Is there a way to only do this when the position of the joystick changes? |
A simplistic approach is to create a variable to store the last direction, something like: function do_my_request(dir) {
console.log("REQUEST", dir)
}
var direction = null
window.addEventListener("load", () => {
let joystick = new JoyStick({
radius: 80,
x: window.innerWidth / 2,
y: window.innerHeight /2,
inner_radius: 70
})
function run_all_time() {
if (joystick.up) {
if (direction != 'up')
do_my_request('up')
direction = 'up'
}
else if (joystick.down) {
if (direction != 'down')
do_my_request('down')
direction = 'down'
}
else if (joystick.left) {
if (direction != 'left')
do_my_request('left')
direction = 'left'
}
else if (joystick.right) {
if (direction != 'right')
do_my_request('right')
direction = 'right'
}
requestAnimationFrame(run_all_time)
}
run_all_time()
}) Maybe this works for you |
The last example have some glitches when passing through the angles. You can give an look in the first example of https://yoannmoi.net/nipplejs/#demo too. They have an attribute called "angle", that gives you what you want. |
I would like to use this to control an RPi car, but I only need 4 directions (up, down, left & right). How do I make these mutually exclusive (eg It can't be
up left
)?The text was updated successfully, but these errors were encountered: