I couldn't find anything that was good enough so I made one that works very well...
UPDATE, VER2: Allows two fingers and works better than this version. VIEW UPDATE HERE!
At the moment only allows one finger, if you want to extend this class feel free to do so!
package beekyr.controllers
{
import flash.geom.Point;
import starling.display.Quad;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
public class TouchPad extends Quad
{
/**
* ...
* @author Jaime Dominguez for Beekyr : 2013
* http://www.jaimedominguez.com
*/
private var _moveVector:Point = new Point();
private var _latestVector:Object = new Object();
private var _sensitivity:Number;
private var _touching:Boolean;
public function TouchPad(width:int, height:int, sensitivity:Number = 1) {
super(width,height,0xff0000);
_moveVector = new Point();
_sensitivity = sensitivity;
_touching = false;
alpha = 0;
addEventListener(TouchEvent.TOUCH , _handleTouch);
}
public function getLatestMovement():Object {
_latestVector._vX = _moveVector.x * _sensitivity;
_latestVector._vY = _moveVector.y * _sensitivity;
_latestVector.touching = _touching;
_moveVector.x = 0 ;
_moveVector.y = 0 ;
return _latestVector;
}
public function setWidth(w:Number):void {
width = w;
}
public function setHeight(h:Number):void {
height = h;
}
public function updateSensitivity(s:Number):void {
_sensitivity = s;
}
private function _handleTouch(e:TouchEvent):void {
e.stopImmediatePropagation()
var touchArray:Vector.= e.getTouches(this);
if (touchArray.length>0){
var touch:Touch = touchArray[0];
switch (touch.phase)
{
case TouchPhase.BEGAN:
storeThisPos(touch);
break;
case TouchPhase.ENDED:
stopMovement();
break;
case TouchPhase.MOVED:
getMovementVector(touch);
break;
}
};
}
private function stopMovement():void
{
_moveVector = new Point();
_moveVector.x = 0;
_moveVector.y = 0;
_touching = false;
}
private function stopMovementVector():void {
_moveVector = new Point(0, 0);
}
private function getMovementVector(touch:Touch):void
{
_moveVector = touch.getMovement(this);
}
private function storeThisPos(touch:Touch):void
{
_touching = true;
}
}
}
With this class initialized at start of the game. Then you need to collect the values in each loop of the game with:
_speed = _game._touchPad.getLatestMovement();
IT will return a vector of the lastest movement.
You can adjust the sensitivity too with:
public function updateSensitivity(s:Number):void {
_sensitivity = s;
}
No comments:
Post a Comment