code到BaseGameActivity

code到BaseGameActivity

本文介绍了从转换的SimpleBaseGameActivity code到BaseGameActivity AndEndgine GLES2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我是相当新的AndEngine的Andr​​oid编程,我不知道如何我SimpleBaseGameActivity code转换为BaseGameActivity。我想将它转换为符合教程中我对BaseGameActivity飞溅+菜单教程中,这样我就可以合并这一切。

这里的code河内游戏之塔:

 公共类MainActivity扩展SimpleBaseGameActivity {
私人ITexture main_font_texture;
文字countText;
字体main_font;
INT Movescount为;
一幕一幕=新场景();
私有静态诠释CAMERA_WIDTH = 800;
私有静态诠释CAMERA_HEIGHT = 480;私人ITextureRegion mBackgroundTextureRegion,mTowerTextureRegion,mRing1,mRing2,mRing3;
私人堆叠式和LT;环> mStack1,mStack2,mStack3;
私人雪碧mTower1,mTower2,mTower3;@覆盖公共EngineOptions onCreateEngineOptions(){
    最终的摄像头摄像头=新相机(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
    返回新EngineOptions(真,ScreenOrientation.LANDSCAPE_FIXED,新RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),摄像头);
}@覆盖保护无效onCreateResources(){
    尝试{         main_font = FontFactory.create(this.getFontManager(),this.getTextureManager(),256,256,TextureOptions.BILINEAR_ preMULTIPLYALPHA,Typeface.DEFAULT,60,真实,Color.BLACK_ABGR_PACKED_INT);
         main_font.load();
        ITexture backgroundTexture =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){
            @覆盖
            公众的InputStream的open()抛出IOException
                返回getAssets()开(GFX / background.png)。
            }
        });        ITexture towerTexture =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){            @覆盖
            公众的InputStream的open()抛出IOException
                返回getAssets()开(GFX / tower.png)。
            }
        });        ITexture RING1 =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){            @覆盖
            公众的InputStream的open()抛出IOException
                返回getAssets()开(GFX / ring1.png)。
            }
        });        ITexture分享帮助=新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){            @覆盖
            公众的InputStream的open()抛出IOException
                返回getAssets()开(GFX / ring2.png)。
            }
        });
        ITexture的ring3 =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){            @覆盖
            公众的InputStream的open()抛出IOException
                返回getAssets()开(GFX / ring3.png)。
            }
        });
        backgroundTexture.load();
        towerTexture.load();
        ring1.load();
        ring2.load();
        ring3.load();        this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
        this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
        this.mRing1 = TextureRegionFactory.extractFromTexture(RING1);
        this.mRing2 = TextureRegionFactory.extractFromTexture(分享帮助);
        this.mRing3 = TextureRegionFactory.extractFromTexture(ring3的);        this.mStack1 =新的堆栈<环>();
        this.mStack2 =新的堆栈<环>();
        this.mStack3 =新的堆栈<环>();
    }赶上(IOException异常五){
        Debug.e(E);
    }
}@覆盖
保护场景onCreateScene(){    精灵backgroundSprite =新精灵(0,0,this.mBackgroundTextureRegion,getVertexBufferObjectManager());    mTower1 =新的雪碧(0.241f * CAMERA_WIDTH,0.133f * CAMERA_HEIGHT,this.mTowerTextureRegion,getVertexBufferObjectManager());
    mTower2 =新的雪碧(0.5F * CAMERA_WIDTH,0.133f * CAMERA_HEIGHT,this.mTowerTextureRegion,getVertexBufferObjectManager());
    mTower3 =新的雪碧(0.756f * CAMERA_WIDTH,0.133f * CAMERA_HEIGHT,this.mTowerTextureRegion,getVertexBufferObjectManager());    环RING1 =新的圆环(1,139,174,this.mRing1,getVertexBufferObjectManager()){        @覆盖
        公共布尔onAreaTouched(的TouchEvent pSceneTouchEvent,浮pTouchAreaLocalX,浮pTouchAreaLocalY){
            如果(this.getmStack()。偷看()getmWeight。()!= this.getmWeight())
                返回false;
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,pSceneTouchEvent.getY() - this.getHeight()/ 2);
            如果(pSceneTouchEvent.getAction()== TouchEvent.ACTION_UP){
                checkForCollisionsWithTowers(本);
            }
            返回true;
        }
    };
    环分享帮助=新的圆环(2,118,212,this.mRing2,getVertexBufferObjectManager()){        @覆盖
        公共布尔onAreaTouched(的TouchEvent pSceneTouchEvent,浮pTouchAreaLocalX,浮pTouchAreaLocalY){
            如果(this.getmStack()。偷看()getmWeight。()!= this.getmWeight())
                返回false;
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,pSceneTouchEvent.getY() - this.getHeight()/ 2);
            如果(pSceneTouchEvent.getAction()== TouchEvent.ACTION_UP){
                checkForCollisionsWithTowers(本);
            }
            返回true;
        }
    };
    环ring3的=新环(3,97,255,this.mRing3,getVertexBufferObjectManager()){        @覆盖
        公共布尔onAreaTouched(的TouchEvent pSceneTouchEvent,浮pTouchAreaLocalX,浮pTouchAreaLocalY){
            如果(this.getmStack()。偷看()getmWeight。()!= this.getmWeight())
                返回false;
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,pSceneTouchEvent.getY() - this.getHeight()/ 2);
            如果(pSceneTouchEvent.getAction()== TouchEvent.ACTION_UP){
                checkForCollisionsWithTowers(本);
            }
            返回true;
        }
    };    this.mStack1.add(ring3的);
    this.mStack1.add(分享帮助);
    this.mStack1.add(RING1);    ring1.setmStack(mStack1);
    ring2.setmStack(mStack1);
    ring3.setmStack(mStack1);
    ring1.setmTower(mTower1);
    ring2.setmTower(mTower1);
    ring3.setmTower(mTower1);    scene.attachChild(backgroundSprite);
    scene.attachChild(mTower1);
    scene.attachChild(mTower2);
    scene.attachChild(mTower3);
    scene.attachChild(RING1);
    scene.attachChild(分享帮助);
    scene.attachChild(ring3的);
    scene.registerTouchArea(RING1);
    scene.registerTouchArea(分享帮助);
    scene.registerTouchArea(ring3的);    scene.setTouchAreaBindingOnActionDownEnabled(真);   countText =新文本(0,0,main_font,移动+ Movescount为,10,this.getVertexBufferObjectManager());
     countText.setPosition(0,0);
     scene.attachChild(countText);
    返回现场;
}
私人无效checkForCollisionsWithTowers(铃铃){
    堆叠式和LT;环>堆栈=无效;
    雪碧塔= NULL;    如果(ring.collidesWith(mTower1)及及(mStack1.size()== 0 || ring.getmWeight()&所述; mStack1.peek()getmWeight())){
        堆栈= mStack1;
        塔= mTower1;
        Movescount为++;
        countText.setText(移动+ Movescount为);    }否则如果(ring.collidesWith(mTower2)及及(mStack2.size()== 0 || ring.getmWeight()&所述; mStack2.peek()getmWeight())){
        堆栈= mStack2;
        塔= mTower2;
        Movescount为++;
        countText.setText(移动+ Movescount为);
    }否则如果(ring.collidesWith(mTower3)及及(mStack3.size()== 0 || ring.getmWeight()&所述; mStack3.peek()getmWeight())){
        堆栈= mStack3;
        塔= mTower3;
        Movescount为++;
        countText.setText(移动+ Movescount为);    }其他{
        堆栈= ring.getmStack();
        塔= ring.getmTower();
    }
    。ring.getmStack()删除(环);
    如果(堆栈= NULL&放大器;!&安培;塔= NULL&放大器;!&安培; stack.size()== 0){
        ring.setPosition(tower.getX()+ tower.getWidth()/ 2 - ring.getWidth()/ 2,tower.getY()+ tower.getHeight() - ring.getHeight());    }否则如果(堆栈=空&放大器;!&放大器;塔=空&放大器;!&放大器; stack.size()大于0){
        ring.setPosition(tower.getX()+ tower.getWidth()/ 2 - ring.getWidth()/ 2,stack.peek()的getY() - ring.getHeight());    }
    stack.add(圈);
    ring.setmStack(栈);
    ring.setmTower(塔);
    isGameOver();
}私人无效isGameOver(){    如果(mStack3.size()== 3){         字体main_font = FontFactory.create(this.getFontManager(),this.getTextureManager(),256,256,TextureOptions.BILINEAR_ preMULTIPLYALPHA,Typeface.DEFAULT,60,真实,Color.BLACK_ABGR_PACKED_INT);
         main_font.load();         文字levelCompleteText =新文本(0,0,main_font,等级完成!,this.getVertexBufferObjectManager());
         levelCompleteText.setPosition(CAMERA_WIDTH / 2 - levelCompleteText.getWidth()/ 2,CAMERA_HEIGHT / 2 - levelCompleteText.getHeight()/ 2);
         scene.attachChild(levelCompleteText);
         scene.clearTouchAreas();
   }
}
    }

下面是为Ring.java:

  @燮pressWarnings(未使用)
公共类环扩展Sprite {私人诠释mWeight;
私人堆叠式和LT;环> MSTACK; //这个重新presents这戒指属于栈
私人雪碧mTower;公共戒指(INT重,浮像素,浮PY,ITextureRegion pTextureRegion,VertexBufferObjectManager pVertexBufferObjectManager){
    超(PX,PY,pTextureRegion,pVertexBufferObjectManager);
    this.mWeight =体重;
}公众诠释getmWeight(){
    返回mWeight;
}公共堆栈<环> getmStack(){
    返回MSTACK;
}公共无效setmStack(堆栈<环> MSTACK){
    this.mStack = MSTACK;
}公共雪碧getmTower(){
    返回mTower;
}公共无效setmTower(雪碧mTower){
    this.mTower = mTower;
}
}


解决方案

 包org.farook.towerofhanoi;进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.util.Stack中;进口org.andengine.engine.camera.Camera;
进口org.andengine.engine.handler.timer.ITimerCallback;
进口org.andengine.engine.handler.timer.TimerHandler;
进口org.andengine.engine.options.EngineOptions;
进口org.andengine.engine.options.ScreenOrientation;
进口org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
进口org.andengine.entity.scene.Scene;
进口org.andengine.entity.sprite.Sprite;
进口org.andengine.entity.text.Text;
进口org.andengine.input.touch.TouchEvent;
进口org.andengine.opengl.font.Font;
进口org.andengine.opengl.font.FontFactory;
进口org.andengine.opengl.texture.ITexture;
进口org.andengine.opengl.texture.TextureOptions;
进口org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
进口org.andengine.opengl.texture.bitmap.BitmapTexture;
进口org.andengine.opengl.texture.bitmap.BitmapTextureFormat;
进口org.andengine.opengl.texture.region.ITextureRegion;
进口org.andengine.opengl.texture.region.TextureRegionFactory;
进口org.andengine.ui.activity.BaseGameActivity;
进口org.andengine.util.adt.io.in.IInputStreamOpener;
进口org.andengine.util.color.Color;
进口org.andengine.util.debug.Debug;进口android.graphics.Typeface;公共类TowerOfHanoiActivity扩展BaseGameActivity {
    私有静态诠释CAMERA_WIDTH = 800;
    私有静态诠释CAMERA_HEIGHT = 480;
    私人ITextureRegion mBackgroundTextureRegion,mTowerTextureRegion,mRing1,mRing2,mRing3;
    私人雪碧mTower1,mTower2,mTower3;
    私人堆叠式和LT;环> mStack1,mStack2,mStack3;
      文字countText;
      字体main_font;
      INT Movescount为;
    一幕一幕;
    @覆盖
    公共EngineOptions onCreateEngineOptions(){
        最终的摄像头摄像头=新相机(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
        返回新EngineOptions(真,ScreenOrientation.LANDSCAPE_FIXED,新RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),摄像头);
    }
    @覆盖
    公共无效onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)抛出异常{        尝试{
            main_font = FontFactory.create(this.getFontManager(),this.getTextureManager(),256,256,BitmapTextureFormat.RGBA_8888,TextureOptions.BILINEAR_ preMULTIPLYALPHA,Typeface.DEFAULT,60,真实,Color.BLACK_ABGR_PACKED_INT);
            main_font.load();
             BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(GFX /);            // 1 - 设置位图纹理
            ITexture backgroundTexture =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){
                @覆盖
                公众的InputStream的open()抛出IOException
                    返回getAssets()开(GFX / background.png)。
                }
            });
            ITexture towerTexture =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){
                @覆盖
                公众的InputStream的open()抛出IOException
                    返回getAssets()开(GFX / tower.png)。
                }
            });
            ITexture RING1 =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){
                @覆盖
                公众的InputStream的open()抛出IOException
                    返回getAssets()开(GFX / ring1.png)。
                }
            });
            ITexture分享帮助=新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){
                @覆盖
                公众的InputStream的open()抛出IOException
                    返回getAssets()开(GFX / ring2.png)。
                }
            });
            ITexture的ring3 =新BitmapTexture(this.getTextureManager(),新IInputStreamOpener(){
                @覆盖
                公众的InputStream的open()抛出IOException
                    返回getAssets()开(GFX / ring3.png)。
                }
            });
            // 2 - 加载位图纹理到VRAM
            backgroundTexture.load();
            towerTexture.load();
            ring1.load();
            ring2.load();
            ring3.load();
            // 3 - 设置纹理区域
            this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
            this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
            this.mRing1 = TextureRegionFactory.extractFromTexture(RING1);
            this.mRing2 = TextureRegionFactory.extractFromTexture(分享帮助);
            this.mRing3 = TextureRegionFactory.extractFromTexture(ring3的);
            // 4 - 创建堆栈
            this.mStack1 =新的堆栈<环>();
            this.mStack2 =新的堆栈<环>();
            this.mStack3 =新的堆栈<环>();
        }赶上(IOException异常五){
            Debug.e(E);
        }        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }    @覆盖
    公共无效onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)抛出异常{        // 1 - 创建新场景
                现场=新场景();
                精灵backgroundSprite =新精灵(0,0,this.mBackgroundTextureRegion,getVertexBufferObjectManager());
                scene.attachChild(backgroundSprite);                // 2 - 添加塔
                mTower1 =新的雪碧(192,63,this.mTowerTextureRegion,getVertexBufferObjectManager());
                mTower2 =新的雪碧(400,63,this.mTowerTextureRegion,getVertexBufferObjectManager());
                mTower3 =新的雪碧(604,63,this.mTowerTextureRegion,getVertexBufferObjectManager());                mTower1.setTag(1);
                mTower2.setTag(2);
                mTower3.setTag(3);                scene.attachChild(mTower1);
                scene.attachChild(mTower2);
                scene.attachChild(mTower3);                // 3 - 创建环
                环RING1 =新的圆环(1,139,174,this.mRing1,getVertexBufferObjectManager()){
                    @覆盖
                    公共布尔onAreaTouched(的TouchEvent pSceneTouchEvent,浮pTouchAreaLocalX,浮pTouchAreaLocalY){
                        如果(((环)this.getmStack()。偷看())。getmWeight()!= this.getmWeight())
                            返回false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,pSceneTouchEvent.getY() - this.getHeight()/ 2);
                        如果(pSceneTouchEvent.getAction()== TouchEvent.ACTION_UP){
                            checkForCollisionsWithTowers(本);
                        }
                        返回true;
                    }
                };
                环分享帮助=新的圆环(2,118,212,this.mRing2,getVertexBufferObjectManager()){
                    @覆盖
                    公共布尔onAreaTouched(的TouchEvent pSceneTouchEvent,浮pTouchAreaLocalX,浮pTouchAreaLocalY){
                        如果(((环)this.getmStack()。偷看())。getmWeight()!= this.getmWeight())
                            返回false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,pSceneTouchEvent.getY() - this.getHeight()/ 2);
                        如果(pSceneTouchEvent.getAction()== TouchEvent.ACTION_UP){
                            checkForCollisionsWithTowers(本);
                        }
                        返回true;
                    }
                };
                环ring3的=新环(3,97,255,this.mRing3,getVertexBufferObjectManager()){
                    @覆盖
                    公共布尔onAreaTouched(的TouchEvent pSceneTouchEvent,浮pTouchAreaLocalX,浮pTouchAreaLocalY){
                        如果(((环)this.getmStack()。偷看())。getmWeight()!= this.getmWeight())
                            返回false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,pSceneTouchEvent.getY() - this.getHeight()/ 2);
                        如果(pSceneTouchEvent.getAction()== TouchEvent.ACTION_UP){
                            checkForCollisionsWithTowers(本);
                        }
                        返回true;
                    }
                };
                scene.attachChild(RING1);
                scene.attachChild(分享帮助);
                scene.attachChild(ring3的);
                // 4 - 将所有环叠1
                this.mStack1.add(ring3的);
                this.mStack1.add(分享帮助);
                this.mStack1.add(RING1);
                // 5 - 初始化开始位置每个环
                ring1.setmStack(mStack1);
                ring2.setmStack(mStack1);
                ring3.setmStack(mStack1);
                ring1.setmTower(mTower1);
                ring2.setmTower(mTower1);
                ring3.setmTower(mTower1);
                // 6 - 添加触摸处理器
                scene.registerTouchArea(RING1);
                scene.registerTouchArea(分享帮助);
                scene.registerTouchArea(ring3的);
                scene.setTouchAreaBindingOnActionDownEnabled(真);                countText =新文本(0,0,main_font,移动+ Movescount为,10,this.getVertexBufferObjectManager());
                countText.setPosition(CAMERA_WIDTH - countText.getWidth(),10);
                scene.attachChild(countText);                pOnCreateSceneCallback.onCreateSceneFinished(场景);    }       @覆盖
       公共无效onPopulateScene(场景pScene,OnPopulateSceneCallback pOnPopulateSceneCallback)抛出IOException           //加载你的资源和场景
           pOnPopulateSceneCallback.onPopulateSceneFinished();       }
        私人无效checkForCollisionsWithTowers(铃铃){
            堆叠式和LT;环>堆栈=无效;
            雪碧塔= NULL;            布尔isRingUpdated = FALSE;
            如果(ring.collidesWith(mTower1)及及(mStack1.size()== 0 || ring.getmWeight()&所述;((环)mStack1.peek())getmWeight())){
                堆栈= mStack1;
                塔= mTower1;               isRingUpdated = checkIsRingUpdated(环,mTower1);            }否则如果(ring.collidesWith(mTower2)及及(mStack2.size()== 0 || ring.getmWeight()&所述;((环)mStack2.peek())getmWeight())){
                堆栈= mStack2;
                塔= mTower2;                isRingUpdated = checkIsRingUpdated(环,mTower2);
            }否则如果(ring.collidesWith(mTower3)及及(mStack3.size()== 0 || ring.getmWeight()&所述;((环)mStack3.peek())getmWeight())){
                堆栈= mStack3;
                塔= mTower3;                isRingUpdated = checkIsRingUpdated(环,mTower3);
            }其他{
                堆栈= ring.getmStack();
                塔= ring.getmTower();
            }
            。ring.getmStack()删除(环);
            如果(堆栈= NULL&放大器;!&安培;塔= NULL&放大器;!&安培; stack.size()== 0){
                ring.setPosition(tower.getX()+ tower.getWidth()/ 2 - ring.getWidth()/ 2,tower.getY()+ tower.getHeight() - ring.getHeight());
            }否则如果(堆栈=空&放大器;!&放大器;塔=空&放大器;!&放大器; stack.size()大于0){
                ring.setPosition(tower.getX()+ tower.getWidth()/ 2 - ring.getWidth()/ 2,((环)stack.peek())的getY() - ring.getHeight());
            }
            stack.add(圈);
            ring.setmStack(栈);
            ring.setmTower(塔);
            如果(isRingUpdated){
                Movescount为++;
                如果(countText!= NULL){
                    countText.setText(移动+ Movescount为);
                }            }            isGameOver();
        }        私人布尔checkIsRingUpdated(铃铃,雪碧mTower){
            如果(ring.getmTower()。getTag()!= mTower.getTag()){
                返回true;
            }
            返回false;
        }        私人无效isGameOver(){
            如果(mStack3.size()== 3){                 文本gameOverText =新文本(0,0,main_fontGAMEOVER,this.getVertexBufferObjectManager());
                 gameOverText.setPosition(CAMERA_WIDTH / 2 - gameOverText.getWidth()/ 2,CAMERA_HEIGHT / 2 - gameOverText.getHeight()/ 2);
                 scene.attachChild(gameOverText);                 scene.clearTouchAreas();
           }            }}

