我正在关注Mario Zechner撰写的“开始Android 4游戏开发”一书。
如果您想通过阅读本书来看看开发的Android框架:
现在,在进一步深入本书并启动OpenGl之前,我决定通过尝试将Framework移植到Java版本来改进我的基础Java知识。
我认为编写一个使用Framework的游戏,并且在将这些GameClasses复制到我的Java版本时,它只会使用相同的框架和不同的接口实现。
因此,当我尝试在java中实现顶级接口时,我不得不遇到诸如以下问题: - 等于AssetsManager等等:
该部分解决了Android GameFramework的书:
________INTERFACE______
public interface FileIO {
// Load assets from apk package
InputStream readAsset(String fileName) throws IOException;
// Load files from storage (SD)
InputStream readFile(String fileName) throws IOException;
OutputStream writeFile(String fileName) throws IOException;
}
_______ ANDROID IMPLEMENTATION _______
public class AndroidFileIO implements FileIO {
Context context;
AssetManager assets;
String externalStoragePath;
// Constructor
public AndroidFileIO(Context context) {
this.context = context;
this.assets = context.getAssets();
this.externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}
@override
public InputStream readAsset(String fileName) throws IOException {
return assets.open(fileName);
}
@override
public InputStream readFile(String fileName) throws IOException {
return new FileInputStream(externalStoragePath + fileName);
}
@override
public OutputStream writeFile(String fileName) throws IOException {
return new FileOutputStream(externalStoragePath + fileName);
}
}
我不知道如何在Java中做同样的事情,因为上面使用的AssetsManager也出现在框架的另一个Android实现中。
我过去常常用Java加载文件:
public void load(String filename){
BufferedReader in = null;
File file1 = new File("/Users/jesjjes/AndroidStudioProjects/MapWindowTest/app/src/main/assets/"+filename+".text");
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
} catch (IOException e) {
// :( It's ok we have defaults
} catch (NumberFormatException e) {
// :/ It's ok, defaults save our day
}
finally {
try {
if (in != null)
in.close();
}
catch (IOException e) {
}
}
}
你会如何解决?框架不是太复杂,你可以很好地猜测上面链接的GitHub Rep。
我不需要确切的代码解决方案,但您可以分享一些有关如何执行此操作的提示。
我应该简单地扩展我的界面吗?后来以某种方式使用顶级界面告诉游戏使用哪一个?所以调用方法如:game.getInput()。getTouchedX()知道他们是否需要调用Android GameFramework或JavaFrameworks输入x版本? :P
提前致谢
答案 0 :(得分:1)
public class DesktopFileIO implements FileIO {
private final File externalStoragePath;
private final File assetPath;
// Constructor
public DesktopFileIO(String externalStoragePath, String assetPath) {
this(new File(externalStoragePath), new File(assetPath));
}
// Constructor
public DesktopFileIO(File externalStoragePath, File assetPath) {
super();
this.externalStoragePath = externalStoragePath;
this.assetPath = assetPath;
}
@override
public InputStream readAsset(String fileName) throws IOException {
return new FileInputStream( new File(assetPath, fileName) );
}
@override
public InputStream readFile(String fileName) throws IOException {
return new FileInputStream( new File(externalStoragePath, fileName) );
}
@override
public OutputStream writeFile(String fileName) throws IOException {
return new FileOutputStream( new File( externalStoragePath, fileName ) );
}
}