小白求教如何让重力感应只能上下左右四个方向走?

想写一个依靠重力感应过关的RPG游戏,但是游戏人物有移动方向,不同移动的方向的动画也不同。求教如何实现。
GSensor:function(){
cc.inputManager.setAccelerometerEnabled(true);
var self = this
var listener1 = cc.EventListener.create({
event: cc.EventListener.ACCELERATION,
callback: function (accelEvent, event) {
this.speed.x = accelEvent.x
this.speed.y = accelEvent.y

          return true
        }.bind(self),
    });
    cc.eventManager.addListener(listener1, this.node);
},,这么写只会四面八方移动。应该也可以根据移动的方向算出旋转的方向。那请问下如何算呢?

感觉callback函数改成这样:

callback: function (accelEvent, event) {
	if (Math.abs(this.speed.x) > Math.abs(this.speed.y)) {
		this.speed.x = accelEvent.x
		this.speed.y = 0
	} 
	else {
		this.speed.x = 0
		this.speed.y = accelEvent.y
	}
}

应该可以吧

不行,这样只能走上下方向,我之前也试过,始终只能一个方向。