gpt4 book ai didi

dictionary - 在 AndEngine 中使用 TMX 文件

转载 作者:行者123 更新时间:2023-12-01 13:56:33 27 4
gpt4 key购买 nike

我有一个用“Tiled”创建的等距 .TMX 文件,我使用的是 AndEngine GLES1。我想将其用作背景或 map 。如果可能的话,我还希望将我的“玩家”运动与瓷砖联系起来。我知道有一些问题与此有关,但我需要实际代码来显示背景。

这是我唯一的java文件

package com.example.game;

import java.util.LinkedList;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.view.Display;

public class SimpleGame extends BaseGameActivity implements
IOnSceneTouchListener {
//Set up camera
private Camera mCamera;

//Set main scene
private Scene mMainScene;
//?? graphics variables?
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
//declaring sprite variable for player character
private Sprite player;
//Equiping ammunition
private LinkedList projectileLL;
private LinkedList projectilesToBeAdded;
private TextureRegion mProjectileTextureRegion;


@Override
public Engine onLoadEngine() {
//Preparing Engine giving it proper resolution (what you see on the screen)
final Display display = getWindowManager().getDefaultDisplay();
int cameraWidth = display.getWidth();
int cameraHeight = display.getHeight();

mCamera = new Camera(0, 0, cameraWidth, cameraHeight);

return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
new RatioResolutionPolicy(cameraWidth, cameraHeight), mCamera));
}

@Override
public void onLoadResources() {
mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);

BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
// assinging the player.png image for the mPlayerTextureRegion
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mBitmapTextureAtlas, this, "player.png",
0, 0);
mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);

mProjectileTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mBitmapTextureAtlas, this,
"Projectile.png", 64, 0);
}

@Override
public Scene onLoadScene() {
//??? Creating the scene?
mEngine.registerUpdateHandler(new FPSLogger());

mMainScene = new Scene();

mMainScene
.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

final int PlayerX = this.mPlayerTextureRegion.getWidth() / 2;
final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
.getHeight()) / 2);

// Using sprite to illustrate player
player = new Sprite(PlayerX, PlayerY, mPlayerTextureRegion);
// Place player on game screen
mMainScene.attachChild(player);

projectileLL = new LinkedList();
projectilesToBeAdded = new LinkedList();

mMainScene.setOnSceneTouchListener(this);

return mMainScene;
}
// This method is used for shooting "bullets"
// Need to find a way to limit ammunition based on in game inventory
private void shootProjectile(final float pX, final float pY) {

int offX = (int) (pX - player.getX());
int offY = (int) (pY - player.getY());
if (offX <= 0)
return;

//Using projectile.png as sprite, what is .deepCopy???
final Sprite projectile;
projectile = new Sprite(player.getX(), player.getY(),
mProjectileTextureRegion.deepCopy());
mMainScene.attachChild(projectile, 1);

int realX = (int) (mCamera.getWidth() + projectile.getWidth() / 2.0f);
float ratio = (float) offY / (float) offX;
int realY = (int) ((realX * ratio) + projectile.getY());

int offRealX = (int) (realX - projectile.getX());
int offRealY = (int) (realY - projectile.getY());
float length = (float) Math.sqrt((offRealX * offRealX)
+ (offRealY * offRealY));
float velocity = 480.0f / 1.0f; // 480 pixels per 1 sec//
float realMoveDuration = length / velocity;

MoveModifier mod = new MoveModifier(realMoveDuration,
projectile.getX(), realX, projectile.getY(), realY);
projectile.registerEntityModifier(mod.deepCopy());

projectilesToBeAdded.add(projectile);
}

public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
final float touchX = pSceneTouchEvent.getX();
final float touchY = pSceneTouchEvent.getY();
shootProjectile(touchX, touchY);
return true;
}
return false;
}


@Override
public void onLoadComplete() {


}
}

