Jump to content

Adding Swipe controls to = Mouse movement


richtornado
 Share

Recommended Posts

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

Link to comment
Share on other sites

@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?

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...