Centering The Content
Lets take our new TextFiled and see how we work with the x,y coordinates in flash and how we can center our TextFiled in the center of the stage.
We explore in this video the 'stage' and see how we interact with the stageWidth and stageHeight.
Video Transcript
00:00The next step that I want to do at this stage,
00:02I want to take my text Hello, and I actually
00:04want to make sure that it's centered to the stage.
00:08And the way I'm going to do that,
00:09and I'm just going to type it for again,
00:11I'm going to grab my text Hello, and I'm
00:13going to change a few properties.
00:14Actually, I want to change my text Hello dot x.
00:20And a lot of display objects share similar attributes,
00:23which are x, y, width, height, scale, scale x, scale y.
00:29And we're going to definitely look into those.
00:31And we're going to just get used to them.
00:33And they're quite intuitive.
00:35We want to move our x and y values.
00:37The only thing that's probably not intuitive
00:39is how x and y coordinates work inside a flash.
00:43So if I just move around on my stage a second,
00:45my x, 0, 0 is x, y, 0, 0 is right here.
00:50Going downwards will grow the y.
00:53Going to the right will make my x grow.
00:56Now, that's not true in math.
00:58Probably this region would probably be negative-wise.
01:02But because it's probably easier to work with positive values,
01:05that's how it works in flash.
01:07Now, there are negative values.
01:08In theory, we could access things
01:10that are in the minus area, which
01:11would be outside of our stage.
01:12We'll look at that later on.
01:17So back to our text Hello.
01:18And I want to center my x in a certain position.
01:20But I don't want to hard code it and try to figure it out.
01:23If I just put 400, then it's going to move it to 400.
01:25But I want it to be really in the center.
01:28To get it really in the center, the way
01:29I could do that is basically approach my text field
01:32and actually ask for it first to ask my stage, which
01:35we met earlier.
01:38Stage, and we're going to ask for the stage or property
01:40of the stage, which is called stage with.
01:45Now, contrary to ActionScript 2, where it was upper case
01:49stage, and it wasn't stage with, but it was actually with.
01:52With and stage.with will not return the value
01:55that we're expecting for.
01:56We actually need to call stage with.
01:59If I set my x to be stage.with and divide that by 2,
02:06I'm getting the center point.
02:08So my x is going to start exactly in the center.
02:11Now, we have only one problem is that we're not counting in also
02:14the size of our text field.
02:16To do that, the next step I want to do
02:18is actually subtract out of it also half
02:21of the size of my text field.
02:23To do that, I'm going to type text Hello.with again.
02:27This time around, I'm just calling with and not stage
02:30with, and I'm going to divide that by 2.
02:33Let me just do the same thing for y before we publish it again,
02:35because this is already starting to sink in.
02:40And we're going to type here y this time around.
02:44And instead of stage with, I'm going
02:45to change it to stage height.
02:50And instead of, I'm going to type height again.
02:54So basically, I'm stage.stageHeight
02:57divided by 2 minus textHello.height divided by 2.
03:01And notice, we're always working with camel case
03:04when we're working in action script 3.
03:07So we're going under that assumption
03:08when we're calling properties.
03:10They always start at lower case.
03:12When we call classes, they always
03:14start with an upper case, just a standard.
03:17And that way, we could easily notice
03:19that this is actually an instance.
03:21And we created already an instance such as the textHello.
03:23So all these different things that we have here
03:25are instances, or variables, or properties,
03:28or we'll see later on OTSO methods.
03:30And functions, which we've met.
03:32They always start with a lower case.
03:34And then the classes and the definitions,
03:36the things that are not tangible,
03:38are defined in upper cases, such as textField,
03:42which is not tangible.
03:43It's a blueprint for a text field.
03:46But this is the actual text field.
03:47Thus, we started up with a lower case.
03:50And now it's not the rule, but that's
03:52how you really want to do it, because it's
03:53going to make it so much easier as your program.
03:56All right, so let's just run this little application
03:58and see if it's actually centering.
04:00And bam, we got our application, and it's centered.
04:03And we are very, very happy.
