Ecco

..array not visible outside onAssetsLoaded()

Recommended Posts

Hi guys..

Im trying to load multiple SPINE animations and each store in an array. 

Problem is that, i cant access loaded SPINE animations created as an array inside onAssetsLoaded function.  Arrays are defined outside of onAssetsLoaded function, which is why im confused. Here it is..

var ANIMATION_SPINE=[]; //declared outside of any function

..then i load animated character like this,

PIXI.loader.add('character', spineJSON_filename).load(onAssetsLoaded);

..and onAssetsLoaded function looks like this..

function onAssetsLoaded(loader,res)
 {
        ANIMATION_SPINE.push(new PIXI.spine.Spine(res.character.spineData));
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].skeleton.setSkinByName( 'skin1');
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].x = 600;
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].y = 600;
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].skeleton.setSlotsToSetupPose();
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].scale.set(1.0);
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].state.setAnimation(0, 'walk', true);
        GAME.stage.addChild(ANIMATION_SPINE[ANIMATION_SPINE.length-1]);   
 }

..after this, i see my character on the screen and its properly animated..however, i cant access ANIMATION_SPINE   (i want to change animations, or skins, etc, in runtime) array outside of onAssetsLoaded() even its declared outside of  any function..how to do this? How to store animations inside array from where i can access them globally ?? Thank you in advance guys.

 

Share this post


Link to post
Share on other sites

This code should work. It has to work, everything is fine. ANIMATION_SPINE is global, its just it'll have zero elements before the animation actually loads. Are you sure that you access it after everything loads?

Do you see any errors in the console?

Also, I predict that this might be your NEXT problem: 

 

 

Share this post


Link to post
Share on other sites

hi Ivan.

Well...how to say this..it works, and it doesn't work at all (everything stuck/freeze), which is a random thing. I thought its because im trying to access loaded entity handler, before is actually loaded. So, i have created new loader for every loaded spine animation and added 'once' so i will trace when its actually loaded

ANIMATION_LOADER.push(new PIXI.loaders.Loader());
ANIMATION_LOADER[ANIMATION_LOADER.length-1].add('character', spineJSON_filename).load(onAssetsLoaded).once('complete',this.complete,this);

..so, onAssetsLoaded looks like this

function onAssetsLoaded(loader,resources)
{
        ANIMATION_SPINE.push(new PIXI.spine.Spine(resources.character.spineData));
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].skeleton.setSlotsToSetupPose();
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].skeleton.setSkinByName(ANIMATION_DEFAULT_SKIN);
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].x = 100;//RND(100,window.innerWidth+100); 
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].y = RND(280,window.innerHeight-250); 
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].scale.set(1.0);
        ANIMATION_SPINE[ANIMATION_SPINE.length-1].state.setAnimation(0, 'walk', true);

        GAME.stage.addChild(ANIMATION_SPINE[ANIMATION_SPINE.length-1]);
}

and function complete looks like

function complete(loader,resources)
{
    ANIMATION_IS_LOADED.push(true);
}

..so, im using ANIMATION_IS_LOADED , which has same index as ANIMATION_SPINE to track is it loaded or not, before i can access animation handle. Function for setting position, im calling in a game loop, looks like this

function ANIMATION_SET_POSITION(characterID,x_,y_)
{
    if(ANIMATION_IS_LOADED[characterID])
    {
       ANIMATION_SPINE[characterID].x=x_;
       ANIMATION_SPINE[characterID].y=y_;  
    }
}

..so, this, sort of fixed my problem...however, i would like to know, what would be another way to know for sure that whatever is thrown at loader is actually loaded ? What would be other method of loading media, kind a 'manually' where program will not go on to next instruction until is previous one completed(media loaded) ??

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.