Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101

Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101

Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785
Faking a Swipe in Phaser - Phaser Games

Faking a Swipe in Phaser

One of the advantages native programs like Java for Android or Swift for IOS has over web languages like HTML5 is having built-in detection for detecting swiping. But with a little bit of coding, we can detect the actions of a swipe. For that, we need to break down the action of a swipe

  1. The user touches the phone (mouseDown)
  2. The user moves a finger across the phone
  3. The user lifts their finger(mouseUp)

Example

Code

var StateMain = {
    preload: function() {},
    create: function() {
    	//
    	//
    	//
        game.input.onUp.add(this.mouseUp, this);
        game.input.onDown.add(this.mouseDown, this);
        //
        //
        this.text1 = game.add.text(game.world.centerX, game.world.centerY, "swipe left or right!");
        this.text1.fill = "#ffffff";
        this.text1.anchor.set(0.5, 0.5);
    },
    mouseDown: function() {
    	//set the mouseIsDown to true
        this.mouseIsDown = true;
        //
        //
        //record the place the mouse started
        //
        //
        this.startX = game.input.x;
    },
    mouseUp: function() {
        this.mouseIsDown = false;
    },
    swipeDone: function() {
        //get the ending point
        var endX = game.input.x;
        //
        //
        //check the start point against the end point
        //
        //
        if (endX < this.startX) {
            //swipe left
            this.text1.text = "Swiped left";
        } else {
            //swipe right
            this.text1.text = "Swiped right";
        }
    },
    update: function() {
        if (this.mouseIsDown == true) {
        	//get the distance between the start and end point
            var distX = Math.abs(game.input.x - this.startX);
            //if the distance is greater than 50 pixels then a swipe has happened
            if (distX > 50) {
                this.swipeDone();
            }
        }
    }
}

 
Source Code Available in the FILE VAULT!

2 thoughts on “Faking a Swipe in Phaser”

Leave a Comment