菜单滑动效果的实现
公共类 MenuScrollerActivity 扩展 BaseGameActivity 实现 IScrollDetectorListener、IOnSceneTouchListener、IClickDetectorListener { // ======================================== ======================= // 常量 // ====================== ======================================= protected static int CAMERA_WIDTH = 480;受保护静态 int CAMERA_HEIGHT = 320;受保护静态 int FONT_SIZE = 24;受保护的静态 int PADDING = 50;受保护的静态 int MENUITEMS = 7; // ================================================== =========== // 字段 // ==================================== ========================= 私有场景 mScene;私人相机mCamera;私人字体 mFont;私有 BitmapTextureAtlas mFontTexture;私有 BitmapTextureAtlas mMenuTextureAtlas;私有TextureRegion mMenuLeftTextureRegion;私有TextureRegion mMenuRightTextureRegion;私有精灵菜单左;私有精灵菜单权限; // 滚动 private SurfaceScrollDetector mScrollDetector;私有 ClickDetector mClickDetector;私有浮点数 mMinX = 0;私有浮点数 mMaxX = 0;私有浮点数 mCurrentX = 0;私有 int iItemClicked = -1;私有矩形滚动条; private List<TextureRegion> columns = new ArrayList<TextureRegion>(); // ================================================== =========== // 构造函数 // ==================================== ========================= // ======================= ====================================== // 获取器和设置器 // =================================================== ========= // ========================================= ==================== // 超类/接口的方法 // =================== ========================================== @Override public void onLoadResources() { //路径 FontFactory.setAssetBasePath("font/"); BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // 字体 this.mFontTexture = new BitmapTextureAtlas(256, 256); this.mFont = FontFactory.createFromAsset(this.mFontTexture, this, "Plok.TTF", FONT_SIZE, true, Color.BLACK); this.mEngine.getTextureManager().loadTextures(this.mFontTexture); this.mEngine.getFontManager().loadFonts(this.mFont); //菜单图像 for (int i = 0; i < MENUITEMS; i++) { BitmapTextureAtlas mMenuBitmapTextureAtlas = new BitmapTextureAtlas(256,256, TextureOptions.BILINEAR_PREMULTIPLYALPHA); TextureRegion mMenuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuBitmapTextureAtlas, this, "menu"+i+".png", 0, 0); this.mEngine.getTextureManager().loadTexture(mMenuBitmapTextureAtlas); columns.add(mMenuTextureRegion); } //菜单箭头的纹理 this.mMenuTextureAtlas = new BitmapTextureAtlas(128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mMenuLeftTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, this, "menu_left.png", 0, 0); this.mMenuRightTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, this, "menu_right.png",64, 0); this.mEngine.getTextureManager().loadTexture(mMenuTextureAtlas); } @Override public Engine onLoadEngine() { this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);最终 EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), this.mCamera); engineOptions.getTouchOptions().setRunOnUpdateThread(true);最终引擎引擎 = 新引擎(engineOptions);返回引擎; } @Override public Scene onLoadScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); this.mScene = new Scene(); this.mScene.setBackground(new ColorBackground(0, 0, 0)); this.mScrollDetector = new SurfaceScrollDetector(this); this.mClickDetector = new ClickDetector(this); this.mScene.setOnSceneTouchListener(this); this.mScene.setTouchAreaBindingEnabled(true); this.mScene.setOnSceneTouchListenerBindingEnabled(true);创建菜单框();返回this.mScene; } @Override public boolean onSceneTouchEvent(final Scene pScene, Final TouchEvent pSceneTouchEvent) { this.mClickDetector.onTouchEvent(pSceneTouchEvent); this.mScrollDetector.onTouchEvent(pSceneTouchEvent);返回真; } @Override public void onScroll(final ScrollDetector pScolDetector, Final TouchEvent pTouchEvent, Final float pDistanceX, Final float pDistanceY) { //禁用左右菜单箭头(15px 填充) if(mCamera.getMinX()<=15) menuleft. setVisible(假);否则 menuleft.setVisible(true); if(mCamera.getMinX()>mMaxX-15) menuright.setVisible(false);否则 menuright.setVisible(true); //如果到达终点则返回 if ( ((mCurrentX - pDistanceX) < mMinX) ){ return; }否则 if((mCurrentX - pDistanceX) > mMaxX){ return; } //相机以当前点为中心 this.mCamera.offsetCenter(-pDistanceX,0 ); mCurrentX -= pDistanceX; //设置滚动条与相机浮动 tempX =mCamera.getCenterX()-CAMERA_WIDTH/2; // 将%部分添加到位置 tempX+= (tempX/(mMaxX+CAMERA_WIDTH))*CAMERA_WIDTH; //设置位置scrollBar.setPosition(tempX,scrollBar.getY()); //设置左右箭头menuright.setPosition(mCamera.getCenterX()+CAMERA_WIDTH/2-menuright.getWidth(),menuright.getY()); menuleft.setPosition(mCamera.getCenterX()-CAMERA_WIDTH/2,menuleft.getY()); //因为Camera可以有负的X值,所以设置为0 if(this.mCamera.getMinX()<0){ this.mCamera.offsetCenter(0,0 ); mCurrentX=0; @Override public void onClick(ClickDetector pClickDetector, TouchEvent pTouchEvent) { loadLevel(iItemClicked); }; // ================================================== =========== // 方法 // ==================================== ========================= private void CreateMenuBoxes() { int spriteX = PADDING; int spriteY = 填充; //当前项目计数器 int iItem = 1; for (int x = 0; x < columns.size(); x++) { //触摸时,保存单击的项目,以防它是单击而不是滚动。最终 int itemToLoad = iItem; Sprite sprite = new Sprite(spriteX,spriteY,columns.get(x)){ public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, Final float pTouchAreaLocalX, Final float pTouchAreaLocalY) { iItemClicked = itemToLoad; }返回假; } }; iItem++; this.mScene.attachChild(精灵); this.mScene.registerTouchArea(sprite); spriteX += 20 + PADDING+sprite.getWidth(); mMaxX = spriteX - CAMERA_WIDTH; //设置滚动条的大小 float scrollbarsize = CAMERA_WIDTH/((mMaxX+CAMERA_WIDTH)/CAMERA_WIDTH);滚动条=新矩形(0,CAMERA_HEIGHT-20,滚动条大小,20);滚动条.setColor(1,0,0); this.mScene.attachChild(scrollBar); menuleft = new Sprite(0,CAMERA_HEIGHT/2-mMenuLeftTextureRegion.getHeight()/2,mMenuLeftTextureRegion); menuright = new Sprite(CAMERA_WIDTH-mMenuRightTextureRegion.getWidth(),CAMERA_HEIGHT/2-mMenuRightTextureRegion.getHeight()/2,mMenuRightTextureRegion); this.mScene.attachChild(menuright); menuleft.setVisible(false); this.mScene.attachChild(menuleft); } @Override public void onLoadComplete() { } //这里是调用项目加载的地方。 private void loadLevel(final int iLevel) { if (iLevel != -1) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MenuScrollerActivity.this, "加载项目" + String.valueOf( iLevel), Toast.LENGTH_SHORT).show(); iItemClicked = -1 } }); } }}以上就是本文的全部内容了,希望大家能够喜欢。