BitmapData
Using BitmapData we solve the issue of figuring out how to know if our point is out/in the safe zone.
Video Transcript
00:00All right, so we have our dot so far now.
00:02Our only problem right now is that there's no end to our dot.
00:06So as soon as our element starts walking,
00:08as soon as it starts moving, there's no end to it.
00:11It has no way to reach a destination.
00:13So actually, as it continues going,
00:15it could even go out of the stage,
00:17out of the boundaries of the stage.
00:19And that's not too good for us.
00:20All right?
00:21So let's sort this out.
00:24Before we continue, I want to make sure
00:25that I change, because the move it just doesn't feel right.
00:28Let's change the word move it to actually,
00:30let's change the function a move it.
00:32Let's change that name to change direction, or change deer.
00:35And the reason is, is because the move it
00:37doesn't really move it, it starts that cycle
00:40of moving a direction of the element.
00:42So I'm just going to change that move,
00:44move it to be change deer,
00:46which I feel is more descriptive,
00:48and maybe will be easier.
00:49So I'm going to replace all just that easily and quickly.
00:52I could change them all.
00:53And let's go into our change deer,
00:55or historically called move it,
00:57and start changing it a bit.
00:59Now, before we change it,
01:01we're going to get back to changing it.
01:02Let's go into our enter frame,
01:04and see how do we actually figure out when is it safe,
01:07or when is it right for us to actually stop the movement.
01:11And if we look at our application just one more time,
01:13we'll see that.
01:14I'm going to start by using that we have this blue,
01:16and I want to test to see, is my dot on the blue area?
01:19If it's on the blue area,
01:21that means that we're safe.
01:23All right?
01:23So to make sure that I could do that,
01:24I have to add a few,
01:26a little bit more logic into our application.
01:29And we're going to have to go back into our create frame,
01:31and create a few more little things.
01:33Now, the first thing is I need to create a new setting
01:35that's called a bitmap data.
01:37Bitmap data will convert our vectors,
01:39because everything we did so far is vectors,
01:42into a bitmap data that I could access.
01:44So to do that,
01:45all I have to do is go into my private var,
01:47create a new private var that is,
01:49and I'm going to call that private var bmd
01:51for bitmap data.
01:57All right, so I have a bitmap data.
01:59Now, I'm never going to use it visually on the stage,
02:01but what I do want to do,
02:03as soon as I create my new fill,
02:05as soon as I create my new background,
02:07I want to make sure that I'm going to create also
02:09my new bitmap data.
02:11And that bitmap data is going to store all the bitmap
02:14information that's inside of my stage right now,
02:17and currently actually inside of my fill.
02:20To do that, I'm going to create a new bitmap data.
02:25And my bitmap data is going to take some information.
02:27First of all,
02:28I want this bitmap data to be the size of the stage,
02:33and I want it to be the size or the height
02:35of our stage as well.
02:37Last thing left is do we want it to be transparent?
02:40No, it's not going to be transparent, all right?
02:44Now, next step is I actually want to go to my bitmap data.
02:46Now that I have a bitmap data,
02:48now that it has a size,
02:49I want to draw into it something.
02:51What do I want to draw into it?
02:53I want to draw into it my background, all right?
02:56Now that I drew my bitmap data
02:58and I drew into it the background,
03:00I could start now manipulating the data
03:02of the bitmap data.
03:03I could actually access pixel data.
03:05To do that, it's quite easy.
03:06So even right after I created my data,
03:08I could go to my BMD, my bitmap data,
03:11that was created.
03:12It's not visual for me, right?
03:14Because it's not on the stage,
03:15it's just data that I'm using for my visual elements.
03:17And I could say get pixel,
03:20and what pixel?
03:21I want to get the pixel at the top.
03:23Let's see what color that pixel is.
03:24And just by asking to get that pixel,
03:26if I run the application one more time,
03:28and let me just fix any errors that I have.
03:30Syntex are expecting right per am before semicolon, right?
03:32So we're missing one closing bracket.
03:35And we could run the application one more time.
03:36We'll see that we just got the color back,
03:38which is the color of our blue.
03:42And how could I could prove that
03:43just by adding here a comma?
03:45And let's just ask for that fill color,
03:51which is the color of our frame.
03:54And here we go, it's the exact same color.
03:56Now you might be confused and you might be saying,
03:57but Ben, this is the color,
03:58why is it coming back in a different color?
04:00I don't want to go yet,
04:01and if you know the history of color theory,
04:04of number theories, where Booleans are zero, one,
04:08and most times when we work in math,
04:10we're using the 10 digit numbers.
04:13Hex values are based on 16 digits.
04:15That means that if by us 10 would be,
04:20or the number 16 would be one, six,
04:22the number 16 in hex values would be F.
04:26Because F alone would be 16,
04:27because the digits are from zero through F,
04:29which gives it 16 digits.
04:31We're not gonna get deep into this,
04:32but by setting this value,
04:33this number and this number are the exact same thing.
04:36They're translated into the computer
04:38in the exact same way.
04:40But we're not gonna dig too deep into this.
04:42Now that we have our pixel data,
04:44now that we could gain access to that pixel data,
04:47we could start being a little bit smarter
04:49by asking when we move around,
04:52asking are we actually in that safe zone or not?
04:55Are we safe already or not?
04:57All right, so let's add this logic into it.
05:00So first I wanna create a new private function.
05:04And because we already know what getters and setters are
05:06from the developer,
05:08from object-oriented program in ActionScript 3,
05:10let's start creating a new getter.
05:12We're gonna start asking for a get is safe,
05:17which is going to return a boolean.
05:21And this is going to do a simple logic.
05:23It's gonna return the bmd get pixel.
05:27In the current position of our dot,
05:32and we're gonna ask if it equals to that fill color.
05:37And that's all we had to do.
05:38And now let's break this down.
05:39So what are we returning?
05:40We're returning if the value is the fill color,
05:44if it's the safe color.
05:45If it is the safe color,
05:46is safe will be true.
05:48If not, it will be false.
05:49So let's test this out.
05:50So I'm just gonna put here in,
05:51I'm just gonna put here a trace for is safe.
05:56And again, a getter makes my function.
05:58It makes my function be,
06:00it enables me to use a function
06:01in a way that makes it look like it's not a function,
06:03like it's a property, is safe.
06:06And if I run my application one more time
06:08and I start moving,
06:09I'll see that it's not safe, it's not safe,
06:11it's not safe, it's not safe.
06:13It is safe, it is safe, not safe again.
06:15The reason is because our animation did not stop
06:16and it continued moving on.
06:18If we just scroll up, we'll find it at some point.
06:20Here we go, it's true right here.
06:22These two pixels, everything was fine.
06:24So let's clear that.
06:26And let's add the logic.
06:27So we know that there is a point where we are safe.
06:29So if we are safe, what do we wanna do
06:30in the case that we are safe?
06:33Let's start by removing the event.
06:35So in the beginning here, we added the event listener,
06:37let's do the exact same thing,
06:39but the opposite, we're gonna remove the event listener.
06:42Now let's test it one more time.
06:44So if I go up, up, I go up.
06:48If I go down, up, it stops.
06:53Up, down, up, down.
06:58All right, and this way it enables me also to walk around
07:01but traveling inside of the blue area is quite tricky, right?
07:05All right, so let's see how could we could travel
07:08and there's a lot of logic that we still need
07:10to add into the application, of course,
07:11but we already have this capability of stepping out,
07:15stepping in and it stops when it steps in,
07:17so we don't have to worry.
07:18We still have to worry about the fact
07:20that we could actually exit the stage area.
07:23So we probably need to add a little bit more logic
07:25before we actually move the dot.
07:28Fair enough.
07:29Now, we have a problem here
07:31because on one end we do wanna enable our dot
07:34to be able to travel in the space that's blue
07:36because our element has the right
07:38to travel in the space that is blue.
07:40On the other half, we don't want the user
07:42to actually exit the stage area.
07:44Now before we actually solve this issue,
07:46what I wanna do is I wanna add a change,
07:48a little change in the way that we've done things.
07:50Now, until now, whenever we change the direction,
07:55we change the direction and that started the event listener.
07:58Whenever our event listener stopped,
08:00we then change back to zero,
08:02but one thing that we're missing here
08:03is we wanna make sure that when our direction is set,
08:05whatever was before really doesn't matter anymore.
08:08So or we could just set our dot directly in here
08:10and then stop it, or on the other hand,
08:13we could add a little bit more logic
08:14in the change direction.
08:17So let's see what we'll do.
08:18So first of all, probably the easiest solution for us
08:20is in the is safe after we stop the event listener
08:22is to go to our P direction
08:26and just set our X to be zero,
08:29our P direction Y, set that to be zero
08:31because now we're not moving anymore.
08:32And that's fine, that's all we need really
08:34for making sure that we're resetting,
08:36making sure that if there's any chance,
08:38anything else starts that there won't be a duplication
08:40of data or something that's not correct.
