Read Text File with AS3 and Tween Each Word from Small to Large out of the screen - actionscript-3

I am looking for some advice on the best way to read in like 200k words and have them each tween from the center of the screen as small dots and tween up to the word filling the SWF then "fly through my head" not literally, but you probably get it...
What would be the best way in AS3 to go about this? I am fairly new to it.
Thanks!

Depending on the origin of your text, you may have to use regular expressions to get rid of punctuation and replace any amount of spaces by a set delimiter.
You could then use the split method of the String class to turn your text into an Array of words.
Each word can then be assigned to a Textfield. Since the Textfield is a DisplayObject, all the manipulations you are mentioning above become possible.
You may be able to streamline all this by creating a class that extends Textfield and defining various methods for the motions you want to implement.
You'll probably want to look at the Timer class and some tweening libraries
Pseudo Code
- Clean up String with regular expressions
-> expected result var cleanString:String = "word1;word2;...wordn";
- Turn String into Array
var words:Array = cleanString.split( ";" );
- Create a class that extends Textfield and define a manipulate() method
var tf:MyTextField = new MyTextField();
//this method could take parameters
//such as x, y, scale , time , delay , ease etc...
tf.manipulate();
- Create an Array( Vector ) of Textfields
loop thru words Array to return array of Textfields
var objects:Array = [ tf1, tf2 , etc...]
- Manipulate objects
loop thru objects Array to manipulate them

if you don't want to do it by hand (like PatrickS described) maybe this will help you ...
http://www.greensock.com/splittextfield/

Related

AS3: Change TextField Text on multiple instances of the same MovieClip

