Archive | AS3 Solutions RSS for this section

AS3 Event Dispatcher Blog

There are 3 people involve in as3 event dispatcher.
1. The dispatcher
2. The listener
3. The worker

What are their job responsibilities?

Dispatcher

The event dispatcher dispatches event.
In other words, it broadcast an event when it is triggered by
something. For instance, the basketball player throws the ball into
a goal, the goal dispatches event (+1 Score to Team A)

A dispatcher can dispatch a common event called Event, or
a Custom Event which you made. A custom event allows you
to insert one or more useful data into it. For example:
(1st Option) Point 1
(2nd Option) Points 3, Description: Ball is thrown from outer ring.
etc…

The dispatcher also needs to name the event that it is dispatching.
For instance, EnemyBall1, or Player13 or GoalTeamA.

A code example:
super.dispatchEvent (new CustomEvent (1,Ball.GOAL_TEAM_A) );

p.s. The Ball refers to Ball.as class
While GOAL_TEAM_A is a public static const (constant variable)
with String data type. You can use any class public static const
string, not necessarily the public static const string from the
class that it is dispatching the event!

Listener

The event listener is the middle-man.
It interacts with both dispatcher and worker.
Since the dispatcher has already named his Custom Event with
a tag/name (such as BALL.GOAL_TEAM_A) it knows which worker
to call to perform a particular task when that particular Custom
Event is being dispatched from the object that he is listening.

Before it could know which Event is being called,
it must have an object to listen.
Example: you are listening to my blog (or rather reading my blog
than reading any other actionscript blog). When you notice
a word called BALL.GOAL_TEAM_A in my blog, you perform a task.
Such as copying it down to your note book. See how it works?
If other blog has the word called BALL.GOAL_TEAM_A, you do
not perform any task if you found it, because you are NOT
reading their blog.

Worker

The worker perform any task that you have described.

For a greater detail with codes , refer to this blog:
http://www.8bitrocket.com/newsdisplay.aspx?newspage=5776

Summary:
The dispatcher does 2 things during the dispatching event process. It adds a tag (unique identifier) to allow the Listener to know which event to look at, it uses a custom event to add more data to it so this custom event can acknowledge the listener AND provide useful data to the worker.
e.g. super.dispatchEvent(new CustomEvent(CustomEventType.EVENT_ID));

The listener needs to do things which are assigning the task to the worker when the object its listening to dispatches an event, the listener also needs to know the EVENT_ID so it knows which event it should be concern with. If the class its listening to have hundreds of event, with the EVENT_ID it can filter out all those events easily to see whether the event that its interested has been triggered.
triggerobject.addEventListener(CustomEventType.EVENT_ID, doSomething);

The worker needs to do 2 things, accept the custom event object that and do the work

public function doSomething (evt : CustomEvent) : void
{/*do something here */}

Advertisements

Newbie Guide on When to use or apply MVC Pattern Technique

Using patterns will certainly increases the complexity of any
software code through additional classes but it helps in
breaking tight coupling.

Something I learn while using MVC is, don’t use it unless is needed.

For instance, the car example from actionscript 3
design patterns (chapter mvc)

If you tried to use MVC on a simple component, it would be:
Model – the properties of the car (car color/rotation/etc)
View – consists of codes that shapes the car, gets data from model.
Controller – consists of codes that changes model data.

When is the right time to use MVC ? (model view controller)
When not to use MVC? (applying KISS – keep it simple)

Before using MVC on anything, ask yourself a few questions:

1. Can the model be used by multiple views?
If the answer is no, just combine the view with the model.
Since the model is only used by 1 view, it is usually takes part
in creating the view (where the model is used to tell the
view on how it should be created)

You need to remember that separating code into different class,
do improve readability but it causes several problems such as:-
(a) the software becomes very complex with many classes.
(b) removing a particular class effects the class that is using it.

Example of model being used by multiple views would be,
a sports car (the model) and the car gadgets (multiple views).
The car will provide the properties (fuel/speed/water) while each
gadgets will keep track one of these properties for changes.

2. Does the model involve in creation of view?
If the answer is yes, just combine the view with the model.
Reason being, the model is only used by that specific view.
For example, the car model provides the width, height, color.
All these can’t be applied for other transportation such as plane
or truck.

3. Is it necessary to place controller into its own domain?
In most cases such as game programming, the controller
tends to be combined with either view or model.
You can use (view + controller) and (model + controller)
to get the best of both worlds (the advantages)

Separate the controller away from model and view if you see
that the controller can be shared by different view/controller.
This allows code re-usability (save time than coding same stuff)

Controller are usually specific for either model/view.
The more flexible it becomes (in terms of code reusability where
the controller is in its own domain) the harder it would be
generic so that it can be used on different objects.

An analogy would be, car, motorcycle and truck.
The car acts as the generic transportation.
Its much faster than a truck and able to carry more passengers
than a motorbike. Truck can carry twice or more than car, but
slower in speed. The motorcycle is the fastest as it can whiz
through the traffic with ease and arrive to destination faster
than both of them, but carries the least amount of passenger.

To clarify the analogy, the car transport can be shared by
different classes. One class might require speed, the other class
might need to carry stuff. But it does not do them that well.
If you notice that if you use specific transport for a specific
purpose such as arriving to destination fast, then probably you
would choose a motorcycle to whiz through the queuing cars.
It gets the job done better than a car! Truck is similar concept.
Truck does better job in carrying bigger and heavier stuff than
a car. You would need to weight the pros and cons between
these. Code reusability such as the car, you will avoid making
specific code since it can handle both situation good, so you
do not need to make extra code (saves time). On the other
hand, if you make specific code for either the model/controller,
it does the job much better/faster than a generic one since
its customized to do a specific purpose.

An example of how view and model works would be a preloader.
The view shapes how the preloader looks like, the controller
gets information from the model (such as the stage) for total
bytes loaded and current bytes loaded. Then it manipulates that
data into portions to create a smooth progress bar, else the
bar would be jerking as in 1 chunk of load and later smaller chunk.
If we were to place the controller inside the model, this causes
more busy-ness as in the model dispatches event multiple times
for X number of portion divided by the controller. As we know,
flash perform slower in creating objects. the more objects
being created (as in events object created to be dispatched),
the slower it will become.

An example of how model combines with controller would be
a RPG game where you have a character with 4 attributes,
strength, dexterity, luck, and intellect (intelligence).
A user clicks a button (the view component) to request for
weapon damage. The controller in (model + controller) class
will reply to the request by returning the manipulated data.
To calculate the weapon damage, the controller
manipulate one or more of these stats base on the job.
For instance, you are a warrior, your weapon damage is base
on strength and dexterity. So your controller get these 2 data
and manipulate it (example: strength x dexterity x 1.5) and
return these new data back to the view.

Extra MVC Technique Notes


After reading my friend’s opinion on how you use mvc, and another
person’s view on how mvc should work, I understand when to mix
and match them depending on the situation, it isn’t necessary to
stick to one (for example: sticking to view + controller , model)

A simple analogy would be a game such as Desktop Defender.
You don’t use purely rocket towers and pellet towers, but use
a variety of the towers at your disposal to maximize the
efficiency of handling many incoming waves quickly.

A typical multiplayer strategy in any mode would be building
pellet tower first because they are cheap, but once you have
sufficient money, sell those and upgrade to squirt tower as
they have faster fire rate and greater range than pellet
but cost a little more to upgrade. Then switch to rocket
towers for their splash damage and long range, and the list
goes on. As you can see, using different towers at the right
time provides the best result. In case of this game, the
result would be additional points for the time saved by
finishing each monster waves quickly.

In the case of MVC, model + controller (a pellet tower) would
provide flexibility of manipulating the data of the model before
handling to any views. This is to allow standardization of
manipulated data results for all views. For view + controller,
some view wants to have a unique way of displaying the data,
therefore, it has its own unique controller to do it.

ActionScript 3.0 Tips

it is also a not so good practice when you don’t cast objects to the expected type

(event.target as TextInput).text == "")

Kongregate Labs Shootorial Resources

Learn to create your first game using Flash!
Making games is easy! Well, okay, maybe it’s actually kind of hard, but starting out is easy at least!
Especially when you have Kongregate’s shootorials (shooting tutorials) to guide you through the process.
Check out the link below to play through the game you can learn to build yourself. Play ‘Shoot!’ now

Kongregate Labs Homepage
Download Link: Adobe Flash Software Trial 30 Days
Download Link: Shootorial Sourcecode ActionScript 3 AS3
Download Link: Shootorial Sourcecode ActionScript 2 AS2

Shootorial 0: Flash!
This beginner flash game programming tutorial covers the basics of downloading the free trial version of Adobe Flash CS3, as well as covering some of the fundamentals of using Flash as a game making tool.

Shootorial 1: Ship Movement Via Keyboard Input
This is the foundation of any game. The player needs to be able to control a game object through user input, usually either via mouse or keyboard. Shootorial’s user controlled game object (or sprite) is a futuristic hand drawn space ship.

Shootorial 2: Scrolling Background
Every game hero needs a world to defend or conquer. The Shootorial game world is represented by a hand drawn fantasy world background that slowly scrolls by, creating the illusion of moving through a virtual space over time.

Shootorial 3: Missiles
The hero needs to fire missiles in order to stop the invading minions. This tutorial explains how to fire missile objects based on keyboard input. These missiles will originate from the hero ship and move across the screen.

Shootorial 4: Enemy Ships
The hero ship needs adversity. In this tutorial we will build a basic enemy ship that will attack the hero in waves.

Shootorial 5: Collision Detection
This tutorial covers how to detect a collision between the hero ship and enemy ships. It also covers how to detect collisions between hero missiles and enemy ships, as well as collisions between enemy missiles and the hero ship.

Shootorial 6: Adding Score, Health Meter and Game Over & Reset Logic
Once the collision detection is in place, we can start recording hits, misses, points earned and damage taken. This tutorial also covers game over menu and game reset logic.

Shootorial 7: Adding Power Ups, Mini Bosses and Bosses
This beginner level Flash game programming tutorial covers power ups, mini bosses, and boss battles.

Shootorial 8: Adding Enhancements, Audio and The Kong API
This beginner level Flash game making tutorial covers adding sounds, implementing the Kong Stats and High Score API, and hints on adding game play enhancements. The fully commented source code for the final game is included in this Shootorial.

Kongregate Labs Shootorial Frequently Ask Question [Shootorial FAQ] written by Jabor
What is Shoot!?
Shoot! is a simple side-scrolling shooter made in Flash – a reasonable first challenge for a budding game developer to make.
Ok, but what are the Shootorials?
The Shootorials are where the real work happens! They show you, step by step, how to create your own game just like Shoot!, starting with downloading Flash and finishing with a slick user interface. Only the first three shootorials are released at the moment, but the remaining 6 are coming soon!
Where do I find the shootorials?
You’ll find the shootorials (along with some other cool stuff, eventually) at the Kongregate Labs page: www.kongregate.com/labs
I already have a version of Flash, but it’s not CS3. Do I still need to download the free trial?
If your version is older than Flash MX 2004 (which is over five years old now), you will need to download CS3, as versions before that do not support ActionScript 2.0. If you have something more recent, then it is your choice – but be warned that the interface will likely be different to that demonstrated in the shootorials.
What’s this contest stuff I see on the page? Can I win real money?
You certainly can! All you need to do is make a game by following the shootorials, and then upload it to Kongregate by following the instructions that will be posted. – Full rules and regulations can be found here.
What’s this about a free trial? Do I have to pay money to create games?
Adobe Flash comes with a 30-day free trial. If you want to use it after that, you have to buy the software – which can be quite expensive. If you’re still keen on creating games, then there are some free alternatives – check the stickied threads in the Programming forum for more details and help on using them. Most of the free tools have a steeper learning curve than Adobe Flash, but are just as powerful and easy to use (or even easier!) once you know your way around.
Ok, so I’ve followed the tutorials, made my game, and discovered that I really like programming! Where do I go to do some more?
It’s good to see talented people keen to get involved in making games 🙂
The best place to start would probably be some beginner tutorials – You can use Google to find some yourself, or if you’re really stuck, just ask in the forums, after making sure you’ve put in an honest effort to find the answer yourself.