Fill It!
isn't this the point of this game? let's fill out the new safe area. we take advantage of Vectors in this video(available from flash player 10 ) if you are working with flash 9 just use an Array instead.
Video Transcript
00:00Now when it hits that point that it stops this time for us to actually clean up, right?
00:04It's time for us to take this area and turn it to be blue
00:08Now because the way we've done it so far we did we don't really have a trail
00:12We don't have a way to know what is that next dots?
00:14We actually need to create maybe some sort of extra information. We have to create an extra array
00:20We have to get and we're going to create an extra array array
00:24So let's do that
00:25We need to create an extra the reason why we need it and let me just show this before we actually do it
00:28The reason why we need to create an extra array is as we travel along every time we change a shape every time you change a direction
00:35We want to save that change a direction so we could recreate it when the application is done
00:40So we want to save this point this point this point this point this point this point and this point we want to keep all
00:45The points so then when we're done moving and we're in the same zone
00:48We could then redraw this clear our fill area and redraw it in blue with the fill color that we wanted it to
00:54Be which is blue
00:56Alright, so let's try this out. Let's check to see if we can make this happen so to do that
01:00I'm going to create a new array and really what I really would probably want to do we probably want to create a new vector
01:06So let's see how we create a new vector. We haven't really worked with vectors before at least not maybe we did
01:11But in case we didn't let's look how we work with vectors and vectors exist only from flash player 10 and up
01:17Var and I'm going to create a new vector, so I'm going to create it. I'm going to call it
01:22the points
01:24And it's going to be a vector and the vector is going to accept only the type of point
01:31And here we go we created a new vector and a vector is like an array
01:35It's only an array that says these are only only this type of things could access it actually need a dot here
01:41Alright, so let's create a new vector, so I'm going to go to my point
01:44I'm going to give this new V points in the create dot itself as well
01:49Because we're going to need it with the dot itself so V points is going to be a new
01:54vector
02:00point
02:03Looks very weird. I admit it looks very weird because it was added into the language later on
02:07But a new vector is like a new array
02:09It only says a new array that could only store points and it helps for processing me
02:14It also helps them make sure that if you add something into the V points
02:17That's not allowed will throw an error and it also reduces your footprint
02:23Which is always a good thing so let's just run the application and make sure that we didn't have any errors
02:27So we could see that everything is still working which is fantastic
02:30And now we can take our V points and go into our change direction and each time we change a direction
02:35We want to put in the part of the current dot we could we want to put this current dot?
02:40XY and let me just show this to you again, so because this is good. This is the perfect example
02:45So this is only called when we change a direction, so it's called now. It's called now
02:50It's called whenever we change a direction, so if I change the direction again
02:53It will be called again again again, and this is exactly the dots that we want
02:58All right, so what's left for us? We just want to store this information instead of just reading it out so to do that
03:04I'm going to
03:05Create here
03:06I'm going to go to my V points dot push, and I'm going to push into it a new dot a
03:12New point actually and that point is going to store that dot X dot Y
03:19All right, so now we have our V points
03:22We have our V points
03:24It's adding in each time we're changing the direction and the last thing that's left for us is whenever the safe is done whenever we're ready
03:30to reset
03:31It's time for us to actually call and actually reset and draw the element so
03:36Let's call this maybe add safe
03:40things
03:42Add safe to bg
03:45All right add safe to bg that makes sense that name implies that we're going to be adding the safe things into the background
03:51So it's going to be a private function
03:54add safe
03:57to bg
04:01All right, so what do we do in our add safe to be yeah?
04:03And I want to take this pdier and move it because you know that makes sense that whenever we add the safe to bg
04:08We're resetting everything
04:10All right, so that's good kind of reduce the the size of this function
04:16Oh, it's oh so that's always a good thing now as if to be g what you want to do
04:20We actually what we want to do is we want to draw into this into the bg all the elements all the data
04:25That was just added into our line to now if you scroll up and just look at the create bg
04:30We'll see that create frame. How did it work?
04:32We first set a style a line style then we set a fill color then we drew our rectangle
04:37I want to just grab this for a second
04:39I'm just going to grab this whole copy everything here because we're going to be using most of it and then tweaking it
04:45So we're going to let's paste it right here
04:47So first of all we don't want to do a line style
04:49But we do want to use this fill color as our fill so we're not going to have a line style
04:52But we're going to have a fill which is in the same color of that blue that we had before
04:56We're not going to draw a rectangle instead of that. We're going to be looping in inside of our vector
05:01So I'm going to have your four bar. Let's make it it nice and official so I'm going to have your bar
05:07I and to be honest the direction doesn't matter so we're going to add and do even something better, so
05:13Because we don't care. What's first. What's last we could go to bar for bar P
05:18Point for each of our actually for each
05:21for each bar P point in
05:26Whoops in our V points. Let's where is V points and
05:32V points here we go
05:35So what are we saying here for each point and V points? We're going to run through all the points for each of the points
05:41What do we want to do?
05:43We want to draw a line to it line to now even though we don't have a line color
05:49We're using the fill and we're going to be with this begin file create a shape
05:53So I'm going to I'm going to go to my I'm going to go to my bg graphics
05:57And I'm going to ask for the line to and this line to I'm going to what position don't want to take it
06:02I want to take it to P x P y which is that point x point y?
06:07All right
06:09now next step after that is
06:11We want we could delete this draw rectangle and now it we're ending a fill
06:16So this in theory should then create this beautiful
06:20This beautiful whatever shape we've just created it should fill it up
06:23Oh, so and the last step that's left and also by the way we're not drawing the whole line right
06:28We're missing that point from one point to the next point
06:30But flash should be smart enough to be able to fill it out
06:33But not always so let's first see what's wrong with what we just did and then we'll fix it now
06:39Let's ignore the new bitmap just for a second here
06:42And we'll talk about it just a second because we want to first sort this all the issues out with the graphic line
06:46So let's see what our error is semicolon missing. So let's see where what's wrong here. We're missing here a closing a
06:53closing bracket a round bracket so that should sort that out and let's see what happens if I start drawing
07:01So firstly we already see that we have a problem, right?
07:05Our first problem is is that because we don't have enough elements
07:09We just have one thing in our points because we have only one item the first time we click
07:14We had only one element in our vector
07:18Then our vector didn't work. We created a shape that just didn't make any sense
07:23Now what we want to do oh, so we want to make sure that we're moving our dot
07:28We want to make sure that we're moving our dots
07:30We're not starting from zero zero when we're doing these lines, too. So oh, it's not here. It's an hour
07:36At safety G. So when we're doing line two we're starting from that zero zero position
07:41So our first thing what even first time we click we have the zero zero then the dot then it tries to do a fill
07:46And this what happens. This is what happens
07:48So let's sort this out to sort this out first of all after I do the begin fill
07:53What I want to do is I want to actually move my element or you we could do is even before we do the begin fill
07:58Because we're before we're drawing so maybe that even would make it easier to comprehend. So I'm going to go to my graphics
08:05Dot move to I want to move my line before I even start drawing
08:09Where do I want to move it to?
08:11I want to move it into the first position in the vector and to do that
08:15We know the name of our vector is V points and we want to go to position zero dot X
08:20position zero dot Y now that should help sort out the issue of
08:25Drawing it from the bottom from the top here. We still have one more issue
08:29So first of all, it's still drawing even though there's no need for it, right?
08:33It's drawing even though we're moving only in the blue area
08:36Now because we're moving in the blue area
08:38We know that only way that we'll be moving from blue to blue if there's only one thing inside of our
08:45Inside inside of our vector so we saw the issue of drawing from the wrong position
08:50But we didn't solve the issue of the fact that we're still only in that area that our vector only has one point
08:57So let's fix this issue
08:58So first of all, I'm gonna have to go all the way up and I'm gonna have to reset resetting the position is fine
09:04I'm gonna have to ask if my V points length is greater than one
09:10then do all this great stuff and
09:14Actually, also this bitmap stuff that we're going to look into in a second if it's not what do we want to do?
09:20Well, even if it is even if it's not what you want to do we want to clear our V points
09:25So let's clear our V points
09:27Do we have a clear function?
09:30No as working with our way what we're gonna have to do is we're gonna have to or create a new element
09:35Or we could actually
09:38we could actually just
09:41We could actually what could we do?
09:45Let's see what we're gonna do we could actually just create a new element which was probably gonna be easier than dealing with anything else
09:50So we're just gonna let we're just gonna create a new one on top of the one that was before and discard
09:54What was there before so to do that again? We're gonna create a new vector
09:59For a new vector point beautiful it automatically filled it to me that's really really cool for flash CS 5.5 and
10:05There we go, so I just cleared everything I didn't care about what was there before there's nothing there that I care about
10:10We're just resetting it recreating our point now
10:14This is not the most clean way of doing things right we in theory would probably would want to do something smarter
10:19Do you guys want to do something smarter? All right? Let's do something smarter now to do something smarter instead of just taking these points out
10:26what we could do is each time is
10:29Remove the element from the out from the point or we could go into our vector and search for properties
10:34So we could go into our Google s3. Let's search for a cleaner option Google s3 vector
10:41We're gonna look for a few properties. It looks for let's look for a property that enables us to just
10:47Just get rid of things. Let's find the best one that will be useful
10:51So pop will take only one thing out, which is not good for us
10:55slice
10:57Or turns a new array sort splice adds elements and removes elements from the vector
11:03All right, there we go. Spice could be a great a great work
11:07Let's just make sure that splice actually changes our element our original elements itself
11:12From the vector this method modifies vector without making a copy beautiful. That's exactly what we want
11:17Fantastic. So what we want to do is we want to go to our V points
11:21dot slice
11:23What do you want to slice you want to slice from position zero?
11:26How much do we want to splice we want to splice to the length of it V points?
11:31that length
11:33Whoopsie
11:36All right, let's test it out. So first of all, we're going to put your trace them before which is going to be the V points
11:44And we're going to do one that's after just to make sure that really our vector is now nice and clean and new and we
11:49Didn't have to create a new vector
11:51So let's do as soon as I click
11:55The before has an element the after has an element as well
12:00Let's see if the same thing happens before now has two now has
12:06Look and our logic did not work. So we want to make sure that maybe after we V points we slice
12:12We're gonna have to actually let's look into our search here. Oh
12:17Splice I use I had to splice instead of slice season one. So that was my issue. Let's try this one more time
12:24There we go
12:26beautiful before after
12:28Now if I do a few shapes here, that's fine before it will give us after it will be deleted now
12:34We could see also that we still have one more problem. Okay, so we filled our element that looks good
12:38But we have here this weird triangle. Why do we have this triangle?
12:42Now if you look at our before and after elements, we'll see that our lines are starting. We are first dot is probably going to be
12:49But our first dot is going to be this let's look look look
12:53let's try to find it because I don't really know really why aren't we are having an issue right now and
13:00What I'm all for for just tracing out all the points that we're doing to see what we're missing
13:05So let me let's see for let's let's start with just tracing out
13:10Whoopsie, let's just
13:13Trace out
13:15the point zero because it's the first thing that we're moving into and
13:20Then we'll just in the trace right here. Oh, so all these
13:23points
13:25And we could probably comment this out because we know that this is working now
13:29Well, we could even delete it. We know that it's working or you know what in case you guys need it
13:33I'm gonna just comment it out. All right, so let's check this one more time
13:38So if I draw a nice little shape here a few little directions and it
13:43Alright, so let's look at our values and try to see what we are doing wrong here. So we're starting from 155
13:49then we're lying we're drawing a line to 155 and
13:54Then we're moving on and on and on and on until we get to 129 4 5 6
13:59And the point that we're missing is we prior missing instead of using the moving to the first out
14:04Let's move to the last dot. So they move to the last I'm just going to go to the
14:09v points
14:11Length minus 1 and
14:16Same for that point
14:18So that way we're getting this nice our beginning will be the end as well
14:21Our goal is at the beginning and end will be the same value. So let's try this one more time
14:28And let's make sure that we're doing the right trace here. All right, let's give it a try
14:33All right. So first of all as soon as we start
14:36We're gonna turn around come and go and go back right down again
14:40And let's look at the best first of all, our element is still not doing what we wanted to do
14:44But let's see if our values are right. So we got there 117 till 5
14:49458
14:51117 till 458. So we're actually doing quite well, but something here is just not
14:59working correctly
15:01Let's try to sort this out
15:06Hmm
15:09Even though we're closing a second
15:11So let's try first thing just creating a simple shape just a regular square just to test this out and see if a square works
15:17Because I'm stuck right now because we have your a bug. I don't really know what it is yet
15:21Okay, so we a good thing that happened now that created a more simple shape is now we're seeing that we're we're seeing that
15:28Our issue is consistent. We're consistently making the same problem
15:33Now we can see that here we have 1 2 3 4 5 6 7
15:407 points even though all we really wanted to have is 1 2 3 4
15:44So why are we getting 7 which is a really really good question. So let's let's try to sort this out. Let's try to think so
15:52When are we adding points we're probably having an issue when we're adding the point so first of all 117 this looks fine
15:59It looks fine as well
16:02It looks like we have your more points than we actually have
16:06So let's see. Let's see what's going on your first of all
16:08what I want to do is I want to make sure that when I'm adding that line to
16:13I'm also doing when I'm adding a line to that's fine
16:17Excuse me
16:19Let's see we are looking for that point. So add BG. This is we're looking a step before that's on enter
16:28Or what I'm actually looking is when I'm adding that V point
16:31So I'm just gonna search for that for a second where I'm doing that push
16:36Where I'm doing that push
16:38So I'm gonna find a replace looking for that push delete that
16:43Alright, so when we change a direction
16:46We get that new point that dot
16:49That looks about right. When do we call the change direction?
16:53Let's just make sure that it only gets called when a direction is changed and it's only getting called on a direction has changed. Oh
17:05Alright, alright our problem is is that when we stop the last time it's not really changing a direction is it?
17:11It's just stopping. So let's find the point where it's stopping. The point where it's stopping is right here is
17:16is
17:18Safe when we stop so when we stop before we even change. Well, we could change the direction. That's fine
17:24We have to add the current position
17:27so maybe even instead of the move to we could do the move to the actually to the position where we are currently and
17:33The position where we are currently, I'm just going to delete that the position where we are currently would be our dot dot x dot y
17:41Now what I could do also just to keep things safe is also after we move to that position where we started from
17:46We want to fill we want to line two
17:50We want to line two after all of it is there just before the fill is over
17:54We want to line to that same dot where we are right now
17:57Let me just show you that it works and then explain why what it would actually right happen here
18:04So first of all, we're going here traveling around now
18:08We could do as many twirls as we want and here we go
18:11And now our element will fill up and the reason is is because the last time that we turned the last time we returned previously
18:19Was the last time we had so we had all these dots
18:21We had this dot this dot this one this one this one this one this one this one this one
18:24But we were missing this dot because here we didn't have a change of direction because here was the place where we stopped
18:31Fantastic, so we've got another issue here sorted out
