NME for android view

142 views
Skip to first unread message

David Mouton

unread,
Dec 3, 2014, 10:37:38 AM12/3/14
to haxe...@googlegroups.com
Hi ,
I try to make android and ios module with Haxe.
It seems that nme can do that :
  • Android view. Instead of generating a full apk, NME can generate a self-contained fragment (jar, binary, java), which can be included in a larger project, which might contain native controls etc.
  • iOS view. NME can generate a library+header file that implements the UIViewController interface, which can then be linked into bigger iPhone applications.

So, i create a new project MyComponent.hx

package com.sakura;
import nme.display.Sprite;
import nme.events.Event;


class MyComponent extends Sprite {

   
var colour:Int;
   
var speed:Float;

   
public function new() {
       
super();
        colour
= 0xff0000;
        speed
= 1.0;
        addEventListener
(Event.ENTER_FRAME, onUpdate);
        redraw
();
   
}

   
function onUpdate(_)
   
{
        x
= x + speed;
       
if (x>stage.stageWidth-100)
            x
= 0;
   
}

   
function redraw()
   
{
       
var gfx = graphics;
        gfx
.clear();
        gfx
.beginFill(colour);
        gfx
.drawCircle(0,0,100);
        getTamere
();
   
}

   
public function setProperty(inName:String, inValue:String)
   
{
       
switch(inName)
       
{
           
case "y", "scaleX", "scaleY", "speed":
               
Reflect.setProperty(this, name, Std.parseFloat(inValue));
           
case "colour", "color":
               
if (inValue.substr(0,2)!="0x")
                    inValue
= "0x" + inValue;
                colour
= Std.parseInt(inValue);
                redraw
();
       
}
   
}

   
public function getTamere():Int{
       
return 42;
   
}

   
public static function main():Void{
        trace
('lalala');
   
}
}

I make a build.nmml :

<?xml version="1.0" encoding="utf-8"?>
<project>
 
 
<meta
     
title="NME View"
     
package="com.sakura"
     
version="1.0.0"
     
company="nme"
 
/>
 
 
<app
     
file="MyComponent"
     
main="com.sakura.MyComponent"
 
/>
 
 
<source path="src" />
 
 
<haxelib name="nme" />
 
</project>


And build them with

nme build build.nmml androidview

I copy Mycomponent_sdk.jar and armeabi folder into the libs folder of my Android project.
And i copy com/nmehost/MyComponent.java into my src folder

But i can't find taMere() on a MyComponent instance nor in MyComponent.java

What's the probleme ?

Thanks

whitetigle

unread,
Dec 3, 2014, 11:22:58 AM12/3/14
to haxe...@googlegroups.com
Are you serious? My mother ? "the answer to life, universe and everything"??!! And you can't find her??
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.


-- 
--
François Nicaise
https://www.linkedin.com/in/fnicaise

Freelance Game Developer / Designer 
Business Relations @ FrenchCows
Gaming & Business @ Bordeaux Games

whitetigle

unread,
Dec 3, 2014, 11:30:28 AM12/3/14
to haxe...@googlegroups.com
Still I can't help you. Sorry mate but your code was funny ;) ( for our english friends: 'tamere' means 'your mother' )

Daniel Glazman

unread,
Dec 3, 2014, 11:41:33 AM12/3/14
to haxe...@googlegroups.com
On 03/12/14 16:37, David Mouton wrote:

> But i can't find taMere() on a MyComponent instance nor in MyComponent.java


Well. taMere() is undefined, only getTamere() is defined in your
source code...

(and whitetigle is right about the naming, ahem...)

</Daniel<

Hugh

unread,
Dec 4, 2014, 12:03:49 AM12/4/14
to haxe...@googlegroups.com
Hi,
Once you copy the component "com/nmehost/MyComponent.java" to your src tree, this becomes part of your "SDK".  You will need to hand-edit this file to include "glue" functions for taking to the haxe code.
Care must be taken when writing the file, since the haxe code generally runs in a different thread.  In the example, "setProperty" uses the "sendToView" technique to make sure that the property is set in the correct (render) thread.  Alternatively, you could set it directly, but have the haxe code do the synchronization, because you can't get/set the DisplayObject properties (scale etc) from the non-render thread.  In your example, calling getTamere directly via "haxeCallbackObject.call0("getTamere")" should be ok since the function is thread-safe, but that will not generally be the case.
Haxe can also send values back to the view via JNI calls, but remember that these calls will not be made by java's GUI thread, so you probably want to use nme.Lib.postUICallback.
So you could use "sendToView" to send a request to your haxe code to send back the value.  The haxe code would then use "postUICallback" to send the value back, and when you receive the callback you will be on the UI thread again.
Also, I do not recommend that you have a "static main" function on your root object, because having one means that you are responsible for creating the root object and putting it on the stage.

Hugh

David Mouton

unread,
Dec 4, 2014, 5:47:31 AM12/4/14
to haxe...@googlegroups.com
Thank you for your help.

So, i edit MyComponent.java like that :

public int getTamere(){
     
return (Integer)haxeCallbackObject.call0("getTamere") ;
   
}


and try to call it from my MainActivity
com.nmehost.MyComponent c =new MyComponent();
       
String tamere = Integer.toString(c.getTamere());
       
Log.d( tamere,"value"  );

And i got a pretty NullPointerException because haxeCallbackObject is null in getTamere()
my component does not seem to be properly instantiated, maybe because it's also an Activity.

Anyway, In accordance with what I can read here https://github.com/haxenme/nme/tree/master/templates/android-view-test i add MyComponent as a Fragment

<fragment
       
android:layout_width="fill_parent"
       
android:layout_height="fill_parent"
       
android:name="com.nmehost.MyComponent"
       
android:id="@+id/fragment"
       
android:layout_marginTop="73dp"
       
android:layout_below="@+id/button"
       
android:layout_alignLeft="@+id/button"

       
android:layout_alignRight="@+id/button"
 
/>

And... when i test it on my phone ...

java.lang.UnsatisfiedLinkError: Couldn't load ApplicationMain from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.dmouton.myapplication-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.dmouton.myapplication-1, /vendor/lib, /system/lib]]]: findLibrary returned null

Aaaarrrrrggg it's make me crazy :/


David Mouton

unread,
Dec 4, 2014, 9:41:38 AM12/4/14
to haxe...@googlegroups.com
Yessssss i got it :D

I try the same thing with ADT insteadof  Android Studio and it's work !!!

I seem that Android Studio doesn't import *.so properly.

If you could, you see me doing a moon walk :)

Hugh

unread,
Dec 4, 2014, 11:22:47 PM12/4/14
to haxe...@googlegroups.com
Great stuff.
I wonder if you need to somehow get it to "scan" the libs directory again, or explicitly get it to include it in the project somehow.
The other issue might be to do with architectures - maybe it is looking for a x86 binary for emulation.

Hugh
Reply all
Reply to author
Forward
0 new messages