richtornado Posted March 27, 2017 Share Posted March 27, 2017 Hello, I'm very new to Javascript (and coding). I just finished my first game, a clone of Pong. I have it controlled via the mouse movement, now I'd like to add code that will let the user swipe up or down on a touchscreen and have that relate the to controls for the mouse. Any Help would be appreciated. Thanks in advance. Here is what I have for my mouse controls. window.onload = function() { canvas = document.getElementById('gameCanvas'); canvasContext = canvas.getContext('2d'); var framesPerSecond = 30; setInterval(function() { moveEverything (); drawEverything(); }, 1000/framesPerSecond); canvas.addEventListener('mousedown', handleMouseClick); canvas.addEventListener('mousemove', function(evt) { var mousePos = calculateMousePos(evt); paddle1Y = mousePos.y - (PADDLE_HEIGHT/2); }); } code help.tiff Quote Link to comment Share on other sites More sharing options...
mattstyles Posted March 27, 2017 Share Posted March 27, 2017 Check out touchdown, touchup and touchmove events, they'll fire when you want although the event structure is slightly different to using mouse events. They're both pointers so there is a newer pointer spec but I don't think its really implemented everywhere yet. Quote Link to comment Share on other sites More sharing options...
alex_h Posted March 27, 2017 Share Posted March 27, 2017 Some useful reference material for touch events here: https://developer.mozilla.org/en/docs/Web/API/Touch_events Quote Link to comment Share on other sites More sharing options...
b10b Posted March 27, 2017 Share Posted March 27, 2017 @richtornado If you are very new to Javascript and coding you may be unfamiliar (or uncomfortable) with the practical need to borrow other people's code (libraries). Rarely is any single scenario sufficient to justify this in it's own right, but collectively the scenarios mount up to the point of common sense. As a newcomer it may be wise to embrace this from the outset - e.g. adopt a gaming framework where lower-level systems (like loop and inputs) are already included and higher-level systems are available from community plugins. For example "adding swipe controls to mouse movement" needs to consider consolidating mouse and touch events, potentially compensating for event target element transformation, extrapolating the delta of a movement over a period of time, recognising such deltas as gestures (swipe being the easiest), testing on multiple devices, optimising for performance, maintaining into the future. I wouldn't wish to put you off doing this though - it's all fun and valuable longer term, just maybe complex and off putting shorter term? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.