2013-09-30 4 views
3

Je travaille sur Android Live Wallpaper en utilisant cocos2dx, j'ai réussi à afficher le fond d'écran en direct, mais je suis confronté à un problème avec le toucher. Les méthodes liées au toucher comme ccTouchBegan, ccTouchMoved ne sont pas appelées sur Android. Sur win32 je reçois le callback tactile mais pas sur Android. Quelqu'un peut-il me dire la solution pour cela? Pour le fond d'écran en direct j'ai fait quelques changements dans la classe .java. Je joins la classe Java. Toute aide sera appréciée.Cocos2dx Android Live Wallpaper

/**************************************************************************** 
Copyright (c) 2010-2012 cocos2d-x.org 

http://www.cocos2d-x.org 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE. 
****************************************************************************/ 
package com.live.MyClass; 

import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 
import org.cocos2dx.lib.Cocos2dxGLSurfaceView; 
import org.cocos2dx.lib.Cocos2dxHelper; 
import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener; 
import org.cocos2dx.lib.Cocos2dxRenderer; 
import android.view.MotionEvent; 
import android.content.Context; 
import android.opengl.GLSurfaceView.Renderer; 
import android.service.wallpaper.WallpaperService; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.View; 
import android.view.View.OnTouchListener; 

/*public class MyClass extends Cocos2dxActivity{ 

    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
    } 

    static { 
     System.loadLibrary("game"); 
    } 
}*/ 

public class MyClass extends WallpaperService{ 
    static { 
     System.loadLibrary("game"); 
    } 
    @Override 
    public Engine onCreateEngine() 
    { 
     Log.d("MyClass", "onCreateEngine"); 
     return new MyWallpaperEngine(); 
    } 


    protected class MyWallpaperEngine extends Engine implements Cocos2dxHelperListener 
    { 
     public class MyGLSurfaceView extends Cocos2dxGLSurfaceView 
     { 


      MyGLSurfaceView(Context context) 
      { 
       super(context); 
       Log.d("MyClass", "MyGLSurfaceView YYYY"); 
       this.setFocusableInTouchMode(true); 

      } 


      @Override 
      public SurfaceHolder getHolder() 
      { 

       Log.d("MyClass", "getHolder &&&&&"); 
       return getSurfaceHolder(); 
      } 

      public void onDestroy() 
      { 
       Log.d("MyClass", "MyGLSurfaceView onDestroy"); 
       super.onDetachedFromWindow(); 
      } 




     } 

     protected class MyGLRenderer extends Cocos2dxRenderer 
     { 
       //prevent the cocos2dxRenderer from calling nativeInit 
         @Override 
       public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig) { 
       } 

       @Override 
       public void onSurfaceChanged(final GL10 pGL10, final int pWidth, final int pHeight) { 
        Log.d("MyClass", "MyGLRenderer onSurfaceChanged"); 
         setScreenWidthAndHeight(pWidth, pHeight); 
         super.onSurfaceCreated(null, null); 
       } 

     } 

     private MyGLSurfaceView mGLSurfaceView; 
     //private Cocos2dxGLSurfaceView mGLSurfaceView; 
     private boolean rendererHasBeenSet;    

     @Override 
     public void onCreate(SurfaceHolder surfaceHolder) 
     { 
      super.onCreate(surfaceHolder); 
      setTouchEventsEnabled(true); 
      mGLSurfaceView = new MyGLSurfaceView(MyClass.this); 
      // mGLSurfaceView = new Cocos2dxGLSurfaceView(MyClass.this); 
      mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 


      //mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer()); 
      Log.d("MyClass", "MyGLRenderer onCreate"); 
      setRenderer(getNewRenderer()); 
      Cocos2dxHelper.init(MyClass.this, this); 
     } 

     @Override 
     public void onVisibilityChanged(boolean visible) 
     { 
      super.onVisibilityChanged(visible); 
      Log.d("MyClass", "onVisibilityChanged ::"+visible); 
      if (visible) 
      { 

       Cocos2dxHelper.onResume(); 
       mGLSurfaceView.onResume(); 
      } 
      else 
      { 
       Cocos2dxHelper.onPause(); 
       mGLSurfaceView.onPause();    
      } 
     } 

     @Override 
     public void onDestroy() 
     { 
      super.onDestroy(); 

      Cocos2dxHelper.end(); 
      Log.d("MyClass", "onDestroy "); 
      mGLSurfaceView.onDestroy(); 
     } 

       protected void setRenderer(Renderer renderer) { 
        Log.d("MyClass", "setRenderer"); 
         mGLSurfaceView.setCocos2dxRenderer((Cocos2dxRenderer) renderer); 
         rendererHasBeenSet = true; 
       } 

       Renderer getNewRenderer(){ 
         Cocos2dxRenderer renderer = new MyGLRenderer(); 
         return renderer; 
       } 

     @Override 
     public void showDialog(String pTitle, String pMessage) 
     { 
     } 

     @Override 
     public void showEditTextDialog(String pTitle, String pMessage, int pInputMode, int pInputFlag, int pReturnType, int pMaxLength) 
     { 
     } 

     @Override 
     public void runOnGLThread(Runnable pRunnable) 
     { 
      mGLSurfaceView.queueEvent(pRunnable); 
     } 
    } 


} 

Répondre

0

Ajoutez simplement une fonction à votre classe:

@Override 
    public void onTouchEvent(final MotionEvent pMotionEvent) 
    { 
     mGLSurfaceView.onTouchEvent(pMotionEvent); 
    } 

Par ailleurs, quelle est votre booléenne rendererHasBeenSet pour?