diff --git a/js/ohms-law/model/OhmsLawModel.js b/js/ohms-law/model/OhmsLawModel.js index 6c08795..9b7ddc9 100644 --- a/js/ohms-law/model/OhmsLawModel.js +++ b/js/ohms-law/model/OhmsLawModel.js @@ -9,57 +9,28 @@ define( function( require ) { 'use strict'; // modules - var BooleanProperty = require( 'AXON/BooleanProperty' ); var DerivedProperty = require( 'AXON/DerivedProperty' ); var inherit = require( 'PHET_CORE/inherit' ); var NumberProperty = require( 'AXON/NumberProperty' ); var OhmsLawConstants = require( 'OHMS_LAW/ohms-law/OhmsLawConstants' ); var ohmsLaw = require( 'OHMS_LAW/ohmsLaw' ); - var Sound = require( 'VIBE/Sound' ); - - // audio - var addBatteryAudio = require( 'audio!OHMS_LAW/add-battery' ); - var removeBatteryAudio = require( 'audio!OHMS_LAW/remove-battery' ); /** * @constructor */ function OhmsLawModel() { - var self = this; - // @public {Property.} in volts this.voltageProperty = new NumberProperty( OhmsLawConstants.VOLTAGE_RANGE.getDefaultValue() ); // @public {Property.} in Ohms this.resistanceProperty = new NumberProperty( OhmsLawConstants.RESISTANCE_RANGE.getDefaultValue() ); - // @public {Property.} - this.soundActiveProperty = new BooleanProperty( true ); - // @public {Property.} create a derived property that tracks the current in milli amps this.currentProperty = new DerivedProperty( [ this.voltageProperty, this.resistanceProperty ], function( voltage, resistance ) { return 1000 * voltage / resistance; } ); - - // Hook up the sounds that are played when batteries are added or removed. - var addBatterySound = new Sound( addBatteryAudio ); - var removeBatterySound = new Sound( removeBatteryAudio ); - - // play sounds when adding or removing a battery - this.voltageProperty.lazyLink( function( voltage, oldVoltage ) { - var newNumberBatteries = Math.floor( voltage / OhmsLawConstants.AA_VOLTAGE ); - var oldNumberBatteries = Math.floor( oldVoltage / OhmsLawConstants.AA_VOLTAGE ); - if ( self.soundActiveProperty.value ) { - if ( newNumberBatteries > oldNumberBatteries ) { - addBatterySound.play(); - } - else if ( newNumberBatteries < oldNumberBatteries ) { - removeBatterySound.play(); - } - } - } ); } ohmsLaw.register( 'OhmsLawModel', OhmsLawModel ); diff --git a/js/ohms-law/view/OhmsLawScreenView.js b/js/ohms-law/view/OhmsLawScreenView.js index fe49167..d2a5a6a 100644 --- a/js/ohms-law/view/OhmsLawScreenView.js +++ b/js/ohms-law/view/OhmsLawScreenView.js @@ -18,7 +18,14 @@ define( function( require ) { var ScreenView = require( 'JOIST/ScreenView' ); var SoundToggleButton = require( 'SCENERY_PHET/buttons/SoundToggleButton' ); var WireBox = require( 'OHMS_LAW/ohms-law/view/WireBox' ); + var Sound = require( 'VIBE/Sound' ); + var BooleanProperty = require( 'AXON/BooleanProperty' ); + var OhmsLawConstants = require( 'OHMS_LAW/ohms-law/OhmsLawConstants' ); + // audio + // The sounds themselves can be constants because there is only every one instance of OhmsLawScreenView. + var ADD_BATTERY_SOUND = new Sound( require( 'audio!OHMS_LAW/add-battery' ) ); + var REMOVE_BATTERY_SOUND = new Sound( require( 'audio!OHMS_LAW/remove-battery' ) ); /** * @param {OhmsLawModel} model @@ -26,6 +33,9 @@ define( function( require ) { */ function OhmsLawScreenView( model ) { + // {Property.} + var soundActiveProperty = new BooleanProperty( true ); + ScreenView.call( this ); // Circuit node with readout node @@ -51,7 +61,7 @@ define( function( require ) { var buttonCenterYOffset = 50; // empirically determined // Sound on/off toggle button - var soundToggleButton = new SoundToggleButton( model.soundActiveProperty, { + var soundToggleButton = new SoundToggleButton( soundActiveProperty, { scale: 1.15, stroke: 'gray', lineWidth: 0.5, @@ -65,8 +75,24 @@ define( function( require ) { radius: 30, centerX: controlPanel.left + controlPanel.width * 0.27, // empirically determined centerY: controlPanel.bottom + buttonCenterYOffset, - listener: function() { model.reset(); } + listener: function() { + model.reset(); + } } ) ); + + // Play sounds when adding or removing a battery + model.voltageProperty.lazyLink( function( voltage, oldVoltage ) { + var newNumberBatteries = Math.floor( voltage / OhmsLawConstants.AA_VOLTAGE ); + var oldNumberBatteries = Math.floor( oldVoltage / OhmsLawConstants.AA_VOLTAGE ); + if ( soundActiveProperty.value ) { + if ( newNumberBatteries > oldNumberBatteries ) { + ADD_BATTERY_SOUND.play(); + } + else if ( newNumberBatteries < oldNumberBatteries ) { + REMOVE_BATTERY_SOUND.play(); + } + } + } ); } ohmsLaw.register( 'OhmsLawScreenView', OhmsLawScreenView );