AS3 - Defining functions inside of other functions - actionscript-3

When adding event listeners and defining their corresponding functions I find myself defining the function in the code of a constructor a lot. Something like this:
package
{
public class stuff extends Sprite
{
public function stuff()
{
minimizeBtn.addEventListener(MouseEvent.CLICK, minimizeOnClick);
function minimizeOnClick(e:MouseEvent):void
{
//do minimization stuff here
}
}
}
}
However, there is clearly another option to define it like any other method of the class. Something like this:
package
{
public class stuff extends Sprite
{
public function stuff()
{
minimizeBtn.addEventListener(MouseEvent.CLICK, minimizeOnClick);
}
internal function minimizeOnClick(e:MouseEvent):void
{
//do minimization stuff here
}
}
}
The second option may not really make sense because the function isn't really a method of the class. But my concern is that the first method will use up extra memory for each instance of the class. Which is the most efficient and correct way to do this and also does the first method take up extra memory or CPU time for each instance of the class?
Thanks!

The latter example is the correct way, and it's true that you should try encapsulate your addEventListener() and listening function within the relevant class. In your case, you may want to consider making a class for your minimizeBtn:
public class MinimizeButton extends SimpleButton
{
public function MinimizeButton()
{
addEventListener(MouseEvent.CLICK, _onClick);
}
private function _onClick(e:MouseEvent):void
{
// do minimization stuff here
}
}
MinimizeButton's _onClick() should then target the relevant instance of your class stuff and run whatever stuff needs to do from there.
This example's process is more like:
MinimizeButton: "I've been clicked, I should inform stuff so it can do something relevant."
Rather than:
stuff: "I'm going to sit and wait for MinimizeButton to get clicked, then I'll do what's required."

Related

AS3 MVC Design Multiple Controllers