I have a MovieClip called "number" in the library. I need to add multiple instances of that MovieClip to stage. Instances should be called number1, number2,number3...and each one needs to have different text inside it.
Is it possible to do this without code, just using flash interface tools? If not, could someone help me with coding that?
Thanks!
For an Class linked MyNumber containing a text field named output:
const N:int = 3; // 3 instances
const TEXTS:Array = ['text 1', 'text 2', 'text 3']; // 3 texts
var n:MyNumber;
for (var i:int = 0; i < N; i++) {
n = new MyNumber();
n.y = 50 * i;
n.output.text = TEXTS[i];
this.addChild(n);
}
You have to use code - at least a little bit.
In addition to #helloflash's answer, here is a simpler solution (with some caveats described below).
On your movieClip, make your text box dynamic, and give it an instance name of txt (or whatever you'd like). Then, put the following line of code on the first frame of your movieClips's timeline:
txt.text = this.name; //works if your text is a simple word with no spaces/puntuaction/symbols and doesn't match any actionscript keywords
This will set the text to whatever the instance name of each movieClip is. Will work great if you text is something simple like "Hello" or "Player1".
Now, if you're text is a number (or starts with one), or your text matches a keyword or already defined variable (like this/continue/function/break/stop/play etc), you'll need make it a bit more complicated, something like this:
txt.text = this.name.replace("$MC_","");
Then give your instance name in this format: $MC_stop, the code will strip out the $MC_ part and show the rest. so the text field would be "stop".
Now, if you want to include spaces, or most symbols (dollar sign, underscore and dash I think are the only supported ones), you'll have to add a replace for each one and create a place holder for that character.
So if your text was "This is my text", you should give it an instance name of `this_is_my_text" and this should be the code:
txt.text = this.name.replace("_"," "); //replace all underscores with space
Add as many replace statements for as many characters you need.
So, if you text was "1. This is my text!!!" - The instance name could be: $MC_1$dot_This_is_my_text$ex$ex$ex and the code:
txt.text = this.name.replace("$MC_","").replace("_"," ").replace("$dot",".").replace("$ex","!"); //you can keep chaining on as many replace statements as you need.
Of course, at this point you might as well just use full on code like #helloflash's answer. But if you text isn't that complicated, this may be a good solution for you.

Square bracket notation to dynamically load library items?

So I've spent an embarrassing number of hours trying to save myself a few minutes and make my code a bit neater, and Google has produced nothing, so now I come crawling to stackoverflow. The problem is with square bracket notation + library items:
So let's say you have a MovieClip called "myMC_5", and you want to set its X position to 0..
myMC_5.x = 0;
..and if you don't want to hard-code the name of the MC but instead you want one line of code to move a specific MovieClip based on a variable, you could do something like this:
var selectMC = 5;
root["myMC_"+selectMC]x = 0;
..and this will have the exact same effect as myMC_5.x = 0, except that this time you must specify the location ("root" or "this" or something).
THE PROBLEM:
I'm working on a game in which the graphic for the background is loaded from the library, and it's different for each level. The initial loading of the vector from the library looks like this:
private var land:vector_land0 = new vector_land0();
..and this works fine, but it only loads that one specific vector. There should be about 30 or more. I'd like to just have 1 line of code in the constructor to load any of them, based on a variable which keeps track of the current level, like this:
private var land:["vector_land"+theLevel] = new ["vector_land"+theLevel]();
..but that doesn't work. I get syntax errors ("expecting identifier before leftbracket") because you need to specify the location of the object, like in the first example:
root["myMC_"+"whatever"].x = 0;
..but this library item has no "location". So, how the heck do I dynamically load a vector from the library? It's not "on the root", or anywhere else. It has no location. I refuse to believe that the standard method for accomplishing this is to create 30 different classes or write a giant block of code with 30 "if" statements, but searching Google has found nothing. :(
It sounds like you're looking for getDefinitionByName(), which you could use to do something like this:
import flash.utils.getDefinitionByName;
private var LevelVectorClass:Class = getDefinitionByName("vector_land" + theLevel) as Class;
private var land:Object = new LevelVectorClass();
This is a horrible way to solve the situation, I don't recommend using square brackets anywhere but arrays. I recommend you putting the "lands" into a LandContainer MovieClip, each frame of that MovieClip would container 1 graphic. It is much cleaner, and you could create constants to store the "identity" of the frames.Example:
var land:LandContainer = new LandContainer();
land.gotoAndStop(FIRST_LEVEL); //FIRST_LEVEL is a constant integer
You can even reuse this LandContainer instance because you can set it's visibility, remove from the display list, set it's frame to the next level without creating another instance. On second thought, I would write a wrapper for this class. Aka link it to your own class which extends the MovieClip class and create custom functions, fields... etc..
This dynamic thing is horrible, hard to maintain, and not efficient at all. Don't know why did not they delete it from AS3... they should have.

How do I HitTest two rotating objects properly? (A way to avoid bounding boxes)

I took an intro level flash course in college this semester and our final task was to make a mini-flash game.
I had to make a pipe-dream type game where there are a number of levels, and in each level you have to align the pipes so that the water flows and then you can pass to the next level.
I successfully made the first level, but upon making the second level,
where I placed a lot of curved pipes (by curved pipes I mean the attached image: ![Curved Pipe]: (http://imgur.com/mwpXAMn) )
I discovered that the method I use to decide when a level is complete is not working properly.
I was using HitTestObject, basically, I was testing whether 2 objects, Pipe_1, and Pipe_2, were intersecting. If all pipes intersected in the correct way, then procession to the next level is granted.
The problem with this I discovered is that flash has bounding boxes for movie clips you make, and that HitestObject uses bounding boxes to test for hits. Therefore, when you rotate a leftpipe so that it does not touch a straight pipe on screen, the bounding boxes still touch and it returns "collision" when in fact it is not actually touching on screen.
I looked up and found that you can use HitTestPoint but I can't figure out how to somehow make dynamic variables (that change upon rotation of object) that store one or two specific points on the leftpipe, say the two ends of it.
Once If I figure out how to get these values into a variable correctly, then I can figure out how to do HitTestpoint.
Also, I know of the LocaltoGlobal function but no matter what I try it keeps coming up with:
"Scene 1, Layer 'Layer 1', Frame 1, Line 30 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.geom:Point."
meaning I don't know the correct code to store an x and a y coordinate as dynamic variables.
edit: ok since a person asked, I hunted this piece of code off the web and this is the one I was trying to play around with but to no avail:
How to use HitTest for 2 rectangles, r1 is rectangle 1 r2 is rectangle 2.
var r1width:Number = 135.0; //width of retangle 1 whith rotation 0
var r2width:Number = 93.0; //width of retangle 2 whith rotation 0
var p1:Object = {x:(r1width/2), y:(r1width/2)};
var p2:Object = {x:(-r1width/2), y:(r1width/2)};
var p3:Object = {x:(-r1width/2), y:(-r1width/2)};
var p4:Object = {x:(r1width/2), y:(-r1width/2)};
r1.localToGlobal(p1);
r1.localToGlobal(p2);
r1.localToGlobal(p3);
r1.localToGlobal(p4);
var p5:Object = {x:(r2width/2), y:(r2width/2)};
var p6:Object = {x:(-r2width/2), y:(r2width/2)};
var p7:Object = {x:(-r2width/2), y:(-r2width/2)};
var p8:Object = {x:(r2width/2), y:(-r2width/2)};
r2.localToGlobal(p5);
r2.localToGlobal(p6);
r2.localToGlobal(p7);
r2.localToGlobal(p8);
if((r2.hitTest(p1.x, p1.y, true))||(r2.hitTest(p2.x, p2.y, true))||(r2.hitTest(p3.x,
p3.y, true))||(r2.hitTest(p4.x, p4.y, true)))
{
trace('collision');
}
if((r1.hitTest(p5.x, p5.y, true))||(r1.hitTest(p6.x, p6.y, true))||(r1.hitTest(p7.x,
p7.y, true))||(r1.hitTest(p8.x, p8.y, true)))
{
trace('collision');
}
I did not write this code and it does not work. I'm not sure what "Object" is because I've never used it before, I'm assuming in this case it's sort of acting like a coordinate pair.
Also, this code is to hittest 2 rectangles, whereas I'm using an L-shaped pipe, so the x/y calculation would be quite different I imagine.
This code above gives the same error that I posted before:
Implicit coercion of a value with static type Object to a possibly unrelated type flash.geom:Point.
and it gives it first on line r1.localToGlobal(p1);
Instead of using Object, you need to use Point like so:
var p1:Point = new Point(r1width/2, r1width/2);

How to copy Input Text Value into Dynamic Text that is in a MovieClip1 Inside a MovieClip2

Current my code is as such
setcustomlocation.addEventListener(MouseEvent.CLICK,customlocation2);
function customlocation2(e:MouseEvent):void
{
locationinput.text = FlashingWon.Won1.name.text;
}
I'm trying to make it such that it would copy the input text field values into a dynamic text. However, it throws up the error that
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main/customlocation2()[main::frame1:9]
Which can I only assume that it is not able to communicate with the dynamic text field in the movieclip within another movieclip.
First, you can be 100% sure that it CAN communicate with the dynamic TextField in the MovieClip.
From you description I understand that you wish to copy from input into dynamic. But from your code I see that you take from the dynamic into the input, please check that out:
// This will copy in such a way: input <= wonText
locationinput.text = FlashingWon.Won1.name.text;
// This will copy from input
FlashingWon.Won1.name.text = locationinput.text;
Anyhow, the error that you get has nothing to do with this, it's rather like you already noticed, that one of your TextField is not 'found'. For this I recommend you a best practice: To create instances of the objects you want to use and populate them from the stage through the getChildByName method. This way you will promptly now (specially if you do this on construction or init) if you had any misspelling or miss structure on the childs you want to get.
like so:
var inputText: TextField;
var dynoText: TextField;
In your Constructor or else where at a soon level, give to your vars the proper value:
inputText = getChildByName('locationinput') as TextField;
dynoText = FlashingWon.Won1.getChildByName('name') as TextField;
This way you will soon enough know if one of this 2 textFields were not found under the object you give, and you have only one place to miss spell it. Also you will get code completion on your TextFields.
Finally the copy text part:
dynoText.text = inputText.text;
Hope it helps.
Alright, sorry for the delay, I was out on holidays.
I have opened your example and can see where the problems are:
1) You are trying to reach the FlashingWon when initiating the dynoText on the FIRST frame like so var dynoText = FlashingWon.Won1.getChildByName('name_txt'); BUT the FlashingWon element is ONLY available on the FIFTH frame. Meaning that you cannot refer to it quite yet, unless you add it on the first frame and make in invisible till the fifth frame. (You can make it visible in the goto1 function if you wish after the goToAndStop(5) line)
2) You called the TextField on the Won1 element 'name' which is a restricted sting in AS3, so change it to name_txt or label if you wish and it will work.
Let me know how it worked.

Actionscript 2.0 and 3.0: Specific "text" in input box causes certain image to display

I want to know how(and what scripts) to take words from a text input box and cause it to display and image Ex: if the text box said "smiley face" in it, then the image "smiley_face.jpg" would display on a certain movieclip and can be dragged around the stage and when a new image is loaded, it doesn't replace the previous image on the movie clip.
You need to listen for the textInput event and you need to constantly search for "smile" using something like the search() function(u can use strings or regular expressions).
It returns -1 if the string you're searching for wasn't found, otherwise it returns the first index where the searched string was found.
Here's a really basic example:
var ti:TextField = new TextField();
ti.type = TextFieldType.INPUT;
ti.border = true;
addChild(ti);
ti.addEventListener(TextEvent.TEXT_INPUT, onInput);
function onInput(event:TextEvent):void {
if(ti.text.search('smile')!=-1) trace('display smiley image');
}
You did mention smileys, so depending on your level of comfort with actionscript 3, it might be also worth having a look at Thibault Imbert's SmileyRenderer. Careful it uses the new FTE so you need to use Flash Player 10, etc.
yeah. In ActionScript you need to add a listener event to the text field. then you can do something like this. My action script is not so good so I will just stick with logic.
if listener.text == "smile"
smile.jpg
else if listener.text == "frown"
frown.jpg
else
default.jpg
end
You should check out lynda.com for their basic AS screencasts