Creating a simple game
Creating a simple game. Yes, we will learn a couple of things in the process, but the main goal of this lecture is to inject a bit more fun and practicality into our training. By the end of this lesson, we will have a simple fun game.
Video Transcript
00:00Alright, so let's gamify this a little bit
00:03And to gamify this what I'm going to do is I want to create some sort of logic where if you click on it
00:09And it's a certain color
00:11Then it'll just get removed from the stack if you click on it in a different color
00:15Then we'll add a few more animations onto the stage. So let's do that. So let's start with the click event
00:21So I'm just gonna make sure that my item has a click event if it doesn't have one already which it does
00:26But this time around is currently it was meant to pause it. So if I
00:31Manage to click on something it would just stop in place and not move anymore
00:36And how about I go that path, but instead of just pausing it, I'll just terminate it
00:41So let me just have your in my click on my interclick
00:45We're gonna go into that function that you already created earlier and by the way speaking about those stuff
00:50There's a few properties that are new to tween max. We're not gonna go into them such as the reverse
00:56because we've seen them in action such as the pause such as the
01:01Pause all such as the update to
01:04But we're not gonna get into all these elements because we are they're easy. They're simple
01:09We want to go into the stuff that are interesting. So let's continue. So we were talking about let's first get and find out
01:16What is the actual basic?
01:20Some sort of basic property of our elements
01:22So first of all, obviously the first thing that we could do is we could go to our object and we could basically kill it
01:26we could kill it we could kill the whole thing and
01:30We could then go to our our object itself our target as a display object
01:36And we could then remove it from the stage and we could even delete it from memory
01:44Because we don't have any hard-coded reference to it
01:47It will get destroyed at some point as long as we get rid of that click event
01:51So to get rid of that click event, how about we just?
01:54Well, by the way, we don't we didn't even have to use the tween for that because we could have referenced directly to our target
02:01as display object, right?
02:07But that was not the point why I've been doing why did this you know
02:10So I just leave it the way it was and we'll just do it the other way around in the next line
02:15Okay, and the next thing I want to remove
02:17the event listener of E dot target remove event listener and
02:24Because it's the only event listener of this element and by the way a great way is to always look for that
02:30Event itself and just add the same exact properties. That's one way to deal with it
02:35another another good way of removing the events is
02:39Trying to do it dynamically. But in our case, it's not really needed because we're very very strict here
02:44We have our function. We don't have much to worry beyond that. So once we've removed the event
02:50Remove the child. That's it. It's gone. Yeah, and we won't have to worry about it any further
02:55So if I just reduce them out of items so we don't go crazy
02:59And I reduce it. So let's say 10 so it'll be easy for us to get rid of all of them
03:02I could just start clicking on the ones that are there and
03:07Slowly, but surely I'll be getting rid of all the items until there is nothing left or in
03:14Until they're too small and I just can't get to them. Ah
03:17We got two more left and if we wanted to make it really interesting
03:21We could go back here and make our text visibility instead of false
03:25We could make it true by not putting there anything and instead of that we could assign here. Let's say a value
03:32private var
03:34items
03:35left or
03:38total
03:40items
03:41It's gonna be integer. We could set it to be 50
03:45And we would just put here in our text itself. We could go to our T names
03:52Text equals
03:56items and
03:58I could just set that into my
04:01set that into my loop and
04:05All I have to do is just update my total items every time I get rid of an item and
04:11Every time I get rid of an item
04:14Which is right here. We got rid of the item. We'd want to update our information
04:18We want to subtract one from our total items before we do that and last but not least
04:24We probably would want to run it and test it
04:27So let me just test it out with let's say five items because 50 might be too many clicks for us, right?
04:32So I'll run it one more time and then we have five items and every time I managed to remove one
04:37We would have subtracted and subtraction in the items now. The only problem I have is once we reach zero items
04:43It's kind of self-defeated to continue this message, but we probably will have your a game over
04:49So let's do that and that will wrap up our conversation about tween max
04:54So before we define what copy we'll have here. I'll ask if total items
04:59Equals or if total items really because that means that there are total items, right?
05:05Because it's one or less than that. If not, we could set the value. Hey, let's let's create a turn
05:10ternary operator by just setting here a question mark instead
05:14I love it and it could be if we have items that would be the value if not, we'll put here
05:21game over
05:23Let's give it a go
05:24So we have your five items four
05:28Three two
05:31Now the the the thing that makes this game really easy is that they always go to the same spot
05:37So this game isn't that complicated because of them
05:40So before we wrap up we really want to probably come up with some sort of method that each round
05:46Would maybe take us to a different point a different position
05:50Before we get to that game over to make this game a little bit more challenging
05:55So to make this game more challenging two things
05:58We probably want to do one we probably wouldn't want to take our objects always to the same spot
06:03Which that's what we're doing right now
06:06But instead of that we would not find that tween. Where is our tween?
06:11We created all those weird properties. So instead of that we would duplicate this by
06:17math random
06:20See how math random is one of your best friends
06:24Anything you want to make it a little bit more less generic
06:27You just use that math random it it creates some stuff that makes it
06:32This on its own is gonna make this way harder. It's gonna be a lot harder
06:36Our only problem is is that it's always going back to the same spot and it's always going back to the same position
06:41So that's that's one of our problems
06:43now so what we want to do is we probably want to go back to that method that we had on our repeat with our
06:48Counter and try to figure out a way to change where it's actually going to so to do that
06:54The next thing I want to do is I'm gonna first of all
06:57You know because we're in 5v we could delete all this stuff and I'm just gonna go into those
07:02properties and add more properties so TMV dot on
07:08Repeat and then I could send that function in so on repeat. We want to call our on repeat function
07:15So I'm gonna send that call back and on repeat
07:19I don't need this one anymore because we're not doing anything with it right now and
07:24Back into our on repeat now. We could try to figure out a way to tinker with it beyond tinting
07:30Maybe we could also change its actual position of our actual
07:36Object itself so we could actually go to our actual TM
07:39and we could try to figure out if we have a way to actually change the property of
07:45Where it's actually going to and to do that we're gonna have to figure out a way to access
07:51Our actual properties that are being animated so in our case
07:56What are the properties that are being animated that properties are being animated are is our X and our Y if I go inside of my?
08:05Data object here, and I'm just going to trace out our X just to see what value comes back and again
08:11What I want to do next oh, so I want to only have one item because I want to not have clutter
08:18So I want to look at the traces that I'm getting back here first of all I'm getting an error because that's not gonna work
08:23Okay, so we're gonna have to find something else and again. This is exactly I didn't do any rehearsals for this
08:28We're completely doing this on the fly
08:31I don't even know if there is a way to do it
08:32And that's exactly what you're gonna be you're gonna be confronted with those type of scenarios as well
08:37So obviously I'm stressed out stressed out because you know I'm being recorded. I'm looking for something so help me out here
08:43Let's try to figure out let's look at the properties and try to find a property that we could use
08:48To change our actual destination
08:50And I'm actually remembering now as I'm doing this that there's a new property or a property that is only available for tween max enough
08:58Which obviously shows you how this is much easier with tween max
09:01Which is called a method called update to and the update to method enables you to basically dynamically change
09:08Whatever properties whatever variables you want in any given time so in our case. I want to change the parameters of my X
09:14Let's say we're gonna set it for 100 just for the sake of this example here for a second
09:19And I don't want to change the duration at all, so I'm not I don't want to reset the duration at all
09:25I don't want to do anything in it although if I wanted to I could have set the duration here
09:29And then click on the reset duration which would then reset our duration
09:33But I'm just gonna set those values up and run my animation again
09:36And this time around what we should get is after we're halfway through it's gonna start moving around to a different position
09:42But obviously this is not gonna do it
09:45But what we are learning from here
09:46Is that we probably don't need the repeat on at all because well we do want it to repeat
09:52But we don't need the yo-yo because what we could do is basically just set here at this value
09:57And just have it happen every single time there a repeat cycle happens
10:01And let me just delete this is not needed and here. I could just set a new stage stage
10:08with
10:09times math dot random
10:13And the same for our y
10:18math dot random
10:20And if we run it this time around every single beat is gonna go to a different spot
10:25Sometimes it's gonna be close sometimes it's gonna be far
10:29Now we could definitely see that we have your problem because when we're doing these repeats
10:34It's basically jumping back to its initial position, so it does make sense for us to actually
10:40Figure out a way for it not to do that and that means that we probably want to turn our yo-yo back on
10:47We probably don't want to update the position every single time, but even if we continue updating the position every single time
10:54Let's see what will happen. So first of all that wasn't madness. So it's
11:02Disappearing
11:05Okay, so we definitely have here a problem and and we kind of want to isolate it
11:10so I'm gonna kind of turn this on update off for a second and
11:15let's let it run for a second and
11:22One of the stuff that we could definitely come up with is
11:26Okay. So first of all, this is fine, you know going from one side to the other totally fine. Okay, cool
11:30Now let's take this update and let's update it only if once a full cycle completes
11:35So I'm gonna put it right here inside after a full circle completes
11:39Which would mean it should start going to a completely different position
11:46Okay, so one was good to still fine we haven't had any problem yet and
11:52This has been working fine
11:54But the only problem that we're having now is that we go back to the same spot twice
11:59Before we can move on to a new spot
12:02Which is not necessarily a horrible thing gives the user a chance
12:05But it makes it again too easy because then I could expect it right here
12:08Which is not exactly what I would want for it to be if this was really a game
12:12So we have to figure out a way to
12:14Reshuffle things around so to be able to reshuffle things around what I'm gonna do is I'm gonna go to my tween when it's in
12:21That halfway zone and I'm gonna say to it in it's halfway zone by the way your position right now is
12:28Your initial position by that that I'm gonna give it an invalidate basically telling it where you are right now
12:34Yeah, this is basically your starting point
12:37So you don't have to go back to where you were before because we're resetting our starting point again
12:41So if I run this one more time now, it's not gonna do
12:46Absolutely anything because the frown the first time around it's invalidating and basically making it get stuck in its course
12:54So that's that's not gonna work
12:58Alright, so we're definitely gonna need to think out of the box for this
13:02So to be able to think out of the box for this one
13:04I'm gonna do is I'm not gonna use on repeat at all instead of on repeat
13:08What I want to do is I'm gonna actually create the same call back
13:12but what I'm gonna do is I'll do a callback that's on complete only and
13:18I'm just gonna call it on complete function on and I'm
13:21Complete and the reason why I'm doing this is because I want to isolate my problems and kind of get rid of them
13:27And my problems are is there's too many factors too many variables going on and really I don't really need a repeat
13:34I need my animation to change each time and I'm just trying to reuse the same animation
13:39Without creating a new object each time which takes time
13:42so I'm gonna again just take my tween there and
13:46instead of actually instead of actually
13:50basically
13:51Changing it what I'm gonna do is once the animation is complete then I'm gonna go to my TM
13:56I'm gonna invalidate it by basically setting it put to wherever it is at this point in time
14:01And now I could call the on update because when I called on update, it's going update to sorry
14:07If I set the update to is I'm gonna change the direction of where it's actually going to after I already
14:14Repositioned and said okay where you are right now. This is your starting point now
14:17We want you to actually go here or for that matter
14:20We could have created a new tween creating a new to tween
14:24But because we already have the tween running
14:26Then there's no point in creating another one
14:28so I'm updating to and all that's left for me to do is to go to my TM and ask it to restart by asking it to
14:35play again or
14:39Play again, and if I run this again, we should have now an endless loop going on and it should oh
14:46Oh
14:48Basically, so if I let me just play it at zero making sure that it will and
14:55Here we go
14:56And now it's moving each time to a completely different place because when I said play the play head was already at then
15:01So I'm basically starting the play it from the beginning again after invalidated its position
15:06So it the beginning is actually the point where it was at already
15:10All right
15:11So this is this makes our game a lot more hard and now we could actually add into our game
15:1710 items and try to get them and it's gonna be a lot harder
15:20especially if we add here some sort of a timer that forces us to
15:24Start an amount of time because the items are and I'm going to the same spot
15:28And it's taking us way longer. It's not as easy as it was before. So there you go. That's it
15:34We are completely done with this. There's one more topic
15:37I want to talk about and we're gonna see it in the next video and then we're done with chapter
15:43five
