Go Back   CodeCall Programming Forum > Software Development > Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Tutorials Programming Tutorials - Post your tutorials here!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-31-2009, 07:48 AM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
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);
This function is called everytime, we enter a new frame. Once the frame is done, we enter a new frame and this calls the method again. Now the method simply has to change the coordinates of the image.

Code:
function moveSmile(e:Event):void {
	smile.x ++;
}
This function makes him move left to right, at a rate of 1 unit(pixel) per frame. You can see what this looks like here.

To make him move down, you just increase the y coordinate.

Code:
function moveSmile(e:Event):void {
	smile.y++;
}
You can view this here.

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++;
}
This is going to make him move diagonally to the bottom right. You can view the result here.

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);
Everytime the user presses a key, we will call the moveSmile function. However, we don't want to move our smiley everytime we press a key. Why would we move if the key pressed is A? Since this is just a demonstration I am calling it moveSmile but a better name for the function would be keyPressed. All this function should do is determine which key was pressed and call an appropriate function for that key.

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;
	}
}
The switch structure is more like a generalized if structure that assesses one variable. The movement code should make sense just by reading it. By including only the keys we care about, nothing happens when the user presses any other key. You can view the result here.

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;
}
You can view the result here: Mouse Move.

Once we find out how to hide the user's mouse cursor, we could easily use this technique to create our own cursors.
Attached Thumbnails
flash-sprite-animation-smile.jpg   flash-sprite-animation-properties.jpg  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-31-2009, 08:52 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Flash: Sprite Animation

That is one creepy looking smilie. +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-01-2009, 05:04 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0