ForumsProgramming ForumSetting up the "console" Flash-as3 classes

0 8642
weirdlike
offline
weirdlike
1,299 posts
Prince

Well I created a startup kit already, you can find it HERE, but I received some feedback regarding the lengthiness of it, and overall confusion as how it comes together. So I'll try and keep things slimmed down as much as possible.

-----------------------------------------------------------------
follow along by grabbing the source HERE

or you can check it out on my server HERE

console.fla
console.as
keyboard.as
gamePlay.as

Completely commented and ready(might need to right click-save as)

To give you an idea of how I like to construct my games you can take look at this IMG as a reference.
http://i.imgur.com/Bo60LXn.jpg
It might seem complex at a glance but really it is pretty simple

So start by creating a new .fla file, add a frame then create an .as file and link them together.
http://imageshack.us/a/img202/4176/61558352.jpg
http://imageshack.us/a/img194/5817/24413230.jpg
http://i.imgur.com/T6N5jne.jpg
note: the box "export classes in frame" change it frame 2, the document class will be whatever the name of your .as file is.

Once that is done you can dive into the programming. The next section here will be a very light pre-loader then adding the controls setting up the audio and adding the game.

Pre-loader:
Awhile back I made a detailed topic on the pre-loader HERE, but with this time I will keep it slim via code only no MovieClips.

//add listener to trace the loading percentage
loaderInfo.addEventListener(ProgressEvent.PROGRESS, showPercentage);
function showPercentage(event):void
{
trace(Math.floor(100*stage.loaderInfo.bytesLoaded/stage.loaderInfo.bytesTotal)+ "% Loaded"
}

//add listener for when loader is complete
loaderInfo.addEventListener(Event.COMPLETE, loaded);
function loaded(event):void
{
//remove listeners
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, showPercentage);
loaderInfo.removeEventListener(Event.COMPLETE, loaded);

//load keyboard, audio, and game
}

Keyboard:
Like the img above shows the keyboard controls will be in a different class, create a new .as file and construct the key codes there.

//2 listeners one for key press and another for key release
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyDepressed);

//keypress function
function keyPressed(event:KeyboardEvent):void
{
//set keys as switch
switch (event.keyCode)
{
//case if desired key is pressed
case Keyboard.UP:
{
//up function
break;
}
case Keyboard.DOWN:
{
//down function
break;
}
case Keyboard.LEFT:
{
//left function
break;
}
case Keyboard.RIGHT:
{
//right function
break;
}
}
}

//key release
function keyDepressed(event:KeyboardEvent):void
{
//set keys as switch
switch (event.keyCode)
{
//case if desired key is pressed
case Keyboard.UP:
{
//up function
break;
}
case Keyboard.DOWN:
{
//down function
break;
}
case Keyboard.LEFT:
{
//left function
break;
}
case Keyboard.RIGHT:
{
//right function
break;
}
}
}

Audio:
The audio will be set up in a way where the stage will listen for a triggering dispatching event from any class

//setup the channel to play a sound
var effectsChannel:SoundChannel = new SoundChannel();

//add the dispatching listener to trigger the sound
stage.addEventListener("DISPATCHINGEVENT", EXECUTE);
function EXECUTE(event):void
{
//declare the variable which is the sound (look in console.fla library to see the linkage)
var soundName:sound = new sound();

//play the sound
effectsChannel = soundName.play();
}

then in any class trigger the dispatch like so
dispatchEvent(new Event("DISPATCHINGEVENT", true));

Game:
At this point you will be going in your own direction as to what game you want to play, but you can make the loop to listen for the key press events

//add the loop there should only be 1
addEventListener(Event.ENTER_FRAME, loop)
function loop(event):void
{
//look at your main .as file that is liked with your .fla
if(console.up)
{
trace("up pressed&quot
}
if(console.down)
{
trace("down pressed&quot
}
if(console.left)
{
trace("left pressed&quot
}
if(console.right)
{
trace("right pressed&quot
}
}

You can still find the more in depth startup kit which includes a menu screen game play and game over screen HERE

  • 0 Replies
Showing 1-0 of 0