AndEngine背景缩放与缩放

时间:2014-07-05 14:35:00

标签: java background camera andengine pinchzoom

我有一个GameScene,其中我使用了ZoomCamera,一个背景和一些“病毒”。 问题在于,当我在背景中插入或缩小背景时也会缩放,我不希望这样,请帮忙。

http://puu.sh/9XPUl/c6725806ae.png

http://puu.sh/9XQ5g/609d77e962.png

public class GameScene extends BaseScene implements IScrollDetectorListener, IPinchZoomDetectorListener, IOnSceneTouchListener  {
private static final String TAG_ENTITY = "entity";
private static final String TAG_ENTITY_ATTRIBUTE_X = "x";
private static final String TAG_ENTITY_ATTRIBUTE_Y = "y";
private static final String TAG_ENTITY_ATTRIBUTE_TYPE = "type";
private ZoomCamera mZoomCamera = GameActivity.mZoomCamera;
private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_CELL = "cell";
//private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM1 = "platform1";
//private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM2 = "platform2";
//private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM3 = "platform3";
//private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_COIN = "coin";
private SurfaceScrollDetector mScrollDetector;
private PinchZoomDetector mPinchZoomDetector;
private float mPinchZoomStartedCameraZoomFactor;
@Override
public void createScene() {
    this.mScrollDetector = new SurfaceScrollDetector(this);
    this.mPinchZoomDetector = new PinchZoomDetector(this);
    this.setOnSceneTouchListener(this);
    this.setTouchAreaBindingOnActionDownEnabled(true);
    this.setOnAreaTouchTraversalFrontToBack();
    createBackground();
    createHUD();
    createPhysics();
    loadLevel(1);
}

@Override
public void onBackKeyPressed() {
    SceneManager.getInstance().loadMenuScene(engine);

}
private void createBackground()
{   Sprite bg=new Sprite(400, 240, resourcesManager.menu_background_region, vbom);
    attachChild(bg);
    /*attachChild(new Sprite(400, 240, resourcesManager.menu_background_region, vbom)
    {
        @Override
        protected void preDraw(GLState pGLState, Camera pCamera) 
        {
            super.preDraw(pGLState, pCamera);
            pGLState.enableDither();
        }
    });*/
}

@Override
public SceneType getSceneType() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void disposeScene()
{
    camera.setHUD(null);
    camera.setCenter(400, 240);
    mZoomCamera.setZoomFactor(1.0f);
    // TODO code responsible for disposing scene
    // removing all game scene objects.
}
private HUD gameHUD;

private Text scoreText;

private void createHUD()
{
    gameHUD = new HUD();

    // CREATE SCORE TEXT
    scoreText = new Text(20, 420, resourcesManager.font, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
    scoreText.setAnchorCenter(0, 0);    
    scoreText.setText("Score: 0");
    gameHUD.attachChild(scoreText);

    camera.setHUD(gameHUD);
}
private int score = 0;

private void addToScore(int i)
{
    score += i;
    scoreText.setText("Score: " + score);
}

private PhysicsWorld physicsWorld;

private void createPhysics()
{
    physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, -17), false); 
    registerUpdateHandler(physicsWorld);
}

private void loadLevel(int levelID)
{
    final SimpleLevelLoader levelLoader = new SimpleLevelLoader(vbom);

    final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f);

    levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(LevelConstants.TAG_LEVEL)
    {
        public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException 
        {
            SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_WIDTH);
            SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_HEIGHT);

            // TODO later we will specify camera BOUNDS and create invisible walls
            // on the beginning and on the end of the level.

            return GameScene.this;
        }
    });

    levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(TAG_ENTITY)
    {
        public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException
        {
            final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X);
            final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y);
            final String type = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_TYPE);

            final Sprite levelObject;

           /* if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM1))
            {
                levelObject = new Sprite(x, y, resourcesManager.platform1_region, vbom);
                PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF).setUserData("platform1");
            } 
            else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM2))
            {
                levelObject = new Sprite(x, y, resourcesManager.platform2_region, vbom);
                final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
                body.setUserData("platform2");
                physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
            }
            else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM3))
            {
                levelObject = new Sprite(x, y, resourcesManager.platform3_region, vbom);
                final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
                body.setUserData("platform3");
                physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
            }
            else*/ if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_CELL))
            {
                levelObject = new Sprite(x, y, resourcesManager.cell_region, vbom);
                final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
                body.setUserData("cell");
                physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
            }
            /*else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_COIN))
            {
                levelObject = new Sprite(x, y, resourcesManager.coin_region, vbom)
                {
                    @Override
                    protected void onManagedUpdate(float pSecondsElapsed) 
                    {
                        super.onManagedUpdate(pSecondsElapsed);

                        /** 
                         * TODO
                         * we will later check if player collide with this (coin)
                         * and if it does, we will increase score and hide coin
                         * it will be completed in next articles (after creating player code)

                    }
                };
                levelObject.registerEntityModifier(new LoopEntityModifier(new ScaleModifier(1, 1, 1.3f)));
            } */           
            else
            {
                throw new IllegalArgumentException();
            }

            levelObject.setCullingEnabled(true);

            return levelObject;
        }
    });

    levelLoader.loadLevelFromAsset(activity.getAssets(), "level/" + levelID + ".lvl");
}

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    this.mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);

    if(this.mPinchZoomDetector.isZooming()) {
        this.mScrollDetector.setEnabled(false);
    } else {
        if(pSceneTouchEvent.isActionDown()) {
            this.mScrollDetector.setEnabled(true);
        }
        this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
    }

    return true;
}

@Override
public void onPinchZoomStarted(PinchZoomDetector pPinchZoomDetector,
        TouchEvent pSceneTouchEvent) {
    this.mPinchZoomStartedCameraZoomFactor = this.mZoomCamera.getZoomFactor();

}

@Override
public void onPinchZoom(PinchZoomDetector pPinchZoomDetector,
        TouchEvent pTouchEvent, float pZoomFactor) {
    this.mZoomCamera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);

}

@Override
public void onPinchZoomFinished(PinchZoomDetector pPinchZoomDetector,
        TouchEvent pTouchEvent, float pZoomFactor) {
    this.mZoomCamera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);

}

@Override
public void onScrollStarted(ScrollDetector pScollDetector, int pPointerID,
        float pDistanceX, float pDistanceY) {
    final float zoomFactor = mZoomCamera.getZoomFactor();
    mZoomCamera.offsetCenter(-pDistanceX / zoomFactor, pDistanceY / zoomFactor);

}

@Override
public void onScroll(ScrollDetector pScollDetector, int pPointerID,
        float pDistanceX, float pDistanceY) {
    final float zoomFactor = mZoomCamera.getZoomFactor();
    mZoomCamera.offsetCenter(-pDistanceX / zoomFactor, pDistanceY / zoomFactor);

}

@Override
public void onScrollFinished(ScrollDetector pScollDetector, int pPointerID,
        float pDistanceX, float pDistanceY) {
    final float zoomFactor = mZoomCamera.getZoomFactor();
    mZoomCamera.offsetCenter(-pDistanceX / zoomFactor, pDistanceY / zoomFactor);

}

}

1 个答案:

答案 0 :(得分:1)

您使用attachChild()方法将您的背景作为实体附加。您必须使用setBackground()方法。

setBackground(new SpriteBackground(bg));