• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

"Pseudo" swiping in createjs/Animate CC

Contributor ,
Oct 11, 2019 Oct 11, 2019

Copy link to clipboard

Copied

I've been working on a project wherein I wanted to be able to rotate a game object by swiping while running on a tablet/mobile device.  So far as I know, there isn't yet a dedicated method to do swiping in Createjs Touch mode, but I've cooked up something of a hack to work around it...

var active = false;
var firstX, runningX, dx;
var star = this.star;

createjs.Touch.enable(stage);
stage.preventSelection = false;
this.stage.addEventListener("stagemousedown", initActive); 
this.stage.addEventListener("stagemousemove", swiping);
this.stage.addEventListener("stagemouseup", endActive); 

function initActive(){
	this.active = true;
	this.firstX = stage.mouseX;
	
}

function swiping(){
	if(this.active){
		this.runningX = stage.mouseX;
		this.dx = this.firstX - this.runningX;
		star.rotation = this.dx;
	}	
}

function endActive(){
	this.active = false;
}

 

Here's the code in action... http://www.e-nimation.com/HTML5/swipeCode/Swiping.html

I had originally included a tick function...

createjs.Ticker.addEventListener("tick", rotating);
function rotating(){
	console.log("star rotation = "+star.rotation);
	if(this.active) star.rotation = this.dx;
}

but it doesn't seem to be necessary.

 

I hope someone finds this useful.  Feel free to amend, change, comment, throw out the window, etc.

 

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

LATEST

REVISION:

I noticed that, with the above code, the rotating object (star in this case) jumps back to a rotation of zero each time the stagemousedown function is called.  Here's the corrected code...

var active = false;
var firstX, runningX, dx, startRotation;
var star = this.star;

createjs.Touch.enable(stage);
stage.preventSelection = false;
this.stage.addEventListener("stagemousedown", initActive); 
this.stage.addEventListener("stagemousemove", swiping);
this.stage.addEventListener("stagemouseup", endActive); 

function initActive(){
	this.active = true;
	this.firstX = stage.mouseX;
        this.startRotation = star.rotation;	
}

function swiping(){
	if(this.active){
		this.runningX = stage.mouseX;
		this.dx = this.firstX - this.runningX;
		star.rotation = this.dx + this.startRotation;
	}	
}

function endActive(){
	this.active = false;
}

This takes into account the rotating object's new rotation rather than resetting it to zero.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines