View Full Version : removing selective data from a variable
the chad
September 4th, 2003, 11:01 PM
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 += "\ncolour1";
}
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
claudio
September 4th, 2003, 11:49 PM
Try this: pressing the button once sets myVar = orange. Pressing again, set its value to null.
my_button.onPress = function() {
!checked ? this._parent.myVar="orange" : this._parent.myVar=null;
checked = !checked;
trace(this._parent.myVar);
};
the chad
September 5th, 2003, 12:07 AM
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,
claudio
September 5th, 2003, 12:11 AM
Set one variable for each button maybe? :-\
the chad
September 5th, 2003, 12:16 AM
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
lostinbeta
September 5th, 2003, 12:22 AM
You can store the variables in an array and remove them from the array when needed. For example...
myArray = ["var1", "var2", "var3", "var4", "var5"];
Array.prototype.removeItem = function(pos) {
this.splice(pos, 1);
trace(this);
};
myArray.removeItem(3);
(since arrays start counting at 0 this will remove "var4")
if it would be easier on you you could do this.. myArray = ["var1", "var2", "var3", "var4", "var5"];
Array.prototype.removeItem = function(pos) {
this.splice(pos-1, 1);
trace(this);
};
myArray.removeItem(3); And that way YOU don't have to count from 0 and removeItem(3) will actually remove "var3"
claudio
September 5th, 2003, 12:25 AM
thanks lost i was about to go on the same direction :thumb:
lostinbeta
September 5th, 2003, 12:35 AM
no problem claudio, I forgot to mention... to add colors to the array you can use...
on (press){
_root.myArray.push("colour1");
}
And then to split it back into a string you can use a for loop... for example... 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[i]+"\n";
}
trace(myString);
lostinbeta
September 5th, 2003, 12:45 AM
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) myArray = ["var1", "var2", "var3", "var4", "var5"];
Array.prototype.removeItem = function(value) {
for (var i in this) {
if (this[i] == value) {
this.splice(i, 1);
break;
}
}
};
myArray.removeItem("var2");
trace(myArray);
Ok sport, i've given many points in the right direction, now run run run like the wind and take it where you can.
the chad
September 5th, 2003, 12:55 AM
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 += "\ncolour1";
}
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[i]+"\n";
}
trace(myString);
---------
Cheers,
the chad
lostinbeta
September 5th, 2003, 01:00 AM
on (press){
_root.myArray.push("colour1");
} is all you need for your buttons, this will add "colour1" to the end of the array called myArray in the _root timeline.
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[i]+"\n";
}
trace(myString); 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.
lostinbeta
September 5th, 2003, 01:08 AM
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
the chad
September 5th, 2003, 01:18 AM
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") ?
4. 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;
};
-------
5. last part
myArray.removeItem(3);
for (var i = 0; i myString += myArray[i]+"\n";
}
trace(myString);
--------
Do I change the myArray parts and myString parts to particular variable names?
---------
6. Basically, I don't understand what to put where...
Hope you can help.
Sorry, for my difficulty in learning.
the chad
lostinbeta
September 5th, 2003, 01:30 AM
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
5) 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.
6) 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.
the chad
September 5th, 2003, 01:38 AM
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
the chad
September 5th, 2003, 02:21 AM
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[i]+"\n";
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 8: Unexpected '}' encountered
}
lostinbeta
September 5th, 2003, 02:26 AM
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.
the chad
September 6th, 2003, 01:51 AM
Hi again...
I am just not getting this... I want to though!
I am wondering if I did not explain properly what the end result is that I am trying to achieve?
I know I said I am trying to create an accumulative colour sample selection system where by clicking a button once, that particular colour is added to a variable (which you said would be better stored in an Array)...
Then by clicking on it a second time, the colour is removed (from the Array)...
But what I didn't include, is that when a 'Submit' button is pressed after selecting your colours, I need all the selections that have been stored in the Array to be sent and listed inside a variable (Input Text box) that is part of an email form, which will be emailed when pressing send.
I know how to do the email part with PHP, I just need someone to explain in plain english, where to put each piece of code (i.e. - I have a main timeline, where the email form is held, then all the colour samples and buttons are in external swf's loaded into a movie clip on the main timeline)...
Thanks to those who have helped. I am slowly getting it... but I just need an extra hand in how to get the info that is 'suppose' to be inside the Array, into the variable in the email form on the main timeline.
Please outline exactly where each piece of code is to be assigned.
Thankyou again, for everyone's help and to whoever helps me here.
Cheers,
the chad
the chad
September 9th, 2003, 07:12 PM
Hi lostinbeta,
Well, I have been trying all sorts of ways to acheive this and still to no avail.
The good news is that it is 'kind of' working. But not fully. So I was hoping that if I describe to you what is happening, you can tell me what's going wrong.
As directed by you, I placed the following code on the main timeline:
myArray = ["colour1", "colour2", "colour3", "colour4"];
Array.prototype.removeItem = function (pos) { this.splice(pos-1, 1);return this;};
myArray.removeItem();
for (var i = 0; i<myArray.length; i++) {
_root.sampleselect += myArray[i]+"\n";
}
trace (_root.sampleselect);
Then on my colour button inside my external movie, I placed the following:
on (press) {
_root.myArray.push("colour1");
}
I tested the movie and what is happening is that when I press the 'colour1' button, all of the colours inside the Array, get displayed inside my variable.
This is not what I am trying to acheive.
Essentially, what needs to happen, is that the variable that the Array outputs the choices to, needs to only shows the choices that have been clicked on (i.e. 'colour1', 'colour5', 'colour14'... and so on)...
I do not want every colour listed when I only click one button... only the colour that has been clicked on.
To top it all off, when the button is clicked on a second time, it is not removing its respective colour from the Array/variable at all.
I need it to perform both functions.
I think we are headed in the right track, cos' its partially working... have you got any further ideas?
Cheers,
the chad
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.