Ending The Game
In this last core video we will focus on completing the game , giving the user feedback when the game completes, adding a basic score and enabling the user to restart the game.
Video Transcript
00:00For now, we don't really have much to do inside of our end,
00:02so I'm just going to put here a little trace
00:04that just says end.
00:05And we're not going to test it out yet.
00:06We're going to get back to seeing
00:08how we're working with the end.
00:10But what I want to do now that we have our application
00:12and it's working,
00:13what I want to do is enable us to start up the application
00:17and then restart it when the application ends.
00:20We want to add a little bit of logic
00:22and basically extract the logic that we have
00:24in our RSS complete.
00:26Basically, we want to take all of this logic
00:30and basically move it over,
00:32well, more accurately,
00:33we'd probably want to take this whole chunk
00:36and we'd want to move this whole chunk
00:37into a new separate function.
00:39So what I'm going to do,
00:40I'm just going to close on complete right here
00:42and start a new private function and call it start.
00:48And all this information would then happen
00:50when we start our application.
00:52What I'd want to do next is then make sure
00:54that my RSS complete would then call start.
00:58I'd want one more thing to do is make sure
01:00that my count is reset to zero
01:03every time we start our application,
01:04making sure that there won't be any chance
01:07that it's not zero.
01:09Okay, this is just a safe, keeping it safe.
01:12All right, so now that we're starting up our application,
01:16how about we add a little bit of logic
01:17where instead of starting immediately,
01:20we would actually have a button
01:22to let the user actually start.
01:25To do that, we could reuse our add element
01:29that we've been working with so far.
01:31So basically, instead of actually
01:33directly creating our start right here,
01:35calling our start directly here,
01:38what we could do is actually create a starter sprite.
01:43Let's call it starter, which would be a sprite.
01:46And we could create a new element.
01:48So create element.
01:51And we want to give it some sort of copy.
01:52So let's say we want to tell it start.
01:56And let's give it a unique color
01:57so it'll stand out from the rest.
01:59And let's say we'll just give this color,
02:02see what happens with that.
02:04So we know that now we created an element
02:06that's already on the stage.
02:07It's randomly somewhere on the stage,
02:08but we want to listen to whenever
02:14the user clicks on it, right?
02:16So let's do that.
02:23So on start, what would on start have?
02:27It's quite simple, right?
02:28Private function on start.
02:31We would have mouse event.
02:36What would on start do?
02:37On start, first of all, before we do anything,
02:39we want to make sure that we remove the event listener
02:41and we remove the child.
02:42So to do that, we could just directly just type target,
02:47remove, and we would pass it in the same values
02:57that we passed into our add event listener.
03:00Next, we want to remove it from the stage.
03:02So we're going to remove child.
03:08We want to make sure that Flash knows
03:09that it's a visual object.
03:10So we're going to make sure that it knows it's a sprite,
03:12using that as sprite.
03:15And last part is to actually start up our application.
03:19So if we run our application one more time,
03:21now we should find, oh, let's see what is our error here.
03:25We see the starter.
03:28OK, so one is I have some sort of spelling error, right?
03:31So starter here is right, but here it's not right.
03:36And let's run it one more time.
03:38And here we go.
03:39So we have our start button, and as soon as we click on it,
03:42our game will begin.
03:43Perfect.
03:44So now that we have a start, now we
03:47could actually restart, right?
03:48How would we restart?
03:49So if we go back to our end function, what functionality
03:53would we put in our end function to actually make
03:56our application restart again?
03:58It's quite easy, right?
03:59Because right now, we already have a method here.
04:03We already have our logic.
04:04We could basically just grab the same logic
04:08and just paste it directly into our end, right?
04:10And that should restart our game.
04:13Only problem is, even though that would work,
04:16only problem is that it's kind of boring, right?
04:18Because when we finished, completed our game,
04:20but there's no scoring involved, and there's
04:22nothing to let us know how good or how bad we were.
04:26So let's add a little bit more logic into our application.
04:30Let's say that to get a good score,
04:35based on the amount of times we click on the button,
04:37that's how our score will be configured, OK?
04:40And maybe we add inside also a time variable.
04:42But let's start from doing it that way.
04:44First, let's see, how could we make a scoring system that
04:47would basically be involved with how many items
04:51were in total?
04:52So let's see how we would do that.
04:54First of all, let's create a private variable here.
04:59That would basically be our count,
05:00but it would be a number that doesn't change.
05:02So it will be our total count, let's say.
05:10Which would be an integer as well.
05:12And we're going to create one more var here,
05:13and we'll call it Total Clicks, which
05:19would be an integer as well.
05:21So in our Total Clicks, we'd want to make sure.
05:23First of all, we want to reset it to zero.
05:25And we actually want to reset everything to zero
05:27besides the total counts, right?
05:28So let's set that.
05:30So Total Click, we want to go into our start function.
05:33And we want to make sure that our Total Click
05:37equals zero when our application begins, right?
05:41And we want to make sure that our total count will
05:46equal the actual amount of count that exists.
05:49And that would be the value of count
05:51after all the loops are completed, right?
05:54We are outside of our function.
05:56And we just want to set that our total count will
05:59equal the amount of counts.
06:01So now we know how many clicks our application,
06:05an ideal scenario, the perfect gamer would only
06:08have that amount of clicks.
06:10Now we just have to make sure that we know how many clicks
06:13the user has, or how many times they drop an element.
06:17So if we go into our onDrop, we could just
06:20add here one more item.
06:22So Total Clicks plus 1.
06:24OK, so we made sure right here that we
06:27counted every single time we dropped an element.
06:32So in our endgame, we could change the copy, right?
06:34We could first get the amount of times the user clicked,
06:39the amount of items that are on the stage.
06:41So the perfect scenario would be that the value would be equal.
06:46Or in other words, if our Total Clicks would equal the,
06:53what was it, let's see, would equal our total count.
06:58So if our total count, total click,
07:00exactly equals the same, that's like a perfect score.
07:03That is like the definition of a perfect score.
07:06Now let's say we want to give a range here
07:08where that would be the highest possible score.
07:13Let's say that would be 100.
07:15But we want to make sure that as users add more clicks
07:19onto their application, their score becomes lower.
07:23Does that make sense?
07:24OK, so how would we do that?
07:26What logic could we put inside here that would somehow
07:29take the total amount and the total actual clicks
07:34and basically punish users that click more?
07:39So first of all, if we would take our total clicks
07:41and subtract from the total count,
07:44then we would get back only the amount of extra clicks
07:48that the user had.
07:49So this would give us all of the extras and only the extras.
07:54So we take the extras and those extra clicks,
07:58let's say for every extra click the user has,
08:01we're going to give them a negative point.
08:06So we're giving the user the option of,
08:08or we're giving the user to give endlessly, well,
08:11until they hit 100.
08:12So let's actually do this this way.
08:16So we're going to take that value, which
08:18is the extra clicks the user had.
08:20We're going to duplicate it by 5.
08:22So basically, we're going to take 100,
08:25subtract that value of whatever comes out there.
08:29So if the user clicked 5 times more than the ideal scenario,
08:33then 25 points will be removed from their perfect score.
08:37And now we just have to make sure
08:39that if the user clicks a lot of times underneath the amount,
08:42then we will give them 0, because 0 is not really
08:46an option, right?
08:47So we're going to use math.min.
08:50We're going to take that value, oh, actually not min.
08:53We want the maximum value of, or this value, or 0.
08:58So if that value is under 0, we're going to get 0 back.
09:01So now that we got our value, which is our score,
09:04the score could be between 0 to 100.
09:08And 100 is the most perfect score ever
09:10that you would always want to get.
09:13Now that we have that, we could change our logic
09:15in our create element, and we could say something like this.
09:19Your score is, we could drop a line,
09:30make it a little bit pretty.
09:32We're going to add our score in in just a second.
09:35So here we're going to put here our score.
09:44And let's revisit our score.
09:45Make sure that we create our score variable.
09:48So we'll just create here a new variable, which
09:50is an integer as a score.
09:52Now I just realized that we have a little bit of a problem
09:54here.
09:55We probably added a bit too much brackets.
09:58And it's kind of confusing.
09:59Let's just try to double check and make sure
10:01that we're actually capturing everything inside of our max.
10:06So if I highlight everything, you'll see that our max
10:10actually has one bracket too much.
10:14The reason why I've noticed that is
10:15because all of the first value and second value
10:19are put into position one because of the bracket.
10:22So if I would do that, we would probably
10:23get always the score of 0.
10:27Let me undo that.
10:28And if you run our application one more time now,
10:31if we run our application, then we'll
10:32get those values between those different scores.
10:36So if we run our application now,
10:37we should be getting now our game.
10:40Now it's going to take a bit of a while to get to then.
10:42So we're just going to start up and we'll skip.
10:47So we're a bit closer to the end
10:48and we have only two values left.
10:50I had probably one or two mistakes.
10:52Now let's see what score we're going to get.
10:54And here we go.
10:55So I had four mistakes.
10:57As we can see, my four mistakes cost me
10:59and my score now is 80.
11:02And if I would play again, if I would click on the play again
11:05button, the game would restart again.
11:07We have a very nice endless loop.
11:10And there we go.
11:10We have our complete Twitter language game,
11:14which is great because every time you'll go back to this game
11:16as I add more values, the game is
11:18going to get more and more complicated
11:20because there are going to be more and more assets
11:22on the stage, which is great practice to learn
11:25a few words in French.
