How to translate the namespace of AS3 to haxe? - actionscript-3

I want to translate AS3 code to haxe using AS3HX toolkit, but AS3HX doesn't know how to do that. Whether somebody can tell me how to deal with it?
namespace mx_internal;
using namespace mx_internal;
public myfun():void
{
mx_internal::setDocumentDescriptor();
}
mx_internal setDocumentDescriptor():void
{
...
...
}
private setDocumentDescriptor():void
{
...
...
}

Related

Override function in CS5 IDE -> Timeline -> Framescript

I have a baseclasse which implements an interface. I use this baseclass as my "template" ( read: Semantics, i'm not talking about Java/C++ Templates).
In my Flash CS5 IDE I want to override these interface methods.
Yes they are implemented in the base class, but trying to override them in framescript throws me( YES this might probably not be a clean design):
Symbol 'GameTest', Layer 'Layer 1', Frame 1, Line 1 1024:
Overriding a function that is not marked for override.
I don't exactly know in what scope framescripts work. And by framscript I just mean in the timeline frame 1.
in my base class:
public class MiniGameTemplate extends MovieClip implements IMiniGame
{
public function MiniGameTemplate()
{
}
public function update():void
{
}
}
In my Library object's first frame:
override function update():void
{
}
I'm using actionscript linkage to inherit my library object from the base class.
If I clear the framescript, it runs fine. No error.
When overriding a method, you must structure the overriding method exactly like the original. In this case you've missed the public access modifier statement.
Solution:
override public function update():void {
}

Getting Error when working with ActionScript Class and Interface in FLEX 3, ActionScript 3.0

I am currently new in ActionScript and I am creating an Interface and Class where class implements Interface.
I have created Interface and Class both in src/com folder. Here is the code what I did till now.
Interface
package com
{
public interface TestData
{
function getInput(str:String):void
function getOutput():String
}
}
Class
package com
{
public class EntityEL implements TestData
{
public var uname:String;
function getOutput():String
{
return uname;
}
function getInput(str:String):void
{
uname = str;
}
public function EntityEL()
{
}
}
}
mxml file
public var etn:EntityEL = new EntityEL();
public function btnClick():void
{
etn.getInput(value.text);
Alert.show(etn.getOutput());
}
<mx:Button label="Button Click" click="{btnClick();}" />
<mx:TextInput id="value" />
I am getting an error "1044: Interface method getInput in namespace com:TestData not implemented by class com:EntityEL."
Please Help me to solve this problem.
The idea of an interface is to define a contract between the caller and callee objects: Which methods can be accessed, which parameters are required, and what kind of data will be returned.
In order for that contract to make any sense, these methods have to be accessible, so that the "outside world" is allowed to call them.
When you omit access modifiers, the Flex compiler assumes internal as the default, which means that classes from within the same package have permission to access the methods - and so to some extent, this contract seems fulfilled. The same would be true for any other namespace.
Strangely enough, Adobe clearly doesn't allow it: Your method implementations have to be public.
However, you can declare your interface as internal, so that only classes from the package are allowed to implement it, and that leaves a way to keep your API internal, as well - if that was your intent.

How to cast a Proxy to an interface? (or tell Proxy to implement an interface)

I need ActionScript Proxy to be castable to a particular interface.
Here is an example without interface:
public dynamic class Tracer extends Proxy {
flash_proxy override function callProperty(method:*, ... args):* {
trace(method + " " + args)
}
}
var t:* = new Tracer()
t.sayHello("123") // prints: "sayHello [123]"
Now I need "t" to be of Talker type (don't ask why, I just love static typing):
public interface Talker {
function sayHello(s:String):void
}
var t:Talker = new Tracer() // throws class cast exception
t.sayHello("123")
The question is: how to cast a proxy?
For example, a solution for Java would be passing a list of interfaces when you create a new Proxy http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Proxy.html
Is it really possible with ActionScript 3?
That's unfortunately not possible in plain actionscript. But I think you can do it with the as commons bytecode API.
what about declaring public dynamic class Tracer extends Proxy implements Talker with all methods that need to be defined?

Mixin or Trait implementation in AS3?

I'm looking for ideas on how to implement a Mixin/Trait style system in AS3.
I want to be able to compose a number of classes together into a single object. Of course this is not a language level feature of AS3, but I'm hoping that there is maybe some way to do this using prototype based techniques or maybe some bytecode hacking that I believe AsMock uses to implement it's functionality.
An existing Java example is Qi4J where the user define interfaces that the Qi4j framework implements based on metadata tags and coding by convention.
Has anyone any ideas on how to get the Mixin/Trait concept working within AS3?
Zero solutions presented on this, so I looked into a few methods. There are ECMA script style mixins by adding methods defined on other objects to the base objects prototype. But this means that the advantages of static typing are gone.
I was looking for a solution that didn't sidestep the static type system. I knew that ASMock used bytecode injection to create proxy classes. I hacked around ASMock for the past few days and came up with a possible solution implemented by creating a class with composed classes (through bytecode injection).
From the users point of view this involves defining your object that uses mixins through many interfaces:
public interface Person extends RoomObject, Moveable
public interface RoomObject
{
function joinRoom(room:Room):void
function get room():Room
}
public interface Moveable
{
function moveTo(location:Point):void
function get location():Point
}
Then you define classes to represent these interfaces:
public class MoveableImpl implements Moveable
{
private var _location:Point = new Point()
public function get location():Point { return _location }
public function move(location:Point):void
{
_location = location.clone()
}
}
public class RoomObjectImpl implements RoomObject
{
private var _room:Room
public function get room():Room { return _room }
public function joinRoom(room:Room):void
{
_room = room
}
}
In a normal situation where you want to compose classes you would write:
public class PersonImpl implements Person
{
private var _roomObject:RoomObject = new RoomObjectImpl()
private var _moveable:Moveable = new MoveableImpl()
public function get room():Room { return _roomObject.room }
public function joinRoom(room:Room):void { _roomObject.joinRoom(room) }
public function get location():Point { return _moveable.location }
public function move(location:Point):void { _moveable.move(location) }
}
This is easily written using code due to it's regular layout. And that is exactly what my solution does, by injecting the equivilant bytecode into a new class. With this bytecode injection system we can create a Person object like so:
public class Main
{
private var mixinRepo:MixinRepository = new MixinRepository()
public function Main()
{
with(mixinRepo)
{
defineMixin(RoomObject, RoomObjectImpl) // associate interfaces with concreate classes
defineMixin(Moveable, MoveableImpl)
defineBase(Person)
prepare().completed.add(testMixins) // the injection is a async process, just liek in ASMock
}
}
private function testMixins():void
{
var person:Person = mixinRepo.create(Person)
var room:Room = new Room('room you can play in')
person.joinRoom(room)
trace('person.room:', person.room)
person.move(new Point(1, 2))
trace('person.location:', person.location)
}
}
At the moment this system is a proof of concept and is therefore very basic and brittle. But it shows that it is possible to come close to a Scala mixin/traits style system to AS3. I've made a github project to hold the code if anyone is interested in running the solution and poking around at how it was done.
A more complete example is given on the project wiki.
Look here, this works, mixes in methods and is simple.
http://github.com/specialunderwear/as3-mixin
o, and it works when you compile in as3 mode.
I found this one in Realaxy -- http://realaxy.com/

Friend methods/classes for AS3 packageless classes

Hi I'm wondering if I can have a packageless () AS3 class call a private method on the main class in the file. For example:
package demo
{
public class MyDemoClass
{
var helper:FriendlyHelperClass = new FriendlyHelperClass(this)
}
private function methodToCall():void
{
...
}
}
public class FriendlyHelperClass
{
public function FriendlyHelperClass(demo:MyDemoClass)
{
demo.methodToCall()
}
}
The call to methodToCall() from FriendlyHelperClass will fail as it is a private member of the MyDemoClass. Is there any way to call the methodToCall() method from the FriendlyHelperClass without extending MyDemoClass.
Basically I'm looking for inner class functionality that Java has or some sort of C++ style friend class.
Short answer : no.
You can never access a private member from outside a class in ActionScript. What you could do is use a namespace instead of a private scope. This would allow to give access to some members to selected classes. This is the closest of a friend class that you will get in AS3.
I'm afraid that is not possible, but if you make the class dynamic, then you can edit it while the program is running, and possibly add some useful functions to it, to access the private functions. I haven't tried it though.
Dynamic classes
Without testing the code, and knowing what your full problem. you can try passing the functions you need into the embedded class as a callback. e.g.,
helper.methodToCallCallback = this.methodToCall;
then inside FriendlyHelperClass:
this.methodToCallCallback();