AS3 Textfield Whitelisting - actionscript-3

So I have this chatting application that only allows users to say specific words in an array (whitelisting). I really need some help creating a function to check each word, and replace those with "###" that are not in the whitelist. I have an external Dictionary.txt file, but I also have an array set up in the code (whitelist).
Please help me out! Thanks

this is the simple way i can imagine it
var words:Array = ["test", "fu--"];
//
function dicionary(word:String) {
for (var i:Number = 0; i < words.length; i++) {
if (words[i] == word) {
trace ("####");
}
}
}
//
dicionary("fu--");
dicionary("Hello");
Hope it helps
EDITED
even better like this, because now it can replace words within sentences
var words:Array = ["test", "fu--"];
//
function dicionary(word:String) {
for (var i:Number = 0; i < words.length; i++) {
word = word.split(words[i]).join("####");
}
return(word);
}
//
trace(dicionary("fu-- I was wrong!"));
trace(dicionary("Hello"));

Related

How To Link To A Frame Adobe Flash CS6 Action Script 3

i need help with my adobe flash assignment , i m still new to adobe flash :( how to tell the user if they input the wrong keyword how to link them to a frame .
var i:int = 0;
var names:Array = new Array("html","head","body");
var frames:Array = new Array("6","7","8");
searchhtmlll.text ="";
searchhtml.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
findInArray(searchhtmlll.text);
gotoAndStop(frames[i]);
}
function findInArray(str:String):int
{
for(i = 0; i < names.length; i++)
{
if(names[i] == str)
{
return i;
}
}
return 0;
}
I am answering based on whatever limited information you have provided in your question, and from what I understand.
Your method findInArray has a return type of int, where as you are just calling it without assigning it to a variable.
So your code should be:
var result:int = findInArray(searchhtmlll.text);
gotoAndStop(frames[result]);
Hope this helps.

Executing a function for all instances of a class?AS3

I'm really new to AS3 and I was wondering how I can apply a function to all instances of a class I'll show you guys what I have, is there anything I can do?
var cloud:Cloud;
for (i=0; i<5; i++)
{
cloud = new Cloud();
addChild(cloud);
}
this makes many instances of the Cloud class.
Later on I want to execute a function on all of these instances at once, how would I do that?
Store them in an Array or Vector:
var clouds:Vector.<Cloud> = new <Cloud>[];
for(var i:int = 0; i < 5; i++)
{
var cloud:Cloud = new Cloud();
clouds.push(cloud);
addChild(cloud);
}
Then iterate over that list and call a function on each item:
for each(var cloud:Cloud in clouds)
{
cloud.do();
}

How to compare properties of objects inside a vector?

I have a couple of display objects moving around the screen, the all belong to the same Vector.
I would like to create a function that lets me pick an object with the lowest x value at the moment the function is called.
This is quite easy and if you're into programming, you should be able to do it yourself, but if you're a beginner, I'll give you some code:
var vec:Vector.<DisplayObject> = new Vector.<DisplayObject>();
function getLowestXObject():DisplayObject
{
var minX:DisplayObject = vec[0];
for(var i:int = 1; i < vec.length; i++)
{
if(minX.x > vec[i].x)
{
minX = vec[i];
}
}
return minX;
}

Flash action script3 problem, can't change property name

i'm not very good at AS3, but i imported XML to my flash and i want to change it dynamically from drop down menu. I need to change "THISPLACE" in script below with simple string, how do i do that? Thanks for help :)
function uzkrautXML():void
{
var XMLURLLoader:URLLoader = new URLLoader();
XMLURLLoader.load(new URLRequest(xmlArdesas));
XMLURLLoader.addEventListener(Event.COMPLETE, processXML);
}
function processXML(event:Event):void
{
var theXMLData:XML = new XML(XMLURLLoader.data);
visoSk = theXMLData.THISPLACE.length();
for (var i:Number = 0; i <visoSk; i++)
{
skArray.push(theXMLData.THISPLACE[i]);
}
uzkrautSkelbimus();
}
I know there are mistakes in code, but dont mind it, i need to know just how replace that plase
You can use child() method of top level XML class.
function processXML(event:Event):void
{
var theXMLData:XML = new XML(XMLURLLoader.data);
theXMLData.ignoreWhite = true;
visoSk = theXMLData.child("THISPLACE_NAME").length();
for (var i:Number = 0; i <visoSk; i++)
{
skArray.push(theXMLData.child("THISPLACE_NAME")[i]);
}
uzkrautSkelbimus();
}
Hope this helps.

ActionScript - Retrieving Index Of Specific Filter

i have a few filters on a sprite. on mouse over i would like to access one of the filters in the filters array, but i'm having a bit of trouble trying to accomplish this.
mySprite.filters = [new DropShadowFilter(), new GlowFilter(), new BlurFilter()];
mySprite.addEventListener(MouseEvent.MOUSE_OVER, mouseOverEventHandler);
function mouseOverEventHandler(evt:MouseEvent)
{
//obtain indexOf the GlowFilter
trace(evt.currentTarget.filters[evt.currentTarget.filters.indexOf([Object GlowFilter])]));
}
the above code doesn't work. what's the proper way to get the index of a specific filter in a filters array?
If I understand correctly, you're essentially trying to do this:
var index:int = evt.currentTarget.filters.indexOf([Object GlowFilter]);
The bracketed part is not valid Actionscript it shouldn't even compile. What you need to do is to iterate over the filters and test them yourself since there's no way to search for a specific class with indexOf.
Try this instead:
function mouseOverEventHandler(evt:MouseEvent) {
var glowFilter:GlowFilter;
for (var i:int = 0; i < evt.target.filters.length; i++) {
if (evt.target.filters[i] is GlowFilter) {
glowFilter = evt.target.filters[i];
break;
}
}
}
Also, if you're going to fiddle with the filters in the array Flash won't accept in-place modifications, so you need to re-set the array once you've changed it:
function mouseOverEventHandler(evt:MouseEvent) {
var glowFilter:GlowFilter;
for (var i:int = 0; i < evt.target.filters.length; i++) {
if (evt.target.filters[i] is GlowFilter) {
glowFilter = evt.target.filters[i];
break;
}
}
if (!glowFilter) return;
glowFilter.blurX = 10;
var filters:Array = evt.target.filters;
filters[i] = glowFilter;
evt.target.filters = filters;
}