How do I - Arrays with For Loops Help

0 favourites
  • 8 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • I read Ashley's comment in this post that helped w/ 1 issue () but having a similar / separate issue.

    I build an array that corresponds to each level... the level shows a light and plays a sound. I've tried several ways to make this work, but I'm missing something (I think prob very similar to the issue mentioned in the post listed above).

    Here is my example of what happens (and pic of my most recent logic).

    Level 1 adds the color Red. Level 2 adds Purple. Level 3 adds White. So, my array is pretty simple:

    0 Red

    1 Purple

    2 White

    But, when I "play" the array (the lights/sound), I'm only getting the last entry. What happens is that my "Play" function will play Red for level 1. For level 2 it will play Purple, Purple. For level 3 it will play White, White, White.

    What I want to happen is: Level 1 will play Red. Level 2 will play Red, Purple. Level 3 will play Red, Purple, White... and so on.

    I've tried the various .CurValue, .CurX, loopindex, etc. It feels like the same issue that Ashley talked about... just not sure how to get past it. Any help is greatly appreciated.

    Thanks.

    Stephen

  • It looks like your trying to access Array_Color from inside a function that is in the foreach loop. I dont beleive the instance reference for Array_color persists through the function call. try it in the original foreach usage and see if that works

  • Thx for reply. I might be missing where to put it... I've added more of my "logic" (which might be the wrong way to do it). My thinking is that when I hit the Play Button (for my testing purposes), it will "build array" meaning add 1 to the CurrentLevel, add the color to the array stack... then, I want all of the lights/sounds to play (i.e. if there are 10 items, it will loop through all 10).

  • Im not sure what choose() function is returning .. but i think i see it... the MySounds for loop does not loop through the array keys its just a standard for loop. you would need to do a Array_Color "foreach element" on Axes X for it to return the CurVal of what was stored in X

  • I've tried the Array_Color "foreach element" as well... but, it only plays the last sound... I'm thinking it might be so fast and just ends on that last color/sound. The Choose() function just builds the array w/ a random color... which does work (screen pic of my Array).

    [attachment=1:3949f8p0][/attachment:3949f8p0]

    Here is my logic using the Array "for each element"

    [attachment=0:3949f8p0][/attachment:3949f8p0]

    Thanks for your help. Seriously. I must be missing something simple here but just can't figure it out.

  • Try to use wait system-function at the end of "for each x".

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Tried that. But, here is the update. Press btnPlay first time adds a color (i.e. Red), appends the CurValue to the Text, and plays the Red light/sound. Press btnPlay second time adds color (i.e. Green) to the array, appends Red Red to the already Red to the Text, and plays the Red light/sound once.

    I'm feeling pretty stupid here.

  • The system loops (for/repeat) work with loopindex. The array loop (for each element) works with Array.CurX, Array.CurValue etc. You can't mix the two and use Array.CurValue with a for loop or use loopindex with an array loop.

    When you use a value after x seconds, you need to think what will the value be at the time the action is run.

    If you have

    set value to 5
    wait 10 seconds
    set text to value[/code:20elajpr]
    then the text will be set to whatever [i]value[/i] is after the 10 seconds - it's not necessarily 5 any more.
    If you're using loopindex or Array.CurValue then it certainly won't be the value you want after x seconds.
    
    So maybe a different approach like:[code:20elajpr]
    every x seconds [or on timer]
    if array is not empty
        get and remove first value from array[/code:20elajpr]
    
    or if you want to keep the array intact:[code:20elajpr]
    global number index = 0
    every x seconds [or on timer]
        color = array.at(index)
        add 1 to index[/code:20elajpr]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)