Jump to content

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


espace
 Share

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)

}

 

Link to comment
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.

Link to comment
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) 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...