Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by peardox

This doesn't work on iOS and requires JQuery

I use this code on Blackberry Playbook and Nexus 7

The Nexus 7 needs the right and left control values reversed

The following is a big hack that overrides the built-in control system but makes for very intuitive play.

On the tested devices you can tilt the tablet left / right to move in that direction, touch anywhere on the screen to fire and flip the device away from you a little to jump.

It's extensible - you could easily use the Z-Axis as well or reverse the jump code for a 'down' command for example

A modification I'll be making shortly will allow different parts of the screen to do different things based on where the touch event takes place

The touchmove binding prevents scrolling of a webview if anyone's wondering

var jumped = 0;
var dbg = true;
var orient = 1;
var shoot = false;

function handleDeviceOrientation(event) 
	{
	if (window.orientation === undefined) {
		return false;
	}
	
	switch(window.orientation) 
		{
		case 0:
			orient = 1;
			break;
		case 180:
			orient = -1;
			break;
		}
	}
	
function handleDeviceMotion(event) 
	{
	ax = event.accelerationIncludingGravity.x * orient;
	ay = event.accelerationIncludingGravity.y * orient;

	ig.input.clearPressed();
	
	if(ax > 4)
		{
		// Right
		ig.input.actions.right = true;
		}
	else
		{
		ig.input.delayedKeyup.right = false;
		}
		
	if(ax < -4)
		{
		// Left
		ig.input.actions.left = true;
		}
	else
		{
		ig.input.delayedKeyup.left = false;
		}
	
	jumper = ay - jumped;
	
	if(jumper > 2)
		{
		// Jump
		ig.input.presses.jump = true;
		}
		
	if(shoot)
		{
		// Jump
		ig.input.presses.shoot = true;
		}
		
	jumped = ay;
	shoot = false;
	}

function doShoot()
	{
	shoot = true;
	}
	
$(document).bind('touchmove', function(e) {
    e.preventDefault();
    window.scroll(0,0);
    return false;
});

$(document).ready(function() {
	handleDeviceOrientation();
	document.addEventListener("touchstart",  doShoot,  false);
	window.addEventListener("orientationchange", handleDeviceOrientation, true);
	if (window.DeviceMotionEvent || window.ondevicemotion) {
		//Check to see if the current application supports the devicemotion
		window.addEventListener("devicemotion", handleDeviceMotion, false);
		jumped = event.accelerationIncludingGravity.y;
	} 
		
	});

1 decade ago by Jerczu

Jquery on a mobile device is an overkill why not use jqmobi?
Page 1 of 1
« first « previous next › last »