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
11 Responses to "Getting started with Actionscript 3." 
|
said this on 01 Jun 2007 11:08:03 AM CDT
I like the site, and the first 2 tutorials I looked where great. This however, says its for beginners, and thats me, for sure. But the first thing you say to do is open 'flex builder'. What is that? What does it have to do with A.S. 3.0? Better yet, what does it have to do with Flash? I stopped reading it after that.
If this is not a beginner's tut, label as such, please. You seem be assuming a lot about us. The history was nice, but prerequisite knowledge should be listed. A quick goal could be stated.. a summary of whats going to be done in the tut to teach lesson.. (what the example will be about). Stuff like that.. so we're not wondering whats going on Thanks! |
|
said this on 13 Jun 2007 3:31:05 AM CDT
yes i believe frank is very much true since as i saw the heading 'begginer' i thought some basics must have been cleared..but to our amusement, i saw the word flex builder. hence forth pls clarify....such things .... think of us a dummies...
|
|
said this on 24 Jun 2007 12:48:30 AM CDT
This article is exactly what I was looking for. It seems that all the manuals and books out there fail to show a straightforward and simple way of compiling actionscript source code directly to SWF.
|
|
said this on 27 Jun 2007 3:42:53 AM CDT
Thank you very much for your tutorial!! I am a begginer and found it very informative. The stuff I didn't know about I used my commonsense and googled it. Thanks for spending some of your time to educate others! Appreciated!
|
|
said this on 14 Jul 2007 4:05:39 AM CDT
This tutorial was exactly what I was looking for. I do agree with the author that it is a beginner level tutorial, but mainly for those who already know how to code.
|
|
said this on 19 Jul 2007 2:53:32 PM CDT
First Run, test movie and I've gotten two errors from the start.
1046: type was not found or was not a compile-time constant: Shape 1180: Call to a possibly undefined method shape. What a waste of time and effort. |
|
said this on 02 Aug 2007 8:10:21 AM CDT
-----------------
@zalah You must import flash.display.Shape; Then you didn't get this error. ----------------- It is a very good Tutorial for Beginners, who know other programming languages. Greetings |
|
said this on 03 Sep 2007 12:54:14 AM CDT
When I first read the very first few lines of the article, I thought I was so stupid since it is supposed to be a beginner's and I have been developing materials using flash for quite some time now. However, after reading some of the comments I feel I am not the only one in the dark.
Thanks |
|
said this on 27 Dec 2007 8:59:43 AM CDT
thank you very much. this and the second part of this tutorial very cleared my mind about creating class methods.
appreciated !:) |
|
said this on 01 Mar 2008 9:09:31 AM CDT
All works fine but
After changing the MovingCircle.as at the last step I get this error: TypeError: Error #1007: Instantiation attempted on a non-constructor. at MovingCircle() at FirstCircle/init() |
|
said this on 06 Aug 2008 8:41:23 AM CDT
Overall I liked this tutorial, though I'd recommend people completely new to AS3 to take a few more basic tutorials first. This tutorial is doable without Flex Builder. I don't know the program, but I will check it out when I'm more comfortable with the AS3 workings and syntax.
I had a similar problem as Dasn, I solved it by not using the embed at all. I added the audio file to my library, gave it linkage (right click -> linkage, check export for actionscript) with base class flash.media.Sound and gave it a class name BallHit. That has as a result that you can use: sfx = new BallHit(); Be sure to declare the variable as a Sound, just as in the tutorial though. I haven't tried this for the image, but I image if you export it in the library as class BallImg (use baseclass movieclip or sprite) you could use gfx = new BallImg() or something. I used this method to get sound working in my app, but it's very possible that my method is needlessly complex and sounds can be imported in a tidier and faster way. Cheers, Jasper |



Author/Admin)