编辑切换到 GLES2 后,这给了我一些错误。我正在使用一个新类并插入提供的代码。我需要为其中任何一个创建类吗?是否通过属性链接 TMXproject - android 和该页面底部的“添加”框?谢谢

package com.example.game;

import org.andengine.engine.camera.Camera;
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.scene.background.Background;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

public class StarterGame extends SimpleBaseGameActivity {

static final int CAMERA_WIDTH = 800;
static final int CAMERA_HEIGHT = 480;
private Object tiledMap;

@Override
public EngineOptions onCreateEngineOptions() {
//Create view for the scene??
Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}

@Override
protected void onCreateResources() {
// TODO Auto-generated method stub
try {
final TMXLoader tmxLoader = new TMXLoader(context.getAssets(), textureManager, TextureOptions.NEAREST, vertexBufferObjectManager, new ITMXTilePropertiesListener() {
@Override
public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
// do stuff with tiles that have properties...
}
});
this.tiledMap = tmxLoader.loadFromAsset("tmx/bg.tmx");
this.tiledMap.setIsometricDrawMethod(TMXIsometricConstants.DRAW_METHOD_ISOMETRIC_CULLING_PADDING);
} catch (final TMXLoadException e) {
Debug.e(e);
}
}

@Override
protected Scene onCreateScene() {
//Setup scene with background color only
Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0));
return scene;
}

}

最佳答案

很遗憾,我只能向您展示我是如何使用 AndEngine GLES2 来实现的。不过,我不确定 GLES1 版本是否可以做到这一点。但也许你可以试着告诉我们你的经历。

首先从 Git 存储库下载 TMX Tilemap 扩展:https://github.com/nicolasgramlich/AndEngineTMXTiledMapExtension

其次将您的项目与 AndEngineTMXTiledMapExtension 项目链接起来(它需要标记为库)

当这两个项目链接在一起时,您实际上可以像这样使用扩展:

 import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXLoader.ITMXTilePropertiesListener;
import org.andengine.extension.tmx.TMXProperties;
import org.andengine.extension.tmx.TMXTile;
import org.andengine.extension.tmx.TMXTileProperty;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.constants.TMXIsometricConstants;
import org.andengine.extension.tmx.util.exception.TMXLoadException;


public class YourActivity extends SimpleBaseGameActivity {
private TMXTiledMap tiledMap;
private TMXLayer tmxLayer;

@Override
protected void onCreateResources() {
try {
final TMXLoader tmxLoader = new TMXLoader(getAssets(), getTextureManager(), TextureOptions.NEAREST, getVertexBufferObjectManager(), new ITMXTilePropertiesListener() {
@Override
public void onTMXTileWithPropertiesCreated(final TMXTiledMap tmxTiledMap, final TMXLayer tmxLayer, final TMXTile tmxTile, final TMXProperties<TMXTileProperty> tmxTileProperties) {
// do stuff with tiles that have properties...
}
});
this.tiledMap = tmxLoader.loadFromAsset("tmx/yourMap.tmx");
this.tiledMap.setIsometricDrawMethod(TMXIsometricConstants.DRAW_METHOD_ISOMETRIC_CULLING_PADDING);
} catch (final TMXLoadException e) {
Debug.e(e);
}

}

@Override
protected Scene onCreateScene() {
this.tmxLayer = this.tiledMap.getTMXLayers().get(0); // the 0 is just an index of the layer. It depends on how many layers you created within Tiled
attachChild(this.tmxLayer);
}

}

当您包含等轴测图时,您可能会看到一些像素瑕疵(黑线,图 block 聚集在一起的地方)——这是一个常见问题,在以下位置有一个修复程序: http://code.google.com/p/andenginetmxtiledmapartifactfixer/在这里你可以找到一个 java 程序,你可以用它加载你的 map 文件,它会生成一个你可以在你的项目中使用的固定 map 。在用法下查看您需要哪些参数。

希望这有帮助:)

克里斯多夫

关于dictionary - 在 AndEngine 中使用 TMX 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12084548/

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