January 18, 2019, 12:25:17 AM

Author Topic: [ JavaScript ] ..why array is empty..  (Read 392 times)

Offline Naughty Alien

  • Hero Member
  • *****
  • Posts: 542
[ JavaScript ] ..why array is empty..
« on: April 17, 2018, 01:55:55 AM »
..hi guys...this is related to JavaScript..i just dont understand, why i cant access an array which i use to store animations, and it is globaly initialized..

so, here is pic 1 where arrays of interest are initialized

..this is function im calling to load animation (ANIMATION_CREATE). Inside that function is a loader and loader will call function LOAD_ANIMATION_MEDIA, once resources are ready, as you can see on pic 2

..here you can see LOAD_ANIMATION_MEDIA function which is called once all resources needed are ready. As you can see, animation is stored inside an array which is global as shown on PIC 1

..and finally, before game loop is entered, here is how loading is done. Problem is, after ANIMATION_CREATE is called, animation IS stored inside array (ANIMATION_SPINE), however, as you see on pic 4, i cant access it and anything i try to do directly with array element which supposed to have loaded animation is of no result, yet, my character is loaded and i can see it on the screen.

So, how come i cant do anything with array element outside of LOAD_ANIMATION_MEDIA function, while array is initialized globally(its not initialized inside function)..any suggestions??

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 1356
Re: ..why array is empty..
« Reply #1 on: April 17, 2018, 06:13:35 AM »
Silly question, but doesn't adding an array element in the method you are doing just continually overwrite the last element?
Until the next time...

Offline Matty

  • Hero Member
  • *****
  • Posts: 584
    • MattiesGames
Re: ..why array is empty..
« Reply #2 on: April 17, 2018, 02:47:31 PM »
I could be wrong but it looks like you have defined an array of size '0' and are simply as Qube says overwriting the 0th element (which doesn't actually exist).

With Javascript you need to resize the array and grow it.

Look here for example:

in the drawimage function - I add a new Object() to the array at the end of the array.