Forum

How can I get action when animation ends?

28 April 2016 16:15
Hello everybody,

I have just started with Blend4web and have little problem with animation. I'm trying to make animations play in a row. But I just cant get it to work.

If I just put two different m_anim.play(obj) functions one after another they will play in same time. Even with m_anim.play(obj, finish_callback, slot) its seems to call that finish_callback() funktion right in start (not in the end like I assumed).

I have tried for and while loops with m_anim.isPlay() and m_anim.get_frame() functions, but same problem there. Loops keep going but frame number (and isPlay boolean) do not change while looping.

Any ideas how can I get this working?

Juha
28 April 2016 18:44
Hello and welcome to the forum.

If I just put two different m_anim.play(obj) functions one after another they will play in same time. Even with m_anim.play(obj, finish_callback, slot) its seems to call that finish_callback() funktion right in start (not in the end like I assumed).

It's a strange behavior. Could you show me your code? It should look like this:
var obj1 = m_scenes.get_object_by_name("Obj1");
var obj2 = m_scenes.get_object_by_name("Obj2");

m_anim.apply(obj1, "action1");
m_anim.apply(obj2, "action2");

m_anim.set_behavior(obj1, m_anim.AB_FINISH_STOP);
m_anim.set_behavior(obj2, m_anim.AB_FINISH_STOP);

m_anim.play(obj1, function() {m_anim.play(obj2)})
29 April 2016 16:16
Here is my code (little part of it): I have made my own "cube object" that have object in cube_obj.obj and name in cube_obj.name.

  function main_canvas_click(e) {
....
   if (obj_clicked_name == cube_obj.name) {
     m_anim.play(cube_obj.obj, end_function(), SLOT_HEILAHDUS);
    }
    if (obj_clicked_name==cube1_obj.name) {
      m_anim.play(cube1_obj.obj, end_function(), SLOT_HEILAHDUS);
    }
  }

  function object_anim(obj, anim_slot){
     m_anim.play(obj, lopetus_funktio(), anim_slot);
  }

  function end_function(){
    m_anim.play(cube_obj.obj, null, SLOT_PYORAHDYS);
  }


When I click "cube"-object it does only HEILAHDUS-anim and if I click "cube1"-object "cube1" do HEILAHDUS-anim and "cube" do PYORAHDYS-anim same time.

So its doing "end_function" but right away.

Juha
29 April 2016 16:20
Sorry about that "object_anim" function. It is an old test.
29 April 2016 18:06
I cannot see anything wrong.
I've prepared an example (it's a link, press it). It was created by the project manager exporter. Please, try to import it.
"use strict"

// register the application module
b4w.register("anim_test", function(exports, require) {

// import modules used by the app
var m_app       = require("app");
var m_data      = require("data");
var m_anim      = require("animation");
var m_cont      = require("container");
var m_scs       = require("scenes");


var FIRST_ANIM_SLOT = 0;
var SECOND_ANIM_SLOT = 1;

var _animated = false;

/**
 * export the method to initialize the app (called at the bottom of this file)
 */
exports.init = function() {
    m_app.init({
        canvas_container_id: "main_canvas_container",
        callback: init_cb,
        show_fps: true,
        console_verbose: true,
        autoresize: true
    });
}

/**
 * callback executed when the app is initialized 
 */
function init_cb(canvas_elem, success) {

    if (!success) {
        console.log("b4w init failure");
        return;
    }

    load();
}

/**
 * load the scene data
 */
function load() {
    m_data.load("anim_test.json", load_cb);
}

/**
 * callback executed when the scene is loaded
 */
function load_cb(data_id) {
    m_app.enable_camera_controls();

    // place your code here

    var suzanne = m_scs.get_object_by_name("Suzanne");
    var cube = m_scs.get_object_by_name("Cube");

    m_anim.apply(suzanne, "SuzanneAction", FIRST_ANIM_SLOT);
    m_anim.apply(cube, "CubeAction", SECOND_ANIM_SLOT);

    m_anim.set_behavior(suzanne, m_anim.AB_FINISH_STOP, FIRST_ANIM_SLOT);
    m_anim.set_behavior(cube, m_anim.AB_FINISH_STOP, SECOND_ANIM_SLOT);

    var canvas = m_cont.get_canvas();

    canvas.addEventListener("mousedown", function() {
        if (_animated)
            return;
        _animated = true;
        m_anim.play(cube, function() {
            m_anim.play(suzanne, function() {
                _animated = false;
                m_anim.set_frame(cube, 0, SECOND_ANIM_SLOT);
                m_anim.set_frame(suzanne, 0, FIRST_ANIM_SLOT);
            }, FIRST_ANIM_SLOT);
        }, SECOND_ANIM_SLOT);
    });

}


});

// import the app module and start the app by calling the init method
b4w.require("anim_test").init();
29 April 2016 20:01
Thank you Roman,

Your set works like it should. Now I need to sort out, what is wrong in mine. I did see few different thinks all ready.

I hope, that I get blend4web going and can help someone else in the future. Like you helped me.

Juha
 
Please register or log in to leave a reply.