如何在继承自CCLayer的类中使用getAssets

3

我有一个扩展了CCLayer的类。我需要从assets文件夹中获取一个文本文件。但是在这个类中,我无法使用getAssets()方法。那么如何在扩展了CCLayer的类中使用getAssets()方法呢?

3个回答

3

终于得到答案了...

Context context = CCDirector.sharedDirector().getActivity().getApplicationContext();
InputStream is = context.getAssets().open("abc.txt");

1

当您在类中创建方法时,请使用:

private void abc (Context context){
    context.getAssets();
}

当您调用此方法时,必须将您的上下文放入方法中:

yourclass.abc(getBaseContext());

当我尝试使用 getBaseContext() 时,出现错误提示,但实际上并不存在该函数。 :( - Deepzz
尝试使用新的yourclass.abc(your_activity.this)或new yourclass.abc(getapplicationcontext())。您可以发布详细的类信息。 - Nguyễn Anh Quế

1

你必须传递上下文给类才能使用它。如果是你自定义的类,就在构造函数中传递它,然后保存并使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接