02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Centering The content on the stage

We are going to do everything via code. Although on your end you can build many things directly on the timeline to keep things very dynamic we will try to avoid having content on the stage limited to our logo. What we really want to do first is set our applications registration point to the top left corner we do that using the StageAlign.TOP_LEFT property and set our application not to scale as well.Once we have an application that doesn't automatically scale we are ready to start adding logic into our application. As such we will start off by listening to the Orientation event and we will recent our logo each time to remain in the center of the screen.

Video Transcript

00:00Welcome back. And it's time for us to set up our initial configurations for our applications via code.
00:07So first of all, I'm just going to go to my asset that's actually on the stage and just give my layer a name.
00:12So I'm going to call it logo. I'm going to lock it up because we're not going to touch it anymore.
00:16Just make sure I remember its name, which is MC logo. All the rest that we're going to do is going to be controlled via code.
00:21I'm going to add a new layer. We're going to call it AS because that's where all of our action script is going to be.
00:26So I could lock that layer as well, select the first frame and go right into my actions panel.
00:31Now there's two things I really want to do by default.
00:33One is I want to make sure that there's no scaling at all to our application.
00:36And the second thing is I want to make sure that our pivot point, where our application will start will be always in the top left corner.
00:46So to do both of them, I'm going to go into my stage.
00:48First of all, I'm going to set my align mode and a nice little shortcut instead of importing it each time when you're working directly in the actions panel.
00:55You can set a new stage align and by setting the word new is going to import it automatically.
01:01And then I could just delete that new and go to my stage align and select top left, which is what I want.
01:06Once I configured my stage align, I could now go to the next item that I want to do, which is the stage align that we had scale mode.
01:16So scale mode. And again, I'm going to go to the same trick by doing a new stage, new stage scale mode.
01:26And it's going to import it free directly, then delete that new.
01:29And I could go right ahead and say that I just don't want to scale anything.
01:33So I'm just going to quickly test it out. I don't have to test it on the device.
01:36I just want to see that when I when I rotate my device and my elements will say the exact same size before I do anything else.
01:43So to do that, I'm going to click on that command return and I'm just going to run it really quickly that way in my regular application runner.
01:51Once the ADL will open up, we'll build a rotate and just make sure that nothing here changes.
01:56So I'm going to go right back to that simulator once it's done loading, set my auto orientation.
02:01And I'm just going to make sure that when I toggle it, it doesn't change and it absolutely doesn't.
02:05You can see that it remains the exact same thing.
02:08All that's left is I want to make sure that it's always in the center of my screen.
02:11And to do that, first of all, what I want to do is I want to go right into my code and I'm not going to worry about that.
02:17I'm going to go right back into my code.
02:19And in my code itself, what I want to do is I want to add an event.
02:22And even before I add an event, what I'm going to do is I'm just, well, let's add the event, right?
02:27Instead of saying before I add the event.
02:29So what we really want to do is we want to get that orientation.
02:33So to get that orientation, I'm going to import flash.display stage orientation.
02:42We'll actually want the stage orientation event.
02:45And we'll create a new stage orientation event.
02:50So stage add event listener.
02:53And we're going to listen into the stage orientation event when the orientation has changed.
02:58And on, we're going to call this on change.
03:03Once it on change happens, we're going to create that function, which accepts the stage orientation event.
03:10And because we don't really care what the orientation is and our logic will remain the same currently,
03:18I'm not going to even check what is the stage orientation.
03:21What I am going to do is I'm going to go into my MC logo.
03:24And I'm going to make sure that its exposition is in the exact center of my stage.
03:28So it's going to be stage with divide by two.
03:32Now because our registration point of our logo is exactly in the middle,
03:35we don't have to worry about the configuring the size of the logo because we're always repositioning it based on the center.
03:41Because when we created the movie clip, we made sure that it was in the center point.
03:45It's pivot point.
03:47So we don't have to worry about anything beyond that, which makes it really easy.
03:50And again, we're going to go for our logo again and stage stage height.
03:53And we're going to make sure that it's going to be in the center as well.
03:56And that's all we really had to do to get our element to go back into the stage exactly in the center.
04:01All we have to do is make sure that we don't have any errors.
04:04So if I grab my compiling errors and read it out, it's going to tell me definition could not be found.
04:13Stage orientation.
04:14Oh, right. Because we did stage orientation event, which isn't one of the display elements.
04:18I've imported it in the wrong place.
04:20It actually is in our events stage orientation event.
04:24But by mistake, I've actually loaded it twice instead of only once.
04:27Let me just delete that one extra time that was in the wrong position.
04:30Save it, run our application one more time.
04:33And this time around, we are not getting any errors.
04:35So that's already good.
04:37I'm just going to wait for the ADL to load up again.
04:40And again, if I just once it loads up and it's ready, I'm going to set the auto orientation.
04:45And I'm going to orient my device.
04:47And we could see that it's going to stay always in the center without resizing,
04:51which is exactly what we wanted it to do.
04:54All right. So in our next video, we're going to look at how do we actually draw out our
04:59information and make sure that our ruler will be drawn out, which is going to be the next step.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!