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

Creating Shapes and filling them with Color

At this stage we got the basics of working with the drawing API at this stage we will start to draw a full shape and fill it with color. using beginFill and endFill.

Video Transcript

00:01All right, so we're starting afresh.
00:04Let's go back into that main again,
00:06create a new main for us.
00:08We've got ourselves a nice new main
00:11and let's see what can we do next.
00:14All right, so next in turn,
00:15let's start creating full shapes.
00:17Create a full shape that actually has also fill.
00:19So we've seen how to create lines.
00:21Let's just set that line up again,
00:22even though we don't have to.
00:24If we don't have to, so actually let's not.
00:26And maybe we'll add it a little bit later on.
00:28So I'm gonna go to this graphics
00:30and in my graphics library,
00:32oh, and first of all, we're not getting the hints
00:33because we're not saving the file.
00:35So let's save that file out.
00:37Again, I'm gonna go down into,
00:38oh, we're in fills already.
00:40We're gonna save it as main
00:41and let's try this one more time,
00:43this dot graphics dot begin fill.
00:50We give it a color.
00:51So let's say we're gonna give it a color of,
00:54let's say that yellow again,
00:58because it was so nice.
01:00And let's give it an alpha value of 75, 0.75,
01:04which basically means 75%.
01:07We could have left it at one,
01:08but we chose to give it a color.
01:10Now it's time for us to start drawing.
01:12So I'm gonna take a graphic,
01:14gonna take it and we're gonna move our line
01:16somewhere in our stage.
01:17So let's say I'm gonna move it to 300, 300,
01:19just randomly I'm picking that.
01:22And I'm gonna try to create
01:23a very distorted triangle of some sort.
01:26I'm gonna go back into our graphics
01:27and I'm gonna line two
01:29and I'm gonna go maybe to 300, 400
01:35and this dot graphics line two,
01:40400, 400 and one more time.
01:45Let's go back to our source.
01:47So we completed a full shape
01:49and we wanna last thing that we wanna do
01:51is we wanna end our fill.
01:52To do that, I'm gonna just type this graphics and fill
01:56and that's all I really needed to do that easy.
01:59If you run our application now,
02:01we're gonna see that we have your triangle
02:02a little bit too low.
02:04So obviously because we hard coded everything,
02:06it's gonna be harder for us to actually solve this,
02:08but I wanna show you actually something really nice
02:10about solving an issue of this sort.
02:12First of all, better off for us usually
02:15not to actually draw directly into our main class.
02:18So what we could do is we create a new variable.
02:20I'm gonna call this variable shape or SHP
02:24and I'm gonna put inside of it a type of shape.
02:26And a shape is basically a very primitive visual element
02:30that more or less only could create graphics in it.
02:32You could only really draw graphics into a shape.
02:35So to do that, I'm gonna come into my,
02:37I'm in my main now and I'm gonna create a new shape
02:40and then I'm just gonna grab this shape,
02:43replace that this in my scenario,
02:45I could have done a search and replaced very easily.
02:49And now that I have this all sorted out,
02:51I could then add that child onto the stage.
02:58And if I wanted to, now that my child
03:00is at zero, zero, so right,
03:01it's gonna look exactly the same thing.
03:04But now it's above our elements, by the way,
03:06because now we're not drawing on the movie clip,
03:09we're actually drawing on top of it
03:11because we have a new movie clip inside.
03:13So it got into higher level
03:15because movie clips have a higher level than drawings.
03:17And now that we have a new element,
03:19a new graphical element, it's sitting on top of the stack.
03:23But we wanna move it in.
03:24So to move it in, I can now,
03:25because I know that I wanna move it in just a little bit,
03:28so I could go to that X position,
03:30and maybe I wanna subtract 100 out of that,
03:33and I wanna go to the Y position,
03:35and I wanna subtract out of that,
03:37also maybe let's say 50, 59, just for fun, save that.
03:43And our graphic now is gonna move in time.
03:45Now, obviously, when you're originally planning,
03:48you wouldn't wanna create a shape in this way, right?
03:50Instead of that, you might wanna create a shape
03:52a little bit more efficiently by already knowing in advance
03:56that you're gonna be moving it around.
03:57So I wanna show you what that actually means.
03:59So I wanna create now a shape,
04:02and when I create this shape,
04:03what I wanna do is I wanna actually start it off
04:07as if it's at the zero, zero position,
04:09and now he's gonna be there.
04:11That way, I could then move it around,
04:13and it's gonna be much easier for me
04:14to figure out where the shape is.
04:16Because in arson, originally, it started at 300, 300.
04:19As soon as we moved it,
04:20now we have to start calculating
04:21to figure out where is the actual shape,
04:23because there's a lot of white space.
04:25So I'm gonna start it over,
04:27and we're gonna do it quickly, create a square now.
04:30Obviously, it's gonna work in the exact same way.
04:33So let's try doing this really, really quickly.
04:36So this time around, I'm gonna just pick a different color.
04:40I'm not gonna even set that because it's default to be one.
04:43And I wanna show you a little trick.
04:44So let me show you that trick.
04:45You could type width, and then open a round bracket.
04:50Say what you wanna use.
04:51It could be any object.
04:58And now, everything you put inside of that width,
05:03between that opening curly bracket
05:05and the closing curly bracket,
05:07will automatically get appended to our shape.graphics.
05:11So begin fill now, because we know already by heart,
05:13we don't wanna move anything.
05:14We wanna start drawing immediately,
05:16which is gonna be line two.
05:18So let's say we wanna take it to 100, 100.
05:21And then we wanna line two, 100, 200.
05:25And then line two, 200, 200.
05:30And then line two.
05:35Oh, actually, I'm completely messing this up.
05:37So we were at zero, zero.
05:38So we're gonna go to zero, 100, then 100, 100.
05:42And then 100, zero.
05:46And last but not least, going back all the way
05:48to the beginning, which would be zero, zero.
05:51That would create our full square.
05:52All that's left for us is just do an end fill.
05:57And we could have done all that
05:58without actually typing in,
06:00without actually typing in,
06:02all that graphic graphics, all these times.
06:04It's a nice shortcut.
06:05It's useful sometimes to use that,
06:07because it's gonna cut down the amount of code
06:09that you have to type.
06:09So if we run this one more time,
06:11we're gonna get our square right on the top here,
06:12but we really want it to be on the center of the screen.
06:15So let's make that happen.
06:16So now that we have it on the center of the screen,
06:18I could screen, not scream.
06:20I could go to shape X and I could tell the shape this.
06:24I wanna take you and I wanna put you in the center.
06:26So stage, stage with, we've met stage with many times.
06:30I'm gonna divide it by two.
06:32And if I really wanna be accurate,
06:33which I really wanna be accurate,
06:34because I'm that, you know,
06:35you'll see that my square is really big.
06:37So although it's starting in the middle,
06:40it's shifting to the right
06:41because it's growing towards the right,
06:43because we started from zero
06:44and we grew it to the right by a hundred.
06:47So what we could do is we could subtract the value
06:49that we get back by the shapes with itself
06:52divided by two.
06:54That would make sure that it's exactly in the center.
06:57And we could repeat this
06:58and instead of doing it for the X only,
07:00doing it for the Y as well.
07:02So now when we do it for the Y,
07:03we just wanna make sure that we use our height
07:06instead of our width,
07:09both for the stage and for our box itself,
07:13for our shape itself.
07:14And if we run this one more time,
07:15we're gonna see that our circle is exactly in the middle,
07:18exactly where we would hope it would be.
07:20And it kind of shows us that my aiming with my rabbit
07:23wasn't that really great
07:24because it's not really in the center, but that's fine.
07:26I did that in purpose because this strip here
07:28makes it look okay when it's offset.
07:33All right, so we've learned another topic.
07:34So we covered lines, we covered fills.
07:37Now, obviously, if you wanted to,
07:39you could have auto add here, added to here a line.
07:42So if you really wanted to,
07:43you could have added to that shape,
07:46shape, graphics, line style.
07:50Actually, I didn't remember the lines,
07:52I'll always forget its name.
07:53Let's make the line style thickness four
07:55and leave the default colors.
07:57Now, if we did that, we're gonna get a nice big,
08:00exactly.
08:01All right, so that's all you really need to know about lines.
08:04Let's move it on.

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!