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
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.
(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]
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.
[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.
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.
I’m guessing that all of the code you sent me goes on each button?
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?
Now to the code: (1st part)
on (press){
_root.myArray.push(“colour1”);
}
Do I replace the myArray part with _root.sampleselect.push(“colour1”) ?
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?
As stated, the only code for the buttons is contained in the on(press) handler (and that is the myArray.push() stuff)
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)
Since you will be using an array and not a variable you will have to target the name of your array.
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…
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.
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.
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.