I'm fairly new to AndEngine Android programming and I don't know how to convert my SimpleBaseGameActivity code to BaseGameActivity. I want to convert it to match the tutorial i found on BaseGameActivity Splash + Menu tutorial so that I'll be able to merge it all.

Here's the code for the Tower of Hanoi Game:

public class MainActivity extends SimpleBaseGameActivity {
private ITexture main_font_texture;
Text countText;
Font main_font;
int movesCount;
Scene scene = new Scene();
private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;

private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
private Stack<Ring> mStack1, mStack2, mStack3;
private Sprite mTower1, mTower2, mTower3;

@Override

public EngineOptions onCreateEngineOptions() {
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}

@Override

protected void onCreateResources() {


    try {

         main_font = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA, Typeface.DEFAULT, 60, true, Color.BLACK_ABGR_PACKED_INT);
         main_font.load();


        ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/background.png");
            }
        });

        ITexture towerTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {

            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/tower.png");
            }
        });

        ITexture ring1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {

            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ring1.png");
            }
        });

        ITexture ring2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {

            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ring2.png");
            }
        });
        ITexture ring3 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {

            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ring3.png");
            }
        });
        backgroundTexture.load();
        towerTexture.load();
        ring1.load();
        ring2.load();
        ring3.load();

        this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
        this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
        this.mRing1 = TextureRegionFactory.extractFromTexture(ring1);
        this.mRing2 = TextureRegionFactory.extractFromTexture(ring2);
        this.mRing3 = TextureRegionFactory.extractFromTexture(ring3);

        this.mStack1 = new Stack<Ring>();
        this.mStack2 = new Stack<Ring>();
        this.mStack3 = new Stack<Ring>();
    } catch (IOException e) {
        Debug.e(e);
    }
}

@Override
protected Scene onCreateScene() {



    Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());

    mTower1 = new Sprite(0.241f * CAMERA_WIDTH, 0.133f * CAMERA_HEIGHT, this.mTowerTextureRegion, getVertexBufferObjectManager());
    mTower2 = new Sprite(0.5f * CAMERA_WIDTH, 0.133f * CAMERA_HEIGHT, this.mTowerTextureRegion, getVertexBufferObjectManager());
    mTower3 = new Sprite(0.756f * CAMERA_WIDTH, 0.133f * CAMERA_HEIGHT, this.mTowerTextureRegion, getVertexBufferObjectManager());

    Ring ring1 = new Ring(1, 139, 174, this.mRing1, getVertexBufferObjectManager()) {

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            if(this.getmStack().peek().getmWeight() != this.getmWeight())
                return false;
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                checkForCollisionsWithTowers(this);
            }
            return true;
        }
    };
    Ring ring2 = new Ring(2, 118, 212, this.mRing2, getVertexBufferObjectManager()) {

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            if(this.getmStack().peek().getmWeight() != this.getmWeight())
                return false;
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                checkForCollisionsWithTowers(this);
            }
            return true;
        }
    };
    Ring ring3 = new Ring(3, 97, 255, this.mRing3, getVertexBufferObjectManager()) {

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            if(this.getmStack().peek().getmWeight() != this.getmWeight())
                return false;
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                checkForCollisionsWithTowers(this);
            }
            return true;
        }
    };

    this.mStack1.add(ring3);
    this.mStack1.add(ring2);
    this.mStack1.add(ring1);

    ring1.setmStack(mStack1);
    ring2.setmStack(mStack1);
    ring3.setmStack(mStack1);
    ring1.setmTower(mTower1);
    ring2.setmTower(mTower1);
    ring3.setmTower(mTower1);

    scene.attachChild(backgroundSprite);
    scene.attachChild(mTower1);
    scene.attachChild(mTower2);
    scene.attachChild(mTower3);
    scene.attachChild(ring1);
    scene.attachChild(ring2);
    scene.attachChild(ring3);
    scene.registerTouchArea(ring1);
    scene.registerTouchArea(ring2);
    scene.registerTouchArea(ring3);

    scene.setTouchAreaBindingOnActionDownEnabled(true);



   countText = new Text(0, 0, main_font, "Moves:" + movesCount ,10,  this.getVertexBufferObjectManager());
     countText.setPosition(0,0);
     scene.attachChild(countText);


    return scene;
}


private void checkForCollisionsWithTowers(Ring ring) {
    Stack<Ring> stack = null;
    Sprite tower = null;

    if (ring.collidesWith(mTower1) && (mStack1.size() == 0 || ring.getmWeight() < mStack1.peek().getmWeight())) {
        stack = mStack1;
        tower = mTower1;
        movesCount++;
        countText.setText("Moves: "+ movesCount);

    } else if (ring.collidesWith(mTower2) && (mStack2.size() == 0 || ring.getmWeight() < mStack2.peek().getmWeight())) {
        stack = mStack2;
        tower = mTower2;
        movesCount++;
        countText.setText("Moves: "+ movesCount);


    } else if (ring.collidesWith(mTower3) && (mStack3.size() == 0 || ring.getmWeight() < mStack3.peek().getmWeight())) {
        stack = mStack3;
        tower = mTower3;
        movesCount++;
        countText.setText("Moves: "+ movesCount);

    } else {
        stack = ring.getmStack();
        tower = ring.getmTower();


    }
    ring.getmStack().remove(ring);
    if (stack != null && tower !=null && stack.size() == 0) {
        ring.setPosition(tower.getX() + tower.getWidth()/2 - ring.getWidth()/2, tower.getY() + tower.getHeight() - ring.getHeight());

    } else if (stack != null && tower !=null && stack.size() > 0) {
        ring.setPosition(tower.getX() + tower.getWidth()/2 - ring.getWidth()/2, stack.peek().getY() - ring.getHeight());

    }
    stack.add(ring);
    ring.setmStack(stack);
    ring.setmTower(tower);


    isGameOver();


}



private void isGameOver(){

    if(mStack3.size() == 3){

         Font main_font = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA, Typeface.DEFAULT, 60, true, Color.BLACK_ABGR_PACKED_INT);
         main_font.load();

         Text levelCompleteText = new Text(0, 0, main_font, "Level Complete!" , this.getVertexBufferObjectManager());
         levelCompleteText.setPosition(CAMERA_WIDTH/2 - levelCompleteText.getWidth()/2, CAMERA_HEIGHT/2 - levelCompleteText.getHeight()/2);
         scene.attachChild(levelCompleteText);
         scene.clearTouchAreas();


   }
}
    }

Here's for the Ring.java :

@SuppressWarnings("unused")
public class Ring extends Sprite {

private int mWeight;
private Stack<Ring> mStack; //this represents the stack that this ring belongs to
private Sprite mTower;

public Ring(int weight, float pX, float pY, ITextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) {
    super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
    this.mWeight = weight;
}

public int getmWeight() {
    return mWeight;
}

public Stack<Ring> getmStack() {
    return mStack;
}

public void setmStack(Stack<Ring> mStack) {
    this.mStack = mStack;
}

public Sprite getmTower() {
    return mTower;
}

public void setmTower(Sprite mTower) {
    this.mTower = mTower;
}
}
解决方案
package org.farook.towerofhanoi;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.bitmap.BitmapTextureFormat;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.color.Color;
import org.andengine.util.debug.Debug;

import android.graphics.Typeface;

public class TowerOfHanoiActivity extends BaseGameActivity {
    private static int CAMERA_WIDTH = 800;
    private static int CAMERA_HEIGHT = 480;
    private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
    private Sprite mTower1, mTower2, mTower3;
    private Stack<Ring> mStack1, mStack2, mStack3;
      Text countText;
      Font main_font;
      int movesCount;
    Scene scene;
    @Override
    public EngineOptions onCreateEngineOptions() {
        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }


    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {

        try {


            main_font = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, BitmapTextureFormat.RGBA_8888, TextureOptions.BILINEAR_PREMULTIPLYALPHA, Typeface.DEFAULT, 60, true, Color.BLACK_ABGR_PACKED_INT);
            main_font.load();


             BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

            // 1 - Set up bitmap textures
            ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("gfx/background.png");
                }
            });
            ITexture towerTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("gfx/tower.png");
                }
            });
            ITexture ring1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("gfx/ring1.png");
                }
            });
            ITexture ring2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("gfx/ring2.png");
                }
            });
            ITexture ring3 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("gfx/ring3.png");
                }
            });
            // 2 - Load bitmap textures into VRAM
            backgroundTexture.load();
            towerTexture.load();
            ring1.load();
            ring2.load();
            ring3.load();
            // 3 - Set up texture regions
            this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
            this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
            this.mRing1 = TextureRegionFactory.extractFromTexture(ring1);
            this.mRing2 = TextureRegionFactory.extractFromTexture(ring2);
            this.mRing3 = TextureRegionFactory.extractFromTexture(ring3);
            // 4 - Create the stacks
            this.mStack1 = new Stack<Ring>();
            this.mStack2 = new Stack<Ring>();
            this.mStack3 = new Stack<Ring>();
        } catch (IOException e) {
            Debug.e(e);
        }

        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {

        // 1 - Create new scene
                scene = new Scene();
                Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
                scene.attachChild(backgroundSprite);

                // 2 - Add the towers
                mTower1 = new Sprite(192, 63, this.mTowerTextureRegion, getVertexBufferObjectManager());
                mTower2 = new Sprite(400, 63, this.mTowerTextureRegion, getVertexBufferObjectManager());
                mTower3 = new Sprite(604, 63, this.mTowerTextureRegion, getVertexBufferObjectManager());

                mTower1.setTag(1);
                mTower2.setTag(2);
                mTower3.setTag(3);

                scene.attachChild(mTower1);
                scene.attachChild(mTower2);
                scene.attachChild(mTower3);

                // 3 - Create the rings
                Ring ring1 = new Ring(1, 139, 174, this.mRing1, getVertexBufferObjectManager()) {
                    @Override
                    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                        if (((Ring) this.getmStack().peek()).getmWeight() != this.getmWeight())
                            return false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                        if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                            checkForCollisionsWithTowers(this);
                        }
                        return true;
                    }
                };
                Ring ring2 = new Ring(2, 118, 212, this.mRing2, getVertexBufferObjectManager()) {
                    @Override
                    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                        if (((Ring) this.getmStack().peek()).getmWeight() != this.getmWeight())
                            return false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                        if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                            checkForCollisionsWithTowers(this);
                        }
                        return true;
                    }
                };
                Ring ring3 = new Ring(3, 97, 255, this.mRing3, getVertexBufferObjectManager()) {
                    @Override
                    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                        if (((Ring) this.getmStack().peek()).getmWeight() != this.getmWeight())
                            return false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                        if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                            checkForCollisionsWithTowers(this);
                        }
                        return true;
                    }
                };
                scene.attachChild(ring1);
                scene.attachChild(ring2);
                scene.attachChild(ring3);
                // 4 - Add all rings to stack one
                this.mStack1.add(ring3);
                this.mStack1.add(ring2);
                this.mStack1.add(ring1);
                // 5 - Initialize starting position for each ring
                ring1.setmStack(mStack1);
                ring2.setmStack(mStack1);
                ring3.setmStack(mStack1);
                ring1.setmTower(mTower1);
                ring2.setmTower(mTower1);
                ring3.setmTower(mTower1);
                // 6 - Add touch handlers
                scene.registerTouchArea(ring1);
                scene.registerTouchArea(ring2);
                scene.registerTouchArea(ring3);
                scene.setTouchAreaBindingOnActionDownEnabled(true);

                countText = new Text(0, 0, main_font, "Moves:" + movesCount ,10,  this.getVertexBufferObjectManager());
                countText.setPosition(CAMERA_WIDTH - countText.getWidth(), 10);
                scene.attachChild(countText);

                pOnCreateSceneCallback.onCreateSceneFinished(scene);

    }



       @Override
       public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {

           //LOAD YOUR RESOURCES AND SCENES


           pOnPopulateSceneCallback.onPopulateSceneFinished();

       }


        private void checkForCollisionsWithTowers(Ring ring) {
            Stack<Ring> stack = null;
            Sprite tower = null;

            boolean isRingUpdated = false;
            if (ring.collidesWith(mTower1) && (mStack1.size() == 0 || ring.getmWeight() < ((Ring) mStack1.peek()).getmWeight())) {
                stack = mStack1;
                tower = mTower1;

               isRingUpdated = checkIsRingUpdated(ring, mTower1);

            } else if (ring.collidesWith(mTower2) && (mStack2.size() == 0 || ring.getmWeight() < ((Ring) mStack2.peek()).getmWeight())) {
                stack = mStack2;
                tower = mTower2;

                isRingUpdated =  checkIsRingUpdated(ring, mTower2);
            } else if (ring.collidesWith(mTower3) && (mStack3.size() == 0 || ring.getmWeight() < ((Ring) mStack3.peek()).getmWeight())) {
                stack = mStack3;
                tower = mTower3;

                isRingUpdated =   checkIsRingUpdated(ring, mTower3);
            } else {
                stack = ring.getmStack();
                tower = ring.getmTower();
            }
            ring.getmStack().remove(ring);
            if (stack != null && tower !=null && stack.size() == 0) {
                ring.setPosition(tower.getX() + tower.getWidth()/2 - ring.getWidth()/2, tower.getY() + tower.getHeight() - ring.getHeight());
            } else if (stack != null && tower !=null && stack.size() > 0) {
                ring.setPosition(tower.getX() + tower.getWidth()/2 - ring.getWidth()/2, ((Ring) stack.peek()).getY() - ring.getHeight());
            }
            stack.add(ring);
            ring.setmStack(stack);
            ring.setmTower(tower);


            if(isRingUpdated){
                movesCount++;
                if(countText != null){
                    countText.setText("Moves:"+ movesCount);
                }

            }

            isGameOver();
        }

        private boolean checkIsRingUpdated(Ring ring, Sprite mTower) {
            if(ring.getmTower().getTag() != mTower.getTag()){
                return true;
            }
            return false;
        }

        private void isGameOver() {
            if(mStack3.size() == 3){

                 Text gameOverText = new Text(0, 0, main_font, "GameOver", this.getVertexBufferObjectManager());
                 gameOverText.setPosition(CAMERA_WIDTH/2 - gameOverText.getWidth()/2, CAMERA_HEIGHT/2 - gameOverText.getHeight()/2);
                 scene.attachChild(gameOverText);

                 scene.clearTouchAreas();
           }

            }

}

这篇关于从转换的SimpleBaseGameActivity code到BaseGameActivity AndEndgine GLES2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:54