Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Stop push from overwriting values in an array

New Here ,
Jan 30, 2018 Jan 30, 2018

Hi,

I'm trying to create an array containing scores and usernames which I can then sort and display in a table, but I'm stuck on one issue. When I try to push a second score to the highscores array, it saves again at position 0 rather than adding new data to position 1 and so on. In the following code, myScore contains the score that the user just achieves and myName contains their set username.

var highscores:Array = new Array();highscores.push({score: myScore, player: myName});highscores.sortOn("score", Array.DESCENDING | Array.NUMERIC);for (var i:int = 0; i < highscores.length; i++){  trace(highscores[i].score, highscores[i].player);}

How can I get this to add to the array rather than overwriting current data?

Thanks.

TOPICS
ActionScript
7.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 30, 2018 Jan 30, 2018

Hi!

Do you always call the new Array() line when you want to update the array? Because if you do this, the push method will always find an empty array.

Translate
Community Expert ,
Jan 30, 2018 Jan 30, 2018

Hi!

Do you always call the new Array() line when you want to update the array? Because if you do this, the push method will always find an empty array.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2018 Jan 30, 2018

This was the issue, not sure how I missed that.

Many thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2018 Jan 30, 2018
LATEST

It happens all the time! No worries.

And you're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines