gpt4 book ai didi

java - AndEngine 背景缩放

转载 作者:行者123 更新时间:2023-12-01 12:47:11 28 4
gpt4 key购买 nike

我有一个 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);

}

}

最佳答案

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

setBackground(new SpriteBackground(bg));

关于java - AndEngine 背景缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587457/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com