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

Sprites

Lets add some interactivity to our rain sample. Its really easy in less then 5 minute we will take the animation that we have and add some interactivity to it.

Video Transcript

00:00So far we've worked our movie clip and now we've worked with our shape and as you can see and again our shape doesn't have
00:06Any interactivity nor does it have or could it contain?
00:10children I
00:11wanted to work now with the sprite and even though we don't really need the sprite because the sprite is actually
00:17Could have multiple objects as its children, and we're not planning on adding children to our
00:23Snowflakes or whatever we want to call them all we really need is an interactive object because what I want to do is
00:28Have a rollover state
00:31But I'm going to care
00:32I'm going to create a sprite for the sake of this example a sprite which would enable us to add children to our sprite
00:37But we're not going to do that in our application what I want to do is
00:43I want to change our loading our import from display dot shape to dot sprite
00:48We're going to select our sprite
00:52If we find it there we go so we have our sprite and instead of instantiating a new a new shape
00:59We're going to instantiate a new sprite, and we're going to create a new sprite, right?
01:03So we're going to set the variable as sprite and then create a new sprite
01:07Now if we run our application just as is with just with changing our
01:11Constructor basically those three things changing what we're importing
01:14changing the type of variable that we're storing and then creating in our loop a new a new sprite each time and
01:21We run our application. We're going to see we're going to get the exact same thing only now our
01:25MC oval could listen to events
01:28It could listen to interactiveness on the stage more accurately, so let's add MC oval
01:36And we're going to add to it an add event listener
01:40whoopsie and
01:42we're going to listen to the mouse event and we're going to listen to mouse event roll over and
01:52When that happens we're going to call on roll
01:55All right, so we have your little event
01:57And we're adding that a hundred times right because we're looping through and children a hundred times
02:01So we're actually added here a hundred events, and if we just listen to our function
02:06We're going to type in our on roll it accepts one parameter. Which was a mouse event. It does not return anything
02:13What else do we want to put here? Let's trace out our e.target. Which should return to us what we just clicked on and
02:23If we do that, and we just roll over the element
02:25We'll see that we're getting back our sprite, which is exactly what we wanted to do
02:29So just to wrap up this little small sample what I want to add is whenever you roll over
02:34We're just going to snap our element back to the top of the stage
02:37Which is basically the code that we had in our enter frame when the object passed our stage height
02:43I'm just going to put that directly
02:45Inside of our R and over and obviously this is not going to work right now because there is no such thing as MC oval
02:51So I'm just going to grab the MC oval, and I'm going to set the value of e.target
02:57place that e.target into our MC oval and
03:01Run that app one more time oopsie. I've got a little error here because we didn't define it so var
03:07MC oval and it's going to be a type of
03:11sprite this time around now as we run this we're going to have a little error because flash is going to tell us well this target is
03:18Not a sprite or at least we don't it doesn't know it's a sprite. Let's see the exact error so basically is going to say implicit
03:25corrosion
03:27Corrosion of a value with a type static type object to a possibly unrelated type flash display spray
03:34Now we know that our event is listening to a sprite, so we're just going to tell flash. Don't worry about it
03:39We know it's a sprite to do that before we add our e.target. We're going to say e.target as
03:46sprite
03:47Basically that is telling flash treat this object as a sprite because we know it's a sprite don't worry about it
03:54And as we do that flash won't create that compiling error anymore, and as we roll over elements. They're going to pop away
04:01Disappear, but actually they're going to come back to the beginning and start all over again, and there we go
04:07That is our little application

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!