|
||||||
| Tutorials Programming Tutorials - Post your tutorials here! |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
Flash: Sprite Animation
Basic Animation of Sprites
Since we can change the location of sprites by changing the x or y coordinates, animation is easy. All that we have to do is constantly change the x or y positions. To do this, we can use the ENTER_FRAME event. This event runs when we enter a frame. Once one frame ends, a new one begins and this event occurs again. Thus the method is called again. This allows for a method to animate a sprite. First, we need to draw something to animate. Again I am going to draw a smiley because they are simple and easy to draw. ![]() My image: The next thing we need to is convert it to a symbol. To do this, select the smiley on the stage. Right click and go to "convert to symbol". Make sure you change the name of the symbol, make it a movie clip and export for action script. This will place a symbol in your library. On the stage, you have an instance of the symbol. In the properties window, you need to change the instance name. Now, we can start entering our code. Go to the timeline, and on the first layer, go to the first keyframe and press F9. This opens the window for you to start writing your action script code. First the event listener: Code:
stage.addEventListener(Event.ENTER_FRAME,moveSmile); Code:
function moveSmile(e:Event):void {
smile.x ++;
}
To make him move down, you just increase the y coordinate. Code:
function moveSmile(e:Event):void {
smile.y++;
}
Notice, that it just keeps moving forever. Eventually it will move off the stage. Even when it is moved off of the stage, it is still moving. Diagonal Movement This is as simple as changing both the x any coordinates. Example: Code:
function moveSmile(e:Event):void {
smile.y++;
smile.x++;
}
Instead of having the sprite slide across the stage, we can actually animate it. This is something that we will look into another day. This requires creating several frames for the movie clip. Then we can use code to tell us what frame we should go to next. This we will look at on a later day. However, this animation technique uses the same concepts as we are using here. Keyboard Movement Similary, we can use the concepts to make something move, when the user presses a key. The first thing we have to do is add an event listener at the stage level. This will listen for a key press. We will then use a callback function to determine which key the user pressed and move accordingly. Go back to your actions panel, and erase whatever you have there. The event listener we need is: Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveSmile); I'm not going to do that though. Code:
function moveSmile(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP:
smile.y -= 5;
break;
case Keyboard.DOWN:
smile.y += 5;
break;
case Keyboard.LEFT:
smile.x -= 5;
break;
case Keyboard.RIGHT:
smile.x += 5;
break;
}
}
Mouse Movement The last thing we are going to do is make him follow the mouse. There are two variables mouseX and mouseY that keep track of the location of the mouse's cursor. So we add an event listener for mouse move and in the callback function, we simply assign the mouseX and mouseY variables to the x and y variables of the smile. Easy, and it is cool. ![]() Code:
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveSmile);
function moveSmile(e:MouseEvent):void {
smile.x = mouseX;
smile.y = mouseY;
}
Once we find out how to hide the user's mouse cursor, we could easily use this technique to create our own cursors. |
|
||||
|
Re: Flash: Sprite Animation
That is one creepy looking smilie. +rep
__________________
Questions and Answers | Online News and Social Bookmarking | Code and Text Collaboration General Chat Forum |
|
||||
|
Re: Flash: Sprite Animation
I think we've found your new avatar. +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hacking Flash Games (PART 1) | TcM | Security Tutorials | 283 | 12-07-2009 10:10 AM |
| Flash: Sprites and Sprite Groups | chili5 | Tutorials | 2 | 09-01-2009 05:02 PM |
| Creating a Basic GUI With Adobe Flash | chili5 | Tutorials | 6 | 08-26-2009 03:57 PM |
| New Google Algorithim for Flash! | Brandon W | News | 9 | 02-09-2009 02:58 PM |
| Adobe Releases Flash Player 9 for Linux | Onur | Linux/Unix General | 1 | 08-28-2007 08:03 PM |
All times are GMT -5. The time now is 08:18 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc