Jump to content

problem with a callback


espace
 Share

Recommended Posts

hi,

i don't understand why the function anim_obj don't run as callback of the appears function ....

could you tell me why ?

 

var reset = function (obj) {
	tw.r = game.add.tween(obj)
		.to({ alpha: 0 }, t.animation_appears, Phaser.Easing.Linear.None)
		.start();
}

//to appears the cloud
var appears = function (obj, callback) {
	obj.flag = true
	tw.s = game.add.tween(obj)
		.to({ alpha: 1 }, t.animation_appears, Phaser.Easing.Circular.Out)
		.start();
	tw.s.onComplete.add(callback})
};
var anim_obj = function (obj) {
	if (obj.flag) {
		tw.a = game.add.tween(obj)
			.to({ x: obj.x, y: obj.y - 1009 }, t.animation_cloud, Phaser.Easing.Linear.None)
			.to({ x: obj.x, y: obj.y }, t.animation_cloud, Phaser.Easing.Linear.None)
			.to({ x: obj.x, y: obj.y + 1009 }, t.animation_cloud, Phaser.Easing.Linear.None)
			.to({ x: obj.x, y: obj.y }, t.animation_cloud, Phaser.Easing.Linear.None)
			.start();
		tw.a.onComplete.add(anim_obj)
	}
};

//orage
var anim_light = function () {
	this.tween_alpha_ = game.add.tween(light).to({ alpha: 1 }, 50, Phaser.Easing.Linear.None, true, 8500);
	this.tween_alpha_.yoyo(true, 50)
}

// check if state is not current and annul + reset all the state if present
//anim state
var stop_state = function (obj) {
	if (obj.alpha == 1) {
		obj.flag = false
		reset(obj) //tween to alpha 0
	}
}

var stop_not_obj = function (obj) {
	if (obj.name != 'cloud') {
		stop_state(o.cloud)
	}
	if (obj.name != 'storm') {
		stop_state(o.storm)
	}
}

var animate = function (obj) {
	if (obj.alpha != 1) { // to avoid to anim 2x
		appears(obj, anim_obj)
		obj.flag = true
	}
}

var start_obj = function (obj) {
	stop_not_obj(obj)
	animate(obj)
}

start_obj(cloud)

 

Link to comment
Share on other sites

Hi @espace,

I think you probably have not passed the argument to the callback. Look at this code made in Phaser Sandbox (try the code here: http://phaser.io/sandbox/obVXEuZb/play ) :

function create() {

    var sprite = game.add.sprite(0, 0, 'phaser');
    
    var appears = function (obj,callback){
        
        var tw = game.add.tween(obj).to( { x: 100 }, 2000, "Linear", true);
        
        // Here is the trick: add(callback, context, priority, args)
        tw.onComplete.add(callback,this,null,obj);
        
    };
    
    var anim_obj = function(obj){

       game.add.tween(obj).to( { y: 100 }, 2000, "Linear", true); 

    };
    
    appears(sprite,anim_obj);

}

Regards.

Link to comment
Share on other sites

Hi again @espace,

The onComplete property of the Tween class is an object of the Signal class, which in this case calls its add method.
This is the documentation of "Signal.add" method:

https://photonstorm.github.io/phaser-ce/Phaser.Signal.html#add

I am using Phaser CE, but in this case it does not change with respect to your version.

2 hours ago, espace said:

Is it possible to avoid "this" ? I want to work with "pure code" without concept who are not evident ...?

Yes, is an optional argument. This code still works changing "this" with "null". 

Regards. 

Link to comment
Share on other sites

since lot of times i dislike the tween manager in Phaser, so with your view i decide to build my own library more simple at my eyes :

//utils.js
var wait = ()=> (callback, duration) {
	setTimeout(callback, duration);
}
//config.js
var p = {
        o: cloud, //object
        t: 1000, //time
        d: 5000, //delay
	a: 1, //alpha
        e: Phaser.Easing.Elastic.Out //Easing
        //r: 85, //rotation
        //sx :2, //scalex
        //sy :4, //scaley
        //dx :400, //displacementx
        //dy :200, //displacementy 
        //y: true //yoyo,
        //dyo :800, //delay yoyo
}
//utils.js
var _tr = (game, p) => { //transition,game,parameter
	this.game = game
	if (p.e === null) {
		p.e = Phaser.Easing.Linear.None
	}
	this.s = () => { // start tween
		if (p.a !== null) { // alpha
			this.tw = game.add.tween(p.o).to({ alpha: p.a }, p.t, p.e, true, p.d);
		}
		if (p.r !== null) { //rotation
			this.tw = game.add.tween(p.o).to({ angle: p.r }, p.t, p.e, true, p.d);
		}
		if (p.sx !== null) { //scale
			this.tw = game.add.tween(p.o.scale).to({ x: p.sx, y: p.sy }, p.t, p.e, true, p.d);
		}
		if (p.dx !== null) { //displacement
			this.tw = game.add.tween(p.o).to({ x: p.dx, y: p.dy }, p.t, p.e, true, p.d);
		}
	}
	this.c = (callback, time) => { //complete
		let time_adapted = p.d + p.t + time
		wait(callback, time_adapted)
	}

	this.y = () => { //yoyo
		if (p.y !== null) {
			this.tw(true, p.dyo)
		}
	}
        this.p=()=>{ //pause
        this.tw.pause()
        }
        this.r=()=>{ //resume
        this.tw.resume()
        }
	this.s() //start the tween
}
//main.js
var tw = new _tr(game,p)
tw.c(()=>console.log("hello"),1000)

just an object parameter like var p (more readeable) and two lines at the end and the job is ok for me without context,priority ...stuff whore are not necessary for all the people and not "this" who are ambiguous.

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...