So I'm reading a book about MVC and the author create a controller (PlayerController) and put some functions for keyboard use and some for mouse use. But he just comment out every keyboard use.
It gave me a idea to create 2 controllers, PlayerMouseController and PlayerKeyboardController so I can decide how to control the player changing one line. And if I can design this way, later I can add a AIController for monsters that use the same view and model but are controlled by AI and so on...
I have my model Player and it do the physics stuff. Now I want two controller, one for mouse and other for keyboard. So I create a PlayerMouseController and PlayerKeyboardController.
The PlayerMouseController has 2 functions: processUpdate() and processMouseDown()
The PlayerKeyboardController has 2 functions: processKeyDown() and processKeyUp()
I create the object like this:
_player = new Player();
_playerController = new PlayerMouseController(_player);
_playerView = new PlayerView(_player, _playerController, stage);
addChild(_playerView);
If I want to change the controller I can just change the _playerController line for this:
_playerController = new PlayerKeyboardController(_player);
And it works fine... But I dont know if the design I use is fine for a large project
To make this work I have to create a Controller class with nothing so I can extends the others controllers and my view can call all methods.
public class Controller
{
public function processKeyDown(e:KeyboardEvent):void
{
}
public function processKeyUp(e:KeyboardEvent):void
{
}
public function processUpdate(stage:Stage):void
{
}
public function processMouseDown(e:MouseEvent):void
{
}
}
In my view (PlayerView) I accept any Controller:
public function PlayerView(model:Player, controller:Controller, stage:Stage)
{
_model = model;
_controller = controller;
_stage = stage;
}
and I decide what to use based on its type:
if (_controller is PlayerKeyboardController)
{
_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
_stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
else if (_controller is PlayerMouseController)
{
_model.addEventListener(Model.UPDATE, onUpdate);
_stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
// EVENTS PlayerKeyboardController
private function onKeyDown(e:KeyboardEvent):void
{
_controller.processKeyDown(e);
}
private function onKeyUp(e:KeyboardEvent):void
{
_controller.processKeyUp(e);
}
// EVENTS PlayerMouseController
private function onUpdate(e:Event):void
{
_controller.processUpdate(_stage);
}
private function onMouseDown(e:MouseEvent):void
{
_controller.processMouseDown(e);
}
So... this is a good idea? How should I improve??
I think as your project evolves you'll soon hit a bottleneck with this kind of approach. I'd suggest creating a general IEntityController interface (don't be afraid of the word 'entity', it just shows that this is a game object controller, something that is parent for example of hero, enemy, etc.).
Then I'd create two separate implementations of this interface: BasicMouseController and BasicKeyboardController, so that I'd have two branches of these controllers with basic logic. If you need additional functionality for the Hero, you'd create a HeroMouseController class which would extend the BasicMouseController class and would have the advantage of calling super methods and adding the extended functionality easily.
You'd still have the benefit of passing different controllers to the PlayerView class as it's constructor would receive an IEntityController as a parameter, meaning anything implementing this class can be passed in.
There are many approaches for problems like this and StackOverflow is usually not meant to give these kind of answers, as every answer to these kind of questions is subjective and this website more fit for Problem/Solution kind of posts.

Can dispatchEvent() carry arguments in AS3?

Behold this example:
addEventListener("myEventType", myFunction("argument"));
function myFunction(args:String):Function {
return function(evt:Event):void {
trace(evt.currentTarget, "has", args);
};
}
dispatchEvent(new Event("myEventType", true));
It works.
Can I do something similar, but passing "argument" through dispatchEvent()?
It'd be very handy in a situation where dispatchEvent() is in a wholly separated class from addEventListener() and myFunction().
I'll be needing this a lot, so I want to do it without creating a custom event class for every situation.
You can use native flash.events.DataEvent for passing String parameter or create custom DataEvent with data:* property in all situations where you need to pass parameters to event handler.
If you want to customize the behavior of event listener in the place of adding event listener you can create "listener" object for holding this custom parameters (but I think this technique is more complicated than custom events):
addEventListener("myEventType", new EventListener("param1").onEvent);, whereEventListener is the class like this:
public class EventListener
{
private var params:*;
public function EventListener(params:*)
{
this.params = params;
}
public function onEvent(event:Event):void
{
trace("onEvent, params = ", params);
}
}
You could take a look at Signals (https://github.com/robertpenner/as3-signals). They are an alternative to Events and you can send whatever extra params you want with a Signal.

Calling a function from one class, from another

I'm having a slight issue with ActionScript 3 and I have come here to ask for some help.
I have two classes. One called Sledge and one called Sock, there is also the document class called Main.
My issues are as follows:
Inside of Sledge, I call a function that is defined inside of the Main document class. How would I go about telling the class to go to the document class and run that function? Would this also be the same for other classes or just for the document class?
Inside Sledge, I have the following statement: if(hitTestObject(sock.myHitArea)) { /* somecode*/ }
sock is an instance of another seperate class, and by this point has already been created. However when I try and run this I am told it is not defined. How would i go about solving this?
There's some ambiguity issues with how you expressed your question. It would help if you posted a short form of the code for the problem.
However, I'll try to answer the first question:
Inside of Sledge, I call a function that is defined inside of the Main document class. How would I go about telling the class to go to the document class and run that function?
You would want to pass the Main class to the Sledge class or use events which is preferable. If pass the class it will look like this...
class Sledge {
private var main:Main;
function Sledge(main:Main) {
this.main = main;
}
function doSomething():void {
main.runSomeFunction();
}
}
Or if using events:
class Main {
private var sledge:Sledge;
function Main() {
sledge = new Sledge();
sledge.addEventListener("mainDoSomething", doSomething);
}
private function doSomething(e:Event):void {
// .... do stuff
}
}
class Sledge extends EventDispacter {
function Sledge() {
}
public function doSomething():void {
dispatchEvent(new Event("mainDoSomething"));
}
}

AS3: Accessing custom class public functions from a MovieClip on a timeline

I've got a AS3 program with a Main.as custom class.
In this class I load an instance of a 'menu' movieclip which has simpleButton instances inside... How do I access the Main class public functions by the menu movieclip buttons?
I.e. Menu button -> gotoPage(5); (which is a Main public function)
If I try to access the Main function with the above statement, it gives
"1180: Call to a possibly undefined method gotoPage.
Create a static method called GetMain() on the Main class that would return the instance of Main (Main should be a singleton).
package whatever
{
public class Main
{
private static var _instance:Main = null;
public static function getMain():Main
{
return _instance;
}
// Main constructor
function Main(..):void
{
_instance = this;
}
}
}
To refer to the instance of Main() from your Menu class, you could use:
Main.getMain().gotoPage(5);
You want to do this with events. If your menu movieclip is a child of Main.as as you say, name the instance buttons inside of the menu movieclip, and set up the listeners in Main.as:
1) Put the below code in the constructor: public function Main(){...
menu.button_a.addEventListener(MouseEvent.CLICK, onButtonClick);
menu.button_b.addEventListener(MouseEvent.CLICK, onButtonClick);
2) and then write the onButtonClick function in Main.as
private function onButtonClick(e:MouseEvent):void{
switch(e.currentTarget.name){
case "button_a":
//call the Main.as function you want here
break;
case "button_b":
//call a different Main.as function
break;
}
ruedaminute's answer on dispatching events from the buttons and having main process those events is by far the best way to handle this, but there are many ways to do this in as3 - but try to use the aforementioned technique. Some of the other techniques.
Make a function in Main such as public function GotoPage(iPageNum:int):void{}
from a button - try this._parent.GotoPage(1);
but this._parent might not be main, do a trace(this._parent), and keep trying
it might end up being
this._parent._parent._parent.GotoPage(1) depending on your display tree hierachry.
Again, this is REALLY bad OOP practices, but well, it will work.
Another tecnique - use a singleton for main- looks like u already are - add that same public method, then from the button click, you could do Main.getMain().GotoPage(1);
That is a bit better, in that you can change the display tree and not have to figure out where the heck Main is in the display tree, but singletons also are discouraged for a variety of reasons, but in this case I would say it makes since.
Good Luck!
~ JT

Flash AS3 Best way to communicate independent classes?

what would be the simpliest way to communicate between independent classes?
I have been searching for two days and couldn't find nothing,
isn't there a way that one class has a dispatchEvent and the other an addEventListener, with a custom Event?? I could only find solutions within the same class o with a parentchild relationship, but what I'm looking for is like a "brother" relationship so to speak
thanks
In general, you want the class that dispatches the event to either extend EventDispatcher or implement IEventDispatcher. (All DisplayObjects do, so if your classes are DisplayObjects, you don't need to do anything extra.)
In the dispatching class:
class DispatchingClass extends Event Dispatcher {
function doSomething() {
// do stuff
dispatchEvent(new Event("FOO"));
}
}
In the listening class:
class ListeningClass {
function startListening(dispatcher:DispatchingClass) {
dispatcher.addEventListener("FOO", handleFoo);
}
function handleFoo(evt:Event) {
// do stuff
}
}
EventDispatchers work fine with custom events.
If for some reason your listening class doesn't have and can't get an instance of your dispatching class, you can make a global event broadcaster. Basically, make a universally-accessible class that extends EventDispatcher (or implements IEventDispatcher) and listens for and dispatches events to anything that tells it to.
Here is a bare-bones implementation of an event broadcaster:
import flash.events.EventDispatcher;
public class EventBroadcaster extends EventDispatcher {
private static var _instance:EventBroadcaster = new EventBroadcaster();
public function EventBroadcaster() {
if (_instance != null) {
trace ("Error: an instance of EventBroadcaster() already exists.");
}
}
public static function getInstance():EventBroadcaster {
return EventBroadcaster._instance;
}
}
You use it pretty much the same way:
class DispatchingClass {
function doSomething() {
// do something
EventBroadcaster.getInstance().dispatchEvent(new Event("FOO"));
}
}
class ListeningClass {
function startListening() {
EventBroadcaster.getInstance().addEventListener("FOO", handleFoo);
}
function handleFoo(evt:Event) {
// do stuff
}
}
dispatchEvent() and addEventListener() are just the functions from the built-in EventDispatcher.
There's some discussion at Event Broadcaster - Simple events solution... on ways to make an event broadcaster and how to add useful features. The article Centralized Event Management in Actionscript 2.0 has a good introduction to the concept.
So if you have ChildClass1, ChildClass2, and ParentClass, where ChildClass1 and ChildClass2 are both the children of ParenClass.
ChildClass1 will dispatch an event.
ParentClass will listen for this event, and then it's handler will update ChildClass2.
If you don't have a ParentClass, you could also use a ChildManagerClass that registers Childs and notifies them accordingly.