Getting started with Actionscript 3.

Part Three - Embedding and Interactivity
Milan Toth
Milan Toth is the Chief Flash Developer of Jasmin Media Group, he created one of the world's biggest flash media server system. He loves Eclipse and OS X, AS3 and JAVA, sci-fi and horror, metal and electronic.
Download the two attachments of the article, and place them in the root of your Flex project.
MovingCircle.as will look like this:
package
{
import flash.media.Sound;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Bitmap;
public class MovingCircle extends Sprite
{
// for realistic movement we use gravity
public static var gravity:Number = 1;
public var xspeed:Number;
public var yspeed:Number;
// we want to "throw" the ball, that's why we need previous positions
// to calculate new speed components
private var oldx:Number
private var oldy:Number;
// gfx will be our "skin"
// sfx will be our sound
private var gfx:Bitmap;
private var sfx:Sound;
// that's how we embed external assets
// we have to define an individual class name for them
[ Embed ( source = "soccerball.gif" ) ] private var soccerGFXClass:Class;
[ Embed ( source = "pop.mp3" ) ] private var soccerSFXClass:Class;
public function MovingCircle ( )
{
// instantiate embedded assets
gfx = new soccerGFXClass( );
sfx = new soccerSFXClass( );
// attaching ball to display list
addChild( gfx );
// skin repositioning to middle to make bouncing more precise
gfx.x = - width / 2;
gfx.y = - height / 2;
}
// initialization, called after parent addChild
public function init ( ):void
{
// x , y and stage are inherited properties
// watching our width and height not to cross "wall"
x = width / 2 + Math.random( ) * ( stage.stageWidth - width / 2 );
y = height / 2 + Math.random( ) * ( stage.stageHeight - height / 2 );
xspeed = 5 + Math.random( ) * 10;
yspeed = 5 + Math.random( ) * 10;
// start step triggering based on enterframe event
addEventListener( Event.ENTER_FRAME , step );
// add mouseevents to make ourself draggable
addEventListener( MouseEvent.MOUSE_DOWN , dragCircle );
addEventListener( MouseEvent.MOUSE_UP , releaseCircle );
}
private function dragCircle ( event:MouseEvent ):void
{
// when mousedown, we starting to listen to mouse movement of the stage
// to catch mouse coordinates
stage.addEventListener( MouseEvent.MOUSE_MOVE , moveCircle );
// we stop stepping during dragging
removeEventListener( Event.ENTER_FRAME , step );
}
private function releaseCircle ( event:MouseEvent ):void
{
// removing stage mousemove listening
stage.removeEventListener( MouseEvent.MOUSE_MOVE , moveCircle );
// start stepping
addEventListener( Event.ENTER_FRAME , step );
}
private function moveCircle ( event:MouseEvent ):void
{
// store previous coordinates
oldx = x;
oldy = y;
x = event.stageX;
y = event.stageY;
// calculate new speed components
xspeed = x - oldx;
yspeed = y - oldy;
}
public function step ( event:Event ):void
{
// sett position
x += xspeed;
y += yspeed;
// adding gravity to y component
yspeed += gravity;
// rotating for realistic movement
rotation += xspeed;
// bounce ball at stage edges
if ( x + xspeed > stage.stageWidth - width / 2 )
{
xspeed *= -1;
sfx.play( ); // playing sound
}
else if ( x + xspeed < width / 2 )
{
xspeed *= -1;
sfx.play( ); // playing sound
}
if ( y + yspeed > stage.stageHeight - height / 2 )
{
yspeed *= -1;
sfx.play( ); // playing sound
}
else if ( y + yspeed < height / 2 )
{
yspeed *= -1;
sfx.play( ); // playind sound
}
}
}
}
Run and try. Drag the ball, and throw it. Hope you like it.
Spread The Word
Article Series
This article is part 1 of a 2 part series. Other articles in this series are shown below:
-
Getting started with Actionscript 3.
Related Articles
Attachments
20 Responses to "Getting started with Actionscript 3." 
|
said this on 01 Jun 2007 11:08:03 AM CST
I like the site, and the
If this The hi Thanks! |
|
said this on 14 May 2009 4:01:17 AM CST
Great article.
I am a And he actually DOES me "AS3 code can Win. |
|
said this on 13 Jun 2007 3:31:05 AM CST
yes i believe frank is ve
|
|
said this on 24 Jun 2007 12:48:30 AM CST
This article is exactly w
|
|
said this on 27 Jun 2007 3:42:53 AM CST
Thank you very much for y
|
|
said this on 14 Jul 2007 4:05:39 AM CST
This tutorial was exactly
|
|
said this on 19 Jul 2007 2:53:32 PM CST
First Run, test movie and
1046 1180: Call What a w |
|
said this on 02 Aug 2007 8:10:21 AM CST
-----------------
@zalah You must import flash Then you ----------------- Greetings |
|
said this on 03 Sep 2007 12:54:14 AM CST
When I first read the ver
Thanks |
|
said this on 27 Dec 2007 8:59:43 AM CST
thank you very much. this
appreci |
|
said this on 01 Mar 2008 9:09:31 AM CST
All works fine but
After TypeErr at MovingC at Firs |
|
said this on 06 Aug 2008 8:41:23 AM CST
Overall I liked this tuto
I had a similar pr That has as sfx = new BallHit() Be sure to declare t I used thi Cheers, Jaspe |
|
said this on 13 Oct 2008 9:46:50 PM CST
Good tut. I ran it using
|
|
said this on 30 Dec 2008 10:27:36 PM CST
For those confused by Fle
You will have three 1) Create FirstCircle.as 2) Create Movi 3) Crea 4 I Good luck! Thanks for the tutori |
|
said this on 14 Jan 2009 4:41:47 PM CST
I'm getting errors 1120 '
|
|
said this on 12 Feb 2009 3:49:06 AM CST
Good tutorial, but my bal
|
|
said this on 15 Jun 2009 4:20:35 AM CST
Halcyon, you copied the c
|
|
said this on 14 Aug 2009 9:45:11 AM CST
This tutorial is nice and
Flex Builder != |
|
said this on 27 Oct 2009 7:59:57 AM CST
Darren, if you notice Fle
|
|
said this on 21 Oct 2009 6:04:24 PM CST
Excellent source for anyo
|


Author/Admin)