I'm messing around making a simple game.
I have a helicopter which the user flies up and down.
I've got the basic code for this to happen working fine (touchstart + hold the helicopter goes up, release it goes down). However, it is very 'reactive'; if you want the helicopter to go in a straight line you need to quickly tap the screen, however the helicopter moves quite alot no matter how fast you can tap.
I need a way for the movement to ease at the start of going up and the start of coming down, so the helicopter can go in a fairly straight line if needs be.
My code is as follows:
var speed = 1;
function copter_touchstart(){
clearInterval(fall);
fly = setInterval( start_fly, 2);
}
function copter_touchend(){
clearInterval(fly);
fall = setInterval( start_fall, 2);
}
function start_fly(){
var matrix = $helicopter.css('-webkit-transform').replace(/[^0-9\-.,]/g, '').split(',');
var y = matrix[5];
var new_top = parseInt(y) - speed;
$helicopter.css('-webkit-transform', 'translate3d(0, '+ new_top +'px, 0)')
}
function start_fall(){
var matrix = $helicopter.css('-webkit-transform').replace(/[^0-9\-.,]/g, '').split(',');
var y = matrix[5];
var new_top = parseInt(y) + speed;
$helicopter.css('-webkit-transform', 'translate3d(0, '+ new_top +'px, 0)')
}
$body.on('touchstart', copter_touchstart).on('touchend', copter_touchend);
JS FIDDLE: HERE
(touch changed for mouse events) tap screen repeatedly to 'hover' the block, its too reactive, but I dont want to reduce the speed when flying normally