flex cairngorm addComand not work - actionscript-3

Hey everybody) I need help with the understanding of the framework Cairngorm for flex.
I code a simple application slider. I have a main class in which I have a tag
<fx:Declarations>
<control:AppController id="appController" />
</fx:Declarations>
Class itself AppController extends FrontController with constructor function:
public function AppController()
{
addCommand(SliderEvent.BUILD, SliderBuildCommand);
addCommand(SliderEvent.TRANSITION, SliderTransitionCommand);
}
and finally the code of SliderBuildCommand class:
public function SliderBuildCommand(){}
public function execute(event:CairngormEvent):void
{
config.loadSlides(this);
}
Unfortunately, in debugging, I see that the addition of the command didn't work out. This can be seen if set a breakpoint in "сonfig.loadSlides(this)" line. However, the command (addCommand) is processed.
Any idea why this is happening? Maybe I am a noob and I don't see the obvious :)

Instead of adding/mapping commands in AppController's constructor. Declare it in initialize() function. Example is given below for your reference.
public class AppController extends FrontController
{
public function AppController()
{
super();
}
public function initialize():void
{
this.addCommand(SliderEvent.BUILD, SliderBuildCommand);
this.addCommand(SliderEvent.TRANSITION, SliderTransitionCommand);
}
}

Related

AS3 - how to extend this function

This is first class with "gordz()" function
public class Model extends Object implements IModel
{
public static function gordz() : void
{
newobject = gallas.pop();
}
}
Now i try to override the function but i still want that old code is executed... How can i extend this function correctly?
public class LOL extends Model
{
override public static function gordz() : void
{
... //New code + execute old code
}
}
Neither super
You cannot use the super statement in a static method.
nor override
You cannot use the override attribute on any of the following:
[...]
Static methods
can be used in a static method.
Whatever you are trying to do should be accomplished in a different way.

Calling a function from within that class - php

I have a function called wsGO() inside a class ceff(). I want to be able to run that function from within the class ceff. I used this code:
class ceff{
$ceff_instance = new ceff($this);
$ceff_instance = wsGO();
public function wsGO(){
....
}
}
However this didn't work. Any ideas?
In PHP you can only initialize a property with a constant - NOT functions or constructor calls. You can though do that in a constructor, such as:
class ceff{
public $ceff_instance;
public function __construct() {
$this->ceff_instance = $this->wsGO();
}
public function wsGO(){
....
}
}

Using interfaces in Actionscript 3 properly

I have made an interface called IHero i implement in my hero.as3 class.
the hero class is written so it can be inheritted in a movieclip class to handle movement etc etc. But somehow i can't figure out how to code this with a good practice.
Maybe i am in the wrong direction.
I want to have a movieclip subclass, which will be a hero for instance.
Should i just implement the IHero in the hero class with the following methods, or is this to overkill? - I guess I am looking for an answer upon what should be in an interface and what should not. Here is the interface.
package com.interfaces
{
public interface IHero
{
//movement
function MoveLeft():void;
function MoveRight():void;
function MoveUp():void;
function MoveDown():void;
//in battle
function DoDamage(isCasting:Boolean):void;
function DoHeal():void;
function Flee():void;
function TakeDamage():void;
function IsAlive():Boolean;
function CheckDeath():void;
function Die():void;
}
}
I think you are on a right track, whether its the right one or the wrong one is always subjective. But if you do want to go down this road, I suggest you read this article by Mick West. Its a few years old, but its still very applicable.
I think you really have two distinct interfaces, but probably more
public interface IMoveable {
function moveLeft(obj:DisplayObject):void;
function moveRight(obj:DisplayObject):void;
function moveUp(obj:DisplayObject):void;
function moveDown(obj:DisplayObject):void;
function Flee(obj:DisplayObject);
}
public interface IFightable {
function doDamage(withWeapon:IEquipableWeapon);
function takeDamage(fromWeapon:IEquipableWeapon);
function get isAlive():Boolean;
function checkDeath():void;
function Die():void;
function doHeal();
function get health():Number;
}
Then....
public class SimpleMover implements IMoveable {
// The movement implementation
// for example:
public funciton moveLeft(obj:DisplayObject) {
obj.x = obj.x -= 10;
}
}
public class SimpleFighter implements IFightable {
// The fighting implementation
private var _health:Number = 100;
function doDamage(withWeapon:IEquipableWeapon) {
_health -= withWeapon.damage;
}
}
Then inject those into your subclass MovieClip for your Hero.
public class Hero extends MovieClip {
private var _mover:IMoveable;
private var _fighter:IFightable;
public function Hero(mover:IMoveable, fighter:IFightable) {
_mover = move;
_fighter = fighter;
}
}
Here you are using the Hero class as both Component Manager and Render component, which goes slightly against what West is talking about in the article, but I digress. But the idea is that your Manager (the Hero) becomes more or less an orchestrator, proxying calls back through to which ever component is applicable; calling methods on _mover and fighter to do your actual work.
There are several advantages using an approach like this, and some disadvantages. First, its more complex to set up; it requires you to really like about components and what each logical chunck of code is going to do. But on the other hand, it decouples your implementations from each other, makes it more testable, and reuseable. You can also swap out components at any time (compile-time or run-time for that matter), which gives you some flexability when creating new Entities.
But anyway, its just a suggestion of a slightly different paradigm that you seem to be flirting with. Maybe you'll get some mileage out of it. But definitely give the article a read, if you haven't already.
Also look into (like check out the API) for the Unity Engine which has a similar aggregation vs inheritance model where interfaces are key to abstraction.
Usually you will want a regular class definition that extends movieclip, then set your hero movieclip to use that class in the export for actionscript options. The interface is most likely not needed at all for your game.
Just to clarify - interfaces are implemented, not inherited. Therefore it does not matter what type of class you create that implements IHero. So to make a type of library class that implements Ihero, extend a display object like Sprite or MovieClip, and then use the implements keyword to indicate the class implements Ihero.
Add the Ihero methods, and you now have a display object class that implements iHero. It might look something like this (note no constructor function supplied)
package
{
import flash.display.Sprite;
import com.interfaces.IHero;
/**
* ...
* #author Zachary Foley
*/
public class MyDisplayObkect extends Sprite implements IHero
{
public function MoveLeft():void
{
// Add Implementation here;
}
public function MoveRight():void
{
// Add Implementation here;
}
public function MoveUp():void
{
// Add Implementation here;
}
public function MoveDown():void
{
// Add Implementation here;
}
//in battle
public function DoDamage(isCasting:Boolean):void
{
// Add Implementation here;
}
public function DoHeal():void
{
// Add Implementation here;
}
public function Flee():void
{
// Add Implementation here;
}
public function TakeDamage():void
{
// Add Implementation here;
}
public function IsAlive():Boolean
{
// Add Implementation here;
}
public function CheckDeath():void
{
// Add Implementation here;
}
public function Die():void
{
// Add Implementation here;
}
}
}

