当我按下我的开关以显示“开启”位置时,我该如何让它做某事,例如,将它链接到一个将玩家的音量设置为0的方法?我在猜它的接口FieldChangeListener
?
我的所有实现都发生在MainScreen
类。
Bitmap switch_left = Bitmap.getBitmapResource("switch_left.png");
Bitmap switch_right = Bitmap.getBitmapResource("switch_right.png");
Bitmap switch_left_focus = Bitmap.getBitmapResource("switch_left_focus.png");
Bitmap switch_right_focus = Bitmap.getBitmapResource("switch_right_focus.png");
LabeledSwitch silentSwitch = new LabeledSwitch(switch_left, switch_right, switch_left_focus, switch_right_focus, "on", "off", true );
JustifiedHorizontalFieldManager silent = new JustifiedHorizontalFieldManager( new LabelField( "Silent Mode" ), silentSwitch, false, USE_ALL_WIDTH );
silent.setPadding(5,5,5,5);
add(silent);
我导入了一个名为OpenGlSpriteDemo的游戏演示,并查看了他们如何使用字段更改侦听器实现启动按钮,因此我尝试为Labeledswitch执行此操作。我正朝着正确的方向前进吗?
LabeledSwitch silentSwitch = new LabeledSwitch(switch_left, switch_right, switch_left_focus, switch_right_focus, "on", "off", false );
silentSwitch.setChangeListener(this);
public void fieldChanged(Field arg0, int arg1)
{
//If user sets the switch to on, reduce the volume to 0,
// else if user sets the switch to false, change it back
// to the default volume
}
答案 0 :(得分:1)
使用布尔标志来判断它是否已经是静音,并使用了卷变量的get和set方法并将其传递给:
volume.setLevel(getVolume());
和
boolean isSilent = false;
public void fieldChanged(Field field, int context)
{
if(!isSilent && field == silentSwitch)
{
setVolume(0);
isSilent = true;
}
else if(field == silentSwitch && isSilent)
{
setVolume(20);
isSilent = false;
}
}