简体   繁体   English

达到某个值后如何触发声音

[英]How to trigger a sound once after a certain value has been reached

How can I adjust the source code so that when I level up, the level up sound should only trigger once? 如何调整源代码,以便在升级时,升级声音仅触发一次?

Now the Sound is reproduced all the time and doesn't stop but I want the level up sound to only trigger a single time for each level up. 现在,声音一直在再现,并且不会停止,但是我希望级别提升的声音只为每次级别提升触发一次。

private void Run(){
    int a = 0;
    do {
        showEmerald();
        a++;
    }while (a<15);//show more emeralds
    vanishEmerald();
    updateScreen();
    handler.postDelayed(this,  600);

    if (points>=25){
        level=2;
        levelup.start();
    }

The way you defined it once it hits over 25 points the If-clause is always fullfilled and will trigger the levelup. 一旦达到25分,您就定义了它的方式, 如果条件子句始终已满,将触发升级。 My advise would be to either define If outside the loop or / and reset the counter of the points after the levelup has been triggered. 我的建议是在循环之外定义If或/并在触发升级后重置点的计数器。

Something like this: 像这样:

if (points>=25){
    level++;
    levelup.start();
    points = 0
}

This way you don't have to increase the level manually everytime and it just increments further everytime by one. 这样,您不必每次都手动提高级别,而只需每次增加一级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM