espace

Is it possible to use tween with an object parameter to be more readable ?

Recommended Posts

hi, i would use tween with an object parameter...how do you do ?

function create() {

    var sprite = game.add.sprite(0, 0, 'phaser');


var config={
    properties : {
        sprite,
        x:100,
    }
    duration : 1000,
    ease : Phaser.Linear.none,
    autoStart : true,
    delay:10,
    repeat : 0,
    yoyo:false
    
}
game.add.tween(config)

}

 

Share this post


Link to post
Share on other sites

Hi @espace,

I think that directly is not possible. But you can make your own wrapper function for "game.add.tween". Here a simple wrapper (  http://phaser.io/sandbox/BFoPWrWT ) : 

// Function to wrap game.add.tween

function tweenWrapper(obj){
    game.add.tween(obj.target).to(obj.properties,obj.duration,obj.ease,obj.autoStart,obj.delay,obj.repeat,obj.yoyo);

}


function create() {

    var sprite = game.add.sprite(0, 0, 'phaser');
    
    var config={
        
     target: sprite,
     properties : {
        y:150,
        x:100,
     },
     duration : 1000,
     ease : Phaser.Linear,
     autoStart : true,
     delay:10,
     repeat : 0,
     yoyo:false
    
    };
    
    tweenWrapper(config);

}

Regards.

Share this post


Link to post
Share on other sites

thanks, in fact i yet use a similar snippet 😁

var tw=[];
var _tw //pseudo prototype

var parameter ={
  e: Phaser.Easing.Bounce.Out,
  i: 0, //number repeat
  y: true, //yoyo
  a: 0, // alpha
  t: 500, //time
  d:100, // delay
  r: 45, //rotation
  dx:100, //distance
  dy:200,
  sx:1, //scale
  sy:1,
}

_tw = (p,n) => { //parameter,number 

	if (p.e == null) {
		p.e = Phaser.Easing.Linear.None
	}	
	if (p.i == null) {
		p.i = 0
	}
	if (p.y == null) {
		p.y = false
	}
	if (p.a != null) { // alpha
		tw[n]=game.add.tween(p.o).to({ alpha: p.a }, p.t, p.e, true, p.d, p.i, p.y);
	}
	if (p.r != null) { //rotation
		tw[n] = game.add.tween(p.o).to({ angle: p.r }, p.t, p.e, true, p.d, p.i, p.y);
	}
	if (p.sx != null) { //scale
		tw[n]= game.add.tween(p.o.scale).to({ x: p.sx, y: p.sy }, p.t, p.e, true, p.d, p.i, p.y);
	}
	if (p.dx != null) { //displacement
		tw[n] = game.add.tween(p.o).to({ x: p.dx, y: p.dy }, p.t, p.e, true, p.d, p.i, p.y);
	}

	tw[n].c = () => {
		if (p.c != null) {
			let time_adapted = p.d + p.t + p.ctime
			wait(p.callback, time_adapted)
		}
	}

	tw[n].p = () => { //pause
		tw[n].pause()
	}
	tw[n].r = () => { //resume
		tw[n].resume()
	}
}
_tw(parameter,0) 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.