Can you create custom events on non-UIComponent based objects in Flex 3?

I have a simple object that get's geocoding data from the Google Maps API and I'd like it to dispatch a set of custom events depending on the response from Google. However, I don't have the addEventListener() and dispatchEvent() methods on this class because it doesn't extend UIComponent. Is there a way to create custom events in Flex 3 without inheriting from UIComponent?
Absolutely yes. To take advantage of event handling, your custom object should extend EventDispatcher:
public class MyClass extends EventDispatcher
{
// ...
public function myFunction():void
{
dispatchEvent(new Event("myEvent"));
}
}
This takes care of dispatching events.
Then as well, if you have the need, you can also create a separate class that extends Event, which gives you the option (among other things) of attaching event-specific data for use in the handler:
public class MyClass extends EventDispatcher
{
// ...
public function myFunction():void
{
dispatchEvent(new MyEvent(someData));
}
}
public class MyEvent extends Event
{
private var _myEventData:Object;
public function MyEvent(eventData:Object)
{
_myEventData = eventData;
}
}
.. so then in your handler, you'd just:
private function myHandler(event:MyEvent):void
{
trace(event.myEventData.toString());
}
Make sense?
Extend the EventDispatcher class, or implement the IEventDispatcher interface from the flash.events package.
Yes, that should be possible. Extend EventDispatcher like this:
import flash.events.*;
public class MyDispatcher extends EventDispatcher {
private function doIt(event:Event):void {
dispatchEvent(new Event("myEvent"));
}
That's exactly what I did! extended EventDispatcher and inherited dispatchEvent
Thanks lads

AS3 - Abstract Classes

How can I make an abstract class in AS3 nicely?
I've tried this:
public class AnAbstractClass
{
public function toBeImplemented():void
{
throw new NotImplementedError(); // I've created this error
}
}
public class AnConcreteClass extends AnAbstractClass
{
override public function toBeImplemented():void
{
// implementation...
}
}
But.. I don't like this way. And doesn't have compile time errors.
abstract classes are not supported by actionscript 3. see http://joshblog.net/2007/08/19/enforcing-abstract-classes-at-runtime-in-actionscript-3/
the above reference also provides a kind of hackish workaround to create abstract classes in as3.
Edit
also see http://www.kirupa.com/forum/showpost.php?s=a765fcf791afe46c5cf4c26509925cf7&p=1892533&postcount=70
Edit 2 (In response to comment)
Unfortunately, you're stuck with the runtime error. One alternative would be to have a protected constructor.... except as3 doesn't allow that either. See http://www.berniecode.com/blog/2007/11/28/proper-private-constructors-for-actionscript-30/ and http://gorillajawn.com/wordpress/2007/05/21/actionscript-3-%E2%80%93-no-private-constructor/.
You may Also find these useful: http://www.as3dp.com/category/abstract-classes/ and, in particular, http://www.as3dp.com/2009/04/07/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/
package
{
import flash.errors.IllegalOperationError;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public class AbstractClass
{
public function AbstractClass()
{
inspectAbstract();
}
private function inspectAbstract():void
{
var className : String = getQualifiedClassName(this);
if (getDefinitionByName(className) == AbstractClass )
{
throw new ArgumentError(
getQualifiedClassName(this) + "Class can not be instantiated.");
}
}
public function foo():void
{
throw new IllegalOperationError("Must override Concreate Class");
}
}
}
package
{
public class ConcreteClass extends AbstractClass
{
public function ConcreteClass()
{
super();
}
override public function foo() : void
{
trace("Implemented");
}
}
}
In AS3 would just use interfaces to make sure all functions are implemented at compile time.
I know it different but does the trick for an example such as the one above.
As long as they don't permit non-public constructors in actionscript, you'd have to rely on run time errors for abstract classes and singletons.