使用带有 AndEngineTMXTileExtension 的 Android Studio 时出现 TMXLoader 错误

TMXLoader error when using Android Studio with AndEngineTMXTileExtension

我正在尝试创建类似塔防的游戏。我使用 Tiled Map Editor 创建了一个平铺等轴测图,并将其保存在 android studio 中的资产文件夹中。我目前正在使用 andengine 在我的 activity 中加载地图。

我的 MainActivity.Java :

package com.example.neelpatel104.towerfinisher;


import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.BoundCamera;
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.util.FPSLogger;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
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.exception.TMXException;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

public class MainActivity extends SimpleBaseGameActivity  {
private TMXTiledMap mTMXTiledMap;
    private TMXLayer tmxLayer;
    private static final int CAMERA_WIDTH=550;
    private static final int CAMERA_HEIGHT=300;

    private BoundCamera mBoundChaseCamera;
    protected int mCactusCount;

@Override
public Engine onCreateEngine(EngineOptions engineOptions){
    return new LimitedFPSEngine(engineOptions,60);
}
    @Override
    public EngineOptions onCreateEngineOptions(){
        this.mBoundChaseCamera = new BoundCamera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);return new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),this.mBoundChaseCamera);
}

    @Override
    protected void onCreateResources() {
    final TMXLoader tmxLoader = new TMXLoader(getAssets(),getTextureManager(),TextureOptions.NEAREST,mEngine.getVertexBufferObjectManager());
    try {
       this.mTMXTiledMap = tmxLoader.loadFromAsset("map/levelone.tmx");
   } catch (final TMXLoadException e){
            Debug.e(e);
        }
    }

    @Override
    protected Scene onCreateScene() {
       this.mEngine.registerUpdateHandler(new FPSLogger());
        final Scene scene = new Scene();
        this.tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);

        scene.setChildrenVisible(false);
        scene.setBackgroundEnabled(false);
        scene.attachChild(this.tmxLayer);

        return scene;
    }

}

下面这行给我一个错误:

 final TMXLoader tmxLoader = new TMXLoader(getAssets(),getTextureManager(),TextureOptions.NEAREST,mEngine.getVertexBufferObjectManager());

这是我的完整堆栈跟踪:

Information:Gradle tasks [:app:clean, :app:generateDebugSources,
:app:generateDebugAndroidTestSources, :app:mockableAndroidJar,
:app:prepareDebugUnitTestDependencies, :app:assembleDebug]
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/math/MathUtils.java
Error:(89, 50) error: cannot find symbol method ceil(float)
Error:(163, 42) error: cannot find symbol method sin(float)
Error:(164, 44) error: cannot find symbol method cos(float)
Error:(239, 19) error: cannot find symbol method sqrt(float)
Error:(246, 19) error: cannot find symbol method sqrt(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseElasticOut.java
Error:(69, 67) error: cannot find symbol method sin(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseElasticIn.java
Error:(70, 49) error: cannot find symbol method sin(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseSineInOut.java
Error:(52, 28) error: cannot find symbol method cos(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseCircularOut.java
Error:(58, 19) error: cannot find symbol method sqrt(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseCircularIn.java
Error:(57, 21) error: cannot find symbol method sqrt(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseSineOut.java
Error:(59, 19) error: cannot find symbol method sin(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/modifier/ease/EaseSineIn.java
Error:(59, 20) error: cannot find symbol method cos(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/adt/transformation/Transformation.java
Error:(122, 30) error: cannot find symbol method sin(float)
Error:(123, 30) error: cannot find symbol method cos(float)
Error:(139, 30) error: cannot find symbol method sin(float)
Error:(140, 30) error: cannot find symbol method cos(float)
Error:(160, 30) error: cannot find symbol method sin(float)
Error:(161, 30) error: cannot find symbol method cos(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/levelstats/LevelStatsDBConnector.java
Error:(12, 23) error: package org.apache.http does not exist
Error:(13, 23) error: package org.apache.http does not exist
Error:(14, 23) error: package org.apache.http does not exist
Error:(15, 30) error: package org.apache.http.client does not exist
Error:(16, 37) error: package org.apache.http.client.entity does not exist
Error:(17, 38) error: package org.apache.http.client.methods does not exist
Error:(18, 35) error: package org.apache.http.impl.client does not exist
Error:(19, 31) error: package org.apache.http.message does not exist
Error:(82, 12) error: cannot find symbol class HttpClient
Error:(82, 40) error: cannot find symbol class DefaultHttpClient
Error:(83, 12) error: cannot find symbol class HttpPost
Error:(83, 36) error: cannot find symbol class HttpPost
Error:(86, 17) error: cannot find symbol class NameValuePair
Error:(86, 63) error: cannot find symbol class NameValuePair
Error:(88, 29) error: cannot find symbol class BasicNameValuePair
Error:(89, 29) error: cannot find symbol class BasicNameValuePair
Error:(90, 29) error: cannot find symbol class BasicNameValuePair
Error:(91, 29) error: cannot find symbol class BasicNameValuePair
Error:(92, 29) error: cannot find symbol class BasicNameValuePair
Error:(94, 29) error: cannot find symbol class UrlEncodedFormEntity
Error:(97, 12) error: cannot find symbol class HttpResponse
Error:(100, 23) error: cannot find symbol variable HttpStatus
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/util/algorithm/path/astar/EuclideanHeuristic.java
Error:(40, 19) error: cannot find symbol method sqrt(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/opengl/texture/atlas/bitmap/source/decorator/ColorSwapBitmapTextureAtlasSourceDecorator.java
Error:(7, 24) error: cannot find symbol class AvoidXfermode
Error:(8, 38) error: package android.graphics.AvoidXfermode does not exist
Error:(70, 31) error: cannot find symbol class AvoidXfermode
Error:(70, 86) error: cannot find symbol variable Mode
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/opengl/font/StrokeFont.java
Error:(76, 36) error: cannot find symbol method floor(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/entity/particle/ParticleSystem.java
Error:(181, 111) error: cannot find symbol method floor(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/entity/particle/emitter/CircleParticleEmitter.java
Error:(50, 54) error: cannot find symbol method cos(float)
Error:(51, 54) error: cannot find symbol method sin(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/entity/particle/emitter/CircleOutlineParticleEmitter.java
Error:(50, 54) error: cannot find symbol method cos(float)
Error:(51, 54) error: cannot find symbol method sin(float)
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/entity/modifier/PathModifier.java
/home/neelpatel104/AndroidStudioProjects/TowerFinisher/AndEngine/src/main/java/org/andengine/engine/camera/hud/controls/AnalogOnScreenControl.java
Error:(85, 39) error: cannot find symbol method cos(float)
Error:(85, 71) error: cannot find symbol method sin(float)
Error:Execution failed for task ':AndEngine:compileReleaseJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1 mins 40.808 secs
Information:55 errors
Information:0 warnings
Information:See complete output in console

我不明白这是什么问题。每次我尝试调试程序时都会出现此错误。我什至尝试重新安装 andengine 和 TMXTiledMAP Extension,但错误始终存在。如果有人看的话我会喜欢的

构建失败,因为某些 classes 和方法在最新版本中已弃用。您可以尝试降低您的 andengine build.gradle 文件的 compileSdkVersion。您也可以通过简单的更改来修复其中的大部分错误:

  1. 使用标准数学 class 更改所有数学调用,如 sin、cos、floor、sqrt、ceil 等:将 FloatMath.sin() 替换为 (float) Math.sin() 其他方法类似。
  2. 修复包 org.apache.http 不存在的错误:添加 useLibrary 'org.apache.http.legacy' 到 andengine build.gradle 文件:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.0'
        }
    }
    
    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 23
        //buildToolsVersion '26'
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
            }
        }
        useLibrary 'org.apache.http.legacy' //for support 23+ sdk
    }
    ...
    
  3. Class AvoidXfermode 已弃用,因此请按照上面的代码设置 compileSdkVersion 23。如果有人描述如何在不降低 compileSdkVersion 的情况下以其他方式修复 AvoidXfermode,我将不胜感激。