Remove a Sprite from an array

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
So I made this array and added sprites on it.


var lineX = 700;
this._buttonList = [];
for (var i = 0; i < 30; i++) {
var button = new Sprite();
button.bitmap = ImageManager.loadPicture('Item');
button.x = 100;
var line = lineX * i;
button.x = button.x + line;
button.y = 300;
this._buttonList.push(button);
this.addChild(this._buttonList);
}


However, I have a function that wants to remove a certain index value from that array. So, I decided to use the splice function. The splicing works, but the array length stays the same. I figured it is because I am not deleting the certain item. So, how can I delete a sprite from the array? I am using this splice by the way:


function move(arr, val) {
var j = 0;
for (var i = 0, l = arr.length; i < l; i++) {
if (arr !== val) {
arr[j++] = arr;
}
}
arr.length = j;
}


I want to delete index 0 for example, but even if I use move(this._buttonList, 0) it won't delete the first sprite on the array. So how can I remove that certain sprite?
 

Quxios

Veteran
Veteran
Joined
Jan 8, 2014
Messages
1,055
Reaction score
785
First Language
English
Primarily Uses
RMMV
Are you using the same reference from the button? and why don't use just use the normal splice function or was that just for show?


You should be able to do something like:

Code:
function removeButton(index) {
  var button = someArray.splice(index, 1)[0];
  Stage.removeChild(button);
}
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I see. I wondered why that did not work. I will try this solution now.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,867
Messages
1,017,062
Members
137,575
Latest member
akekaphol101
Top