我正在使用LibGdx在Eclipse中编写一个应用程序,用于具有两个屏幕的游戏。还有一个Question类,它有两个实例变量是Music对象,两个是Strings。第一个屏幕获取对Question对象的引用,并播放问题的Music实例以及包含答案且没有问题的Music实例。但是,当调用第二个屏幕时,对此Question对象的引用将传递到第二个屏幕,但Music对象(从Question中获取)都不再播放。我知道问题被传递到第二个屏幕,因为第二个屏幕可以访问问题的实例变量,这些变量是字符串而不是音乐。 这个问题只能在Android上运行时观察到,当我运行桌面项目时,一切正常。 这是问题类
public class QuestionClothsType {
private int ID;
private String question;
private Music questionVoice;
private String answer;
private Music answerVoice;
private String colourAnswer;
public QuestionClothsType(int anID, String aQuestion, Music aVoice, String anAnswer, Music anAnswerVoice, String aColourAnswer){
this.ID = anID;
this.question = aQuestion;
this.questionVoice = aVoice;
this.answer = anAnswer;
this.answerVoice = anAnswerVoice;
this.colourAnswer = aColourAnswer;
}
public int getID(){
return this.ID;
}
public Music getQuestionVoice() {
return this.questionVoice;
}
public String getAnswer(){
return this.answer;
}
public Music getAnswerVoice() {
return this.answerVoice;
}
public String getColourAnswer(){
return this.colourAnswer;
}
}
这是第二个屏幕的相关部分:
….
Music applause;
Music voiceItem;
Music voiceItem2;
Music voiceItem3;
private QuestionClothsType passedQuestion;
….
public ItemsScreen(Room room, ItemType aType, QuestionClothsType aQuestion){
this.passedQuestion = aQuestion;
voiceItem = passedQuestion.getQuestionVoice();
voiceItem2 = passedQuestion.getQuestionVoice();
voiceItem3 = passedQuestion.getAnswerVoice();
…
private void checkAnswer(int answer2) {
….
if (answerColour.equals(passedQuestion.getColourAnswer())){
System.out.println("right. the answer was " + passedQuestion.getAnswer());
applause.play();
applause.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(Music aMusic){
dispose();
roomGame.setScreen(new ChestOfDrawersScreen(roomGame));
}
}
);
}
else{
System.out.println("wwrong. the answer was " + passedQuestion.getAnswer());
mistake.play();
mistake.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(Music aMusic){
voiceItem2.play();
}
}
);
}
}
….
@Override
public void show() {
voiceItem.play();
}
在Android上都没有播放voiceItem和voiceItem2(两者都是从passQuestion.getQuestionVoice()获得的),而是System.out.println(" wwrong。 回答是" + passedQuestion.getAnswer());打印出来,它在桌面上运行良好。
感谢您的帮助。 卡洛塔
答案 0 :(得分:0)
我认为您应该为资源声明另一个类。这种情况正在发生,因为Android设备的资源比台式机低,因此可能无法及时加载所需的资源。 声明另一个名为Resources的类或任何你喜欢的类。并在其中做这样的事情。
public class Resources {
public static Music myMusic1;
public static Music myMusic2;
public static Music myMusic3;
public Resources() {}
public static void load() {
myMusic1 = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic1.mp3"));
myMusic2 = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic2.mp3"));
myMusic3 = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic3.wav"));
}
}
现在在你的主Game类中调用这个方法。
public class MyGame extends Game{
@Override
public void create() {
//Here call your method
Resources.load();
}
@Override public void render(int width, int height) {}
}
现在进入您想要使用它的自定义类。
public class ItemsScreen {
//----
private void checkAnswer(int ans) {
if (ans == 3) {
//now play music where ever you like in your app.
Resources.myMusic1.play();
Resources.myMusic2.play();
} else {
//stop the music from any where in your app.
Resources.myMusic1.stop();
Resources.myMusic1.dispose();
Resources.myMusic3.play();
}
}
}
以上示例适用于所有设备i-e,android,ios,desktop。它经过测试。无论如何,这是一个通用的例子。