3

我正在使用 cocos2dx 制作 Android 动态壁纸,我已经成功地显示了动态壁纸,但我遇到了触摸问题。像 ccTouchBegan、ccTouchMoved 等与触摸相关的方法不会在 Android 上被调用。在 win32 上,我得到了触摸回调,但在 Android 上没有。谁能告诉我解决方案。对于动态壁纸,我在 .java 类中做了一些更改。我正在附加java类。任何帮助将不胜感激。

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


}
4

1 回答 1

0

只需在您的类中添加一个函数:

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

顺便说一句,你的布尔值rendererHasBeenSet是什么?

于 2014-01-15T08:15:43.333 回答