|
-
DOT-INVADER
array question that sounds newbie
ok, here's an example of what i want.
> imagine i have that array in a load event:
Code:
selection=[1, 2, 3, 4]
> now, i want to remove the "2" in it; how can i do that?
> result array after the change:
Code:
selection=[1, 3, 4]
that array will depend on some actions the player do. so how can i use a code to have that result array from the first one i posted? can i use the delete method or something? as far as i know, i can only add/remove values that are located at the first/last place of an array...?
thanks.
-
Spelunker
sorry, but this doesnt answer your question:
Avec quelle programme a-tu fait ton raton-laveur? J'aimerais savoir car je suis entraint de faire un jeux qui a des images fait en pixel(dessine en Jasc). Jasc est bon, mais lequel a tu utilise?
-
hmmmm - I dunno but would you make a new array using something like
newArray = newArray()
for (i=0; i<selection.length; i++) {
if (i != 2) {
// code to make the new array
}
}
perhapsa
-
DOT-INVADER
beatcow
adobe photoshop. but beware; the raccoon is ripped from the snes game kikikaikai (pocky and rocky), so i didn't create it and didn't draw it, just ripped it.
but when i create pixel sprites, i'm using photoshop ^^
ripX
thanks for the hint. the idea seems interesting, i'll try it out. i have also found a very interesting thread in the actionscript forum, using some splice and indexOf methods.
-
yeah I've just been looking at the slice in the actionscript dictionary - check it out and all will be clear!
-
you want to splice the array
Code:
myArray=[1, 2, 3, 4]
trace(myArray)
myArray.splice(1,1)
// start from position 1, remembering arrays start at 0
// splice (position, the number of values to remove)
trace(myArray)
mike
-
Elvis...who tha f**k is Elvis?
Just pop it:
pop()
Also check out the dictionary under arrays...it shows the different basic ways of manipulating an array 
...sorry pop is for the last element! Splice would be the way to go!
selection.splice(startPoint,deleteCount);
ie.:
selection.splice(1,1);
startPoint points to where in the arrayy to start, and deleteCount tells how many you want to delete...if you wanted the 3 to go to you would change that from 1 to 2!
Last edited by phreax; 03-30-2003 at 05:49 AM.
-
ism
token3 is on the money there. splice is a killer on the performance though. mabey look at actionscript.org (library section) or figtree for a substitute method.
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
-
just a thought
you could use shift () to return and remove the first element in the array
and you could use (as phreax said) pop () which will return and remove the last element
if you needed to remove elements that are not either at the start or the end of the array then you would use splice ()
that's the only way to remove elements from an array without creating a new one.
mike
-
SaphuA
:)
pizza = new Array("olives", "cheese", "mushrooms", "tomatos");
slice = pizza.slice(1, 3);
Hows this?
//--Note---
This will leave the pizza array unchanged, it only makes a new array/varraible with: cheese and mushrooms
Last edited by mosterdfles_flash; 03-30-2003 at 07:10 AM.
-
DOT-INVADER
thanks for your answers guys. i just found my solution in the actionscript forums, it works perfectly for what i want.
token3, your code works perfectly, BUT what if i want to remove another value again after the first changes? it will not work anymore because the values have already changed their original order.
> i wanted to know this because in my game you have the ability to choose 4 stages + 1 exit menu. so there's a selection screen.
each time you success a stage, it's no more in the selection screen (i delete it in the array containing the stages remaining to play).
here's the code:
Code:
Array.prototype.indexOf = function (what) {
for (var j=0;j<this.length;j++) {
if (this[j] == what) {
return j;
}
}
return -1;
};
s_selection = [1,2,3,4,5,6];
s_selection.splice(s_selection.indexOf(4), 1);
trace(s_selection);
^ here i delete the value "4" in the array "s_selection".
in my game it means: stage 4 already played and finished, so it's no more selectionnable in the stage selection screen, because it doesn't appear anymore. i hope it will help you also ^^
splice is a killer on the performance though
maybe, but it will be used only in a load event each time you finish a stage, so it's ok.
** edit **
mosterdfles_flash
this should work:
Code:
Array.prototype.indexOf = function (what) {
for (var j=0;j<this.length;j++) {
if (this[j] == what) {
return j;
}
}
return -1;
};
pizza = ["olives", "cheese", "mushrooms", "tomatos"];
pizza.splice(pizza.indexOf("mushrooms"), 1);
trace(pizza);
^ this for example will delete the "mushrooms".
Last edited by marmotte; 03-30-2003 at 08:08 AM.
-
well, from your code it looks like you won't be allowing the player to replay a section. Wouldn't it be easier if you just set the array position to null or undefined?
//
myArray = ["A","B","C","D"]
// you complete "C"
myArray[2] = undefined // or ""
//
would that be any better?
-
DOT-INVADER
yes, it works also, with some additional codes...
> each time i press left or right (in order to choose the stage i want to play) i have a var increamenting/decresing (s_selection[my var]), so if there's a "blank" value (i mean undefined or "" like you said) in the array, the correct picture of the stage will not be displayed; that's why i want the array to only have complete values.
of course i said your idea will work "with some additional codes", so i can develop a little bit more your idea, and when we encounter a undefined or "" in the array, it looks the next/previous element.
thanks anyway
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|