Removing selective data from a variable

Hi there,

I am in real need of help.

I have created a user interactive colour sample system that sends accumulative preferences from an external swf to the _root timeline when different colour sample buttons are clicked.

The code looks as follows:

on (press) {
_root._root.myvariable += "
colour1";
}

I am now faced with the problem of someone changing their mind and not wanting a particular colour anymore.

I want them to be able to click on the button again to remove a previously selected colour choice.

Can anyone help me?

I tried putting -= instead of += , but it retuns the variable with a value of NaN

Cheers

Try this: pressing the button once sets myVar = orange. Pressing again, set its value to null.
[AS]my_button.onPress = function() {
!checked ? this._parent.myVar=“orange” : this._parent.myVar=null;
checked = !checked;
trace(this._parent.myVar);
};[/AS]

Hi claudio,

Thanks for that.

The only problem is, I am not trying to clear the variable completely when the button is clicked.

As there are many buttons (all with different colour choices), the user will be selecting multiple colours. Hence the reason I scripted the variable with the += operator, so that it lists the choices accumulatively.

The action I need each colour button to perform, is that if a particular button has been clicked on once (which will mean its own name has been added to the list inside the variable), it has the option to be clicked on again to remove that particular name from the variable while still leaving any other previously clicked colour names still inside the variable.

The data removal needs to be specific to the button pressed.

Many Thanks,

Set one variable for each button maybe? :-\

thanks anyway claudio…

there are about 50 buttons in total… so I don’t really want to be setting 50 separate variables.

Cheers,

the chad

You can store the variables in an array and remove them from the array when needed. For example…

[AS]myArray = [“var1”, “var2”, “var3”, “var4”, “var5”];
Array.prototype.removeItem = function(pos) {
this.splice(pos, 1);
trace(this);
};
myArray.removeItem(3);[/AS]

(since arrays start counting at 0 this will remove “var4”)

[edit]if it would be easier on you you could do this… [AS]myArray = [“var1”, “var2”, “var3”, “var4”, “var5”];
Array.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
trace(this);
};
myArray.removeItem(3);[/AS] And that way YOU don’t have to count from 0 and removeItem(3) will actually remove “var3” [/edit]

thanks lost i was about to go on the same direction :thumb:

no problem claudio, I forgot to mention… to add colors to the array you can use…

[AS]on (press){
_root.myArray.push(“colour1”);
}[/AS]

And then to split it back into a string you can use a for loop… for example… [AS]myArray = [“colour1”, “colour2”, “colour3”, “colour4”, “colour5”];
Array.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
return this;
};
myArray.removeItem(3);
for (var i = 0; i<myArray.length; i++) {
myString += myArray*+"
";
}
trace(myString);[/AS]

And here is yet ANOTHER way to remove items from an array… this method is by the actual value of the item and not the array position… (this may get confusing if multiple array positions contain the same values, the last added value in this case would be the one that gets deleted) [AS]myArray = [“var1”, “var2”, “var3”, “var4”, “var5”];
Array.prototype.removeItem = function(value) {
for (var i in this) {
if (this* == value) {
this.splice(i, 1);
break;
}
}
};
myArray.removeItem(“var2”);
trace(myArray);[/AS]

Ok sport, i’ve given many points in the right direction, now run run run like the wind and take it where you can.

Thanks lostinbeta,

I really appreciate your help.

I am not that great at Actionscript and need a bit more help along the way if you don’t mind.

I have never set up an Array before. I don’t mean to sound dumb but how do I go about it. Do I need to get rid of everything I have done so far.

So far I have placed a variable on the main timeline and called it _root.sampleselect

Then in the external swf with all the colour sample buttons, I have coded each button to add to the variable accumulatively when clicked on

on (press) {
_root._root.sampleselect += “
colour1”;
}

Do I need to replace this with a new action for selecting the colours as well?

Would you mind breaking down the code you sent me and telling me what parts I need to customise and what parts should be left as they are?

How do I change the storage variable into an Array? OR am I on the wrong track?

Do I put all this code on every button, just for removing the colours from the array? Or does it do both actions (adding and deleting)?

on (press){
_root.myArray.push(“colour1”);
}

myArray = [“colour1”, “colour2”, “colour3”, “colour4”, “colour5”];
Array.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
return this;
};
myArray.removeItem(3);
for (var i = 0; i<myArray.length; i++) {
myString += myArray*+"
";
}
trace(myString);


