LibGdx如何仅在Actor之间重叠开始时播放声音?

时间:2018-07-26 16:20:20

标签: android-studio libgdx

我正在努力做一个Ouch!仅在球与墙Actor重叠开始时才进行声音播放。因此,如果保持重叠,除非演员停止重叠然后再重叠,否则我不希望声音播放。

这是我尝试播放ouch声音的代码的一部分:

for (BaseActor wallActor : BaseActor.getList(mainStage, "com.mygdx.game.WallBlock")){
        ball.preventOverlap(wallActor);

        ouch.play();
    }

请提供一个示例,说明如何进行此工作。

1 个答案:

答案 0 :(得分:1)

您没有发布太多代码,但是解决方案很简单,请在球形演员中保持布尔重叠,并且只有当布尔值从false变为true时才播放声音。

伪代码:

public class Ball
{
    private boolean overlapping;
    private YourGame game; //could be a screen too or simply a world object
    // other ball fields ...


    public void update()
    {
        boolean wasOverlapping = overlapping;
        overlapping = isOverlapping();

        if(!wasOverlapping && overlapping)
            ouch.play();

        if(!overlapping && wasOverlapping)
        {
            //you could also play a sound when it stops overlapping here!
        }
    }

    private boolean isOverlapping()
    {
        for(Wall wall : game.getWalls())
        {
            if(wall.overlap(this)) //this refers to the ball
                return true;
        }

        return false;
    }
}