2013-09-30 176 views
3

我正在使用cocos2dx在Android動態壁紙上工作,我已成功設法顯示動態壁紙,但我面臨觸摸問題。與觸摸相關的方法,如ccTouchBegan,ccTouchMoved不會在Android上調用。在win32上我得到了觸摸回調,但不是在Android上。任何人都可以告訴我解決方案。 對於動態壁紙,我已經在.java類中做了一些改變。我附加了java類。 任何幫助將不勝感激。Cocos2dx Android動態壁紙

/**************************************************************************** 
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); 
     } 
    } 


} 

回答

0

一個簡單的功能添加到您的類:

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

通過你有什麼布爾rendererHasBeenSet的方法是什麼?