Cheers,

the chad

[AS]on (press){
_root.myArray.push(“colour1”);
}[/AS] is all you need for your buttons, this will add “colour1” to the end of the array called myArray in the _root timeline.

[AS]myArray = [“colour1”, “colour2”, “colour3”, “colour4”, “colour5”];
Array.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
return this;
};
myArray.removeItem(3);
for (var i = 0; i myString += myArray*+"
";
}
trace(myString);[/AS] was just merely an example of how to go about converting your array into a string the way you wanted.

If you would like to point out which parts of which scripts you don’t understand I will gladly try to explain them more clearly if I can.

Oh yeah, and heres a few good tutorials on arrays that may help you…

Kirupa Array Tutorial:
http://www.kirupa.com/developer/actionscript/array.htm

The Power of Arrays I:
http://www.flashmagazine.com/html/480.htm

The Power of Arrays II:
http://www.flashmagazine.com/html/509.htm

As far as I know, I don’t even have an Array yet. I only created a variable on the main timeline… but the buttons all need individual code of some sort to send their value to the variable.

How do I change the variable into an Array? Maybe I’m not on the right track so I’ll try and break things down so you can explain it to me in laymans terms.

  1. I’m guessing that all of the code you sent me goes on each button?

  2. The variable on the main timeline is called ‘_root.sampleselect’. Does that stay as is and just automatically become my Array? Or do I have to change something?

  3. Now to the code: (1st part)

on (press){
_root.myArray.push(“colour1”);
}

Do I replace the myArray part with _root.sampleselect.push(“colour1”) ?

  1. next part:

myArray = [“colour1”, “colour2”, “colour3”, “colour4”, “colour5”];
Array.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
return this;
};


Should the code above read like this: (I’m just taking a wild guess, probably am wrong. What other parts need customising? Do I need to change the words ‘prototype’, ‘function’, ‘splice’ to anything personalised?

_root.sampleselect = [“colour1”, “colour2”, “colour3”, “colour4”, “colour5”];
_root.sampleselect.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
return this;
};


  1. last part

myArray.removeItem(3);
for (var i = 0; i myString += myArray*+"
";
}
trace(myString);


Do I change the myArray parts and myString parts to particular variable names?


  1. Basically, I don’t understand what to put where…

Hope you can help.

Sorry, for my difficulty in learning.

the chad

  1. As stated, the only code for the buttons is contained in the on(press) handler (and that is the myArray.push() stuff)

  2. You will no longer need a variable, you will have to store the new info into the array and then when you are ready you parse that array into a string (one of my posts has an example of that)

  3. Since you will be using an array and not a variable you will have to target the name of your array.

  4. No, prototype functions are Object specific functions (MovieClip.prototype, Button.prototype, Array.prototype, String.prototype, etc) so it will stay as Array.prototype. here is a kirupa.com tutorial on prototypes…

http://www.kirupa.com/developer/actionscript/tricks/prototypes.htm

  1. You change the myArray part to whatever name you assign to your array, and you change the myString part to whatever name you want to assign the final output string.

  2. You put all of the code on a frame (since you are new to AS I would say preferably the _root timeline) EXCEPT the on(press) stuff, that code will go on your buttons.

To be able to do something of this magnitude it is VERY important that you have knowledge of functions, prototypes, arrays/array manipulation and for loops. I recommend reading up on those because they are needed for this, and will become VERY handy in the future if you need them.

thanks for that…

Sorry, I only got your links to the tutes after I sent all my questions through. I am reading the tutes at the moment and finding them very helpful.

I will have a crack at it and see how I go…

Cheers for your help.

the chad

Hi again,

I am trying your script out, but as soon as I paste it into the Actions Panel, I get the following error message:


Scene=Scene 1, Layer=Layer 1, Frame=1: Line 7: ‘;’ expected
for (var i = 0; i myString += myArray*+"
";

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 8: Unexpected ‘}’ encountered
}

Try the script I have pasted between the AS tags. When you copy and paste a script as plaintext on the forum (like you did in your last post) it converts the < symbol and makes the forum think everything on that line is part of an HTML tag.

If that isn’t the problem, then the answer can be found in this line

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 8: Unexpected ‘}’ encountered
Apparently there is a curly bracket there that shouldn’t be.

I tested all the code I pasted in here so anything extra added to cause the error had to be added manually.