On-Screen Controls

  ... information about changes and new features of AndEngine,

On-Screen Controls

Postby Nicolas Gramlich » Sun Jul 11, 2010 11:54 am

Hello Community,

I couldn't resist implementing some On-Screen Controls :)

Example-Code:

Hope this helps some of you :)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: On-Screen Controls

Postby Radzell » Sun Jul 11, 2010 4:34 pm

Thank you this is exactly what i need i made buttons but they where horrible now all i need if you to make a sample program on adding controls a score, a username, and other options to the HUD to they follow the chase camera then i will be almost done.
Radzell
 
Posts: 465
Joined: Sun Jul 04, 2010 8:52 am

Re: On-Screen Controls

Postby Nicolas Gramlich » Sun Jul 11, 2010 4:46 pm

Hi Andrew,

Unfortunately I (currently) don't have the time to do exactly what you asked for, BUT....
I made a little game this morning - a Snake Clone, which is actually playable :) :controller:

It has On-Screen-Controls (which extend from HUD), a Score, but no ChaseCamera.
Snake-Sources:


Hope this helped for now :)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: On-Screen Controls

Postby Paul » Sun Jul 11, 2010 11:20 pm

Nicolas.. you're awesome :D thanks for doing this, saved me a lot of time!
Paul
 
Posts: 92
Joined: Sat Jun 26, 2010 3:00 pm

Re: On-Screen Controls

Postby Radzell » Mon Jul 12, 2010 2:00 am

I tried addding two buttons to scene.setchild() and only one showed update the i added the two analog sticks to one scene and the background is all black is there a way of puttting two analog controls in one scene.
Radzell
 
Posts: 465
Joined: Sun Jul 04, 2010 8:52 am

Re: On-Screen Controls

Postby Nicolas Gramlich » Mon Jul 12, 2010 9:35 am

Opps that was a little bug. andengine.jar is fixed now.
Example on how to do two Controls: http://code.google.com/p/andengineexamp ... ample.java

Multitouch is NOT yet supported :!:
If somebody has experience with MultiTouch on Android (that doesn't cause problems to prior 2.X devices), let me know.

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: On-Screen Controls

Postby Paul » Mon Jul 12, 2010 1:35 pm

Ok, with the help of http://blog.javia.org/android-application-backwards-compatibility/ I implemented multitouch which should work on all sdk versions.

Here's the code (onTouch listener on a SurfaceView):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                        
  2. public boolean onTouch(View v, MotionEvent event) {
  3.    input = event;                              
  4.    String sdkVersion = Build.VERSION.SDK;
  5.    Log.d(Build.VERSION.SDK, " ");
  6.                                
  7.    if(sdkVersion.equals("3") || sdkVersion.equals("4")){
  8.         // do single touch event, or notify that there is no multitouch available
  9.    } else {
  10.         // say its ok to use multitouch, or if this code is to be used in the
  11.         // ontouch listener, do:
  12.         float pointers[] = MultitouchWrapper.getPointerLocations(event);
  13.         Log.d(pointers[0] + " " + pointers[1], pointers[2] + " " + pointers[3]);
  14.    }
  15.  
  16.    return true;
  17. }
  18.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


and now the MultitouchWrapper class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MultitouchWrapper {
  2.  
  3.         static float[] getPointerLocations(MotionEvent event) {
  4.  
  5.                 float pointers[] = new float[4];
  6.  
  7.                 //Log.d(event.getPointerCount() + " pointers", " ");
  8.                
  9.                 switch (event.getPointerCount()) {
  10.                 case 1:
  11.                         pointers[0] = event.getX(0);
  12.                         pointers[1] = event.getY(0);
  13.                         pointers[2] = -1;
  14.                         pointers[3] = -1;
  15.                         break;
  16.                 case 2:
  17.                         pointers[0] = event.getX(0);
  18.                         pointers[1] = event.getY(0);
  19.                         pointers[2] = event.getX(1);
  20.                         pointers[3] = event.getY(1);
  21.                         break;
  22.                 }
  23.  
  24.                 return pointers;
  25.         }
  26. }
  27.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I guess you could add a "convenience" layer to the multitouch wrapper class, where the wrapper would return an object, say a PointerLocations class, where an instance called "pl" would have fields like pl.x1, pl.x2, pl.areMoreFingersPressed() etc.

I tested this out on a G1 and N1, and it works like a charm. However, the build target sdk of the project has to be 2.0+, and the manifest has to contain <uses-sdk android:minSdkVersion="3" /> for it to run on a G1.

Hope this helped :)
Paul
Paul
 
Posts: 92
Joined: Sat Jun 26, 2010 3:00 pm

Re: On-Screen Controls

Postby Nicolas Gramlich » Mon Jul 12, 2010 3:41 pm

Ah you got me, I created a branch where I'll test how this works.

But I'm not sure when I have the time to do so :( (Weekend probably)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: On-Screen Controls

Postby Christoph » Tue Jul 13, 2010 2:22 am

Christoph
 
Posts: 4
Joined: Wed Jun 09, 2010 10:52 pm

Re: On-Screen Controls

Postby Nicolas Gramlich » Tue Jul 13, 2010 10:00 am

Thanks Chris, found that one tonight via Twitter.

I still have mixed feelings, building against a higher apiLevel :? . I might accidentally use one method that would cause crashes on 1.6 devices :(

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Next

Return to Updates

Who is online

Users browsing this forum: No registered users and 3 guests