android - 如何在COCOS2D android中设置背景?

标签 android background cocos2d-android

我想设置一张图片,从一个URL下载到COCOS2D android,作为背景。
我的类(class)是 CCGLSurfaceView。这是我的代码。
我如何从 Internet 将图像设置为背景。

package org.cocos2d.opengl;


public class CCGLSurfaceView extends GLSurfaceView {
private static final int VIEWID = 0x1235;
// private static final String LOG_TAG = CCGLSurfaceView.class.getSimpleName();
private CCTouchDispatcher mDispatcher;

public CGSize frame;

public CCGLSurfaceView(Context context) {
    super(context);

    CCDirector.theApp = (Activity) context;

    mDispatcher = CCTouchDispatcher.sharedDispatcher();

    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setId(VIEWID);

    // add this to resolve Samsung's Galaxy opengl problem
    //  here for reference.
    // http://www.anddev.org/samsung_galaxy_odd_ogl_es_hardware_acceleration_resolved-t8511.html
    /* need a real machine to test
    this.setEGLConfigChooser(
            new GLSurfaceView.EGLConfigChooser() {
                public EGLConfig chooseConfig(EGL10 egl,EGLDisplay display) {
                    int[] attributes=new int[]{
                            //EGL10.EGL_RED_SIZE,
                            //5,
                            //EGL10.EGL_BLUE_SIZE,
                            //5,
                            //EGL10.EGL_GREEN_SIZE,
                            //6,
                            EGL10.EGL_DEPTH_SIZE,
                            16,
                            EGL10.EGL_NONE
                    };
                    EGLConfig[] configs=new EGLConfig[1];
                    int[] result=new int[1];
                    egl.eglChooseConfig(display,attributes,configs,1,result);
                    return configs[0];
                }
            }
    );*/
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    frame = CGSize.make(right - left, bottom - top);
}        

@Override
public boolean onTouchEvent(MotionEvent event) {

    mDispatcher.queueMotionEvent(event);

//      switch (event.getAction()) {
//      case MotionEvent.ACTION_CANCEL:
//          mDispatcher.touchesCancelled(event);
//          break;
//      case MotionEvent.ACTION_DOWN:
//          mDispatcher.touchesBegan(event);
//          break;
 //     case MotionEvent.ACTION_MOVE:
//          mDispatcher.touchesMoved(event);
  //            break;
//      case MotionEvent.ACTION_UP:
//          mDispatcher.touchesEnded(event);
//          break;
//      }

    synchronized (CCDirector.sharedDirector()) {
        try {
            CCDirector.sharedDirector().wait(20L);
        } catch (InterruptedException e) {
            // Do nothing
        }
    }

    return true;
}
}

最佳答案

要设置背景图片,您需要在构造函数中定义CCSprite,将标记设置为1,并将 anchor 值设置为(0,0)

代码如下:

CCSprite background = CCSprite.sprite("Background.jpeg");
background.setTag(1);
background.setAnchorPoint(0, 0);
addChild(background);

关于android - 如何在COCOS2D android中设置背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8427793/

相关文章:

android - 如何在android中做翻页/翻页/ curl 效果

java - 在 cocos2d android 中刷新 Sprite

android - 选择前置摄像头应用程序崩溃,如何解决?

android - Gson根据字段名自定义反序列化逻辑

CSS背景重复问题

android - android中的cocos2d游戏只显示黑屏

android - 在 iphone 4 android 4.1 上平滑的 CSS3 转换

android - 条形码和二维码扫描不适用于CameraX/ZXing

css - 移动设备 - 不同的背景 - 媒体查询不工作

java - JFrame 背景图片