Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Stop sound.play on button click — Gideros Forum

Stop sound.play on button click

myspsmysps Member
edited March 2013 in Game & application design
So I have sound buttons and works great until I click the next button. The sound continues.. I'm not sure how to stop :/

Still learning.. If anyone could point me in understanding why?
Page10 = gideros.class(Sprite)
 
-- duck
 
function Page10:init( pageNo, parent)
	self.no = pageNo
 
	self.background = Bitmap.new(TextureRegion.new(Texture.new("gfx/duckback.jpg", true)))	
 
	self.rooster =  Bitmap.new(TextureRegion.new(Texture.new("gfx/roosterwood.png", true )))	
	self.rooster:setX(0)
	self.rooster:setY(450)
 
	self.ball =  Bitmap.new(TextureRegion.new(Texture.new("gfx/duck.png", true)))	
	self.ball:setX(250)
	self.ball:setY(0)	
	self.ball.scaleX = 1
	self.ball.inc = -0.001
 
    self.buttonup =  Bitmap.new(TextureRegion.new(Texture.new("gfx/1-up.png", true)))	
	self.buttonup:setX(20)
	self.buttonup:setY(695)
 
	 self.buttonup2 =  Bitmap.new(TextureRegion.new(Texture.new("gfx/2-up.png", true)))	
	self.buttonup2:setX(110)
	self.buttonup2:setY(695)
 
 
	self:addChild(self.background)
	self:addChild(self.rooster)	
	self:addChild(self.buttonup)
	self:addChild(self.buttonup2)
	self:addChild(self.ball)
 
	self.sound = Sound.new("mp3/Japanese/cat.mp3")
 
 
	self.rooster.isMoving = false
 
	self:addEventListener(Event.ADDED_TO_STAGE, self.onAddedToStage, self)	
	self:addEventListener(Event.REMOVED_FROM_STAGE, self.onRemovedFromStage, self)	
 
end
 
function Page10:onAddedToStage()
	-- we need mouse functions to interact with the rooster
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
 
end
 
function Page10:onRemovedFromStage()
 
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end	
 
 
 
 
 
 
function Page10:onMouseDown(event)
	-- touch event begins here. you need to get the initial x and y, we will use them later
	if (self.rooster:hitTestPoint(event.x, event.y) == true) then 
		self.rooster.isMoving = true
		self.rooster.x0 = event.x
		self.rooster.y0 = event.y
		print("rooster")
 
 
	elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then 
		self.sound:play()
 
	elseif (self.buttonup:hitTestPoint(event.x, event.y) == true) then 
		self.sound:play()
	end
 
 
 
 
 
end		
 
function Page10:onMouseUp(event)
	-- touch event ends here. we must get out of the mode.
	if (self.rooster.isMoving == true) then  self.rooster.isMoving = false end
 
end		
 
function Page10:onMouseMove(event)
 
	if (self.rooster.isMoving == true) then  
		-- calculate the difference
		local dx = event.x - self.rooster.x0
		local dy = event.y - self.rooster.y0
		--store the new x,y of touch coordinates
		self.rooster.x0 = event.x
		self.rooster.y0 = event.y
		--set the new coordinates
		self.rooster:setX(dx + self.rooster:getX())
		self.rooster:setY(dy + self.rooster:getY())
	end
 
end		
 
 
 
 
function Page10:onEnterFrame(event)
	self.ball.scaleX = self.ball.scaleX  + self.ball.inc
	if (self.ball.scaleX < 0.90) and self.ball.inc < 0  then self.ball.inc = -self.ball.inc end 
	if (self.ball.scaleX > 1) and self.ball.inc > 0 then self.ball.inc = -self.ball.inc end 
	self.ball:setScaleX(self.ball.scaleX)
 
 
end


Comments

  • petecpetec Member
    If you use a sound channel to play the sound then you can stop the sound channel. So use something like
    self.soundChannel=self.sound:play()
    to play the sound and then
    self.soundChannel:stop()
    to stop it.
  • myspsmysps Member
    edited March 2013
    I'm trying but both sounds overlap. i still dont have an understanding of it
    function Page3:onMouseDown(event)
    	-- touch event begins here. you need to get the initial x and y, we will use them later
    	if (self.moon:hitTestPoint(event.x, event.y) == true) then 
    		self.moon.isMoving = true
    		self.moon.x0 = event.x
    		self.moon.y0 = event.y
    		print("moon")
     
    	elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then 
    		self.soundChannel = self.sound:play()
     
     
    	elseif (self.buttonup:hitTestPoint(event.x, event.y) == true) then 
    		self.soundChannel  = self.sound2:play()
     
    	end
     
     
    		if (self.buttonup2:hitTestPoint(event.x, event.y) == true)
    	or (self.buttonup:hitTestPoint(event.x, event.y) == true)then
    		if playingSound == false then
    			print("play")
    			playingSound = true
    		end
    	end
    	function onSoundComplete(event)
    		print("sound end")
    		playingSound = false
                    self.soundChannel:stop()
    	end
    	if playingSound then
    		soundChannel:addEventListener(Event.COMPLETE, onSoundComplete)
    	end
     
    end
  • If the sounds overlap then you'll need to store their sound channels in different variables so you can access them. Also, in a few spots you're declaring global variables instead of keeping them in self. I marked a few spots with notes below, but I also don't see where you're trying to stop the sounds in this code.
    function Page3:onMouseDown(event)
    	-- touch event begins here. you need to get the initial x and y, we will use them later
    	if (self.moon:hitTestPoint(event.x, event.y) == true) then 
    		self.moon.isMoving = true
    		self.moon.x0 = event.x
    		self.moon.y0 = event.y
    		print("moon")
     
    	elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then 
    		self.soundChannel = self.sound:play()
     
     
    	elseif (self.buttonup:hitTestPoint(event.x, event.y) == true) then 
                    -- NOTE: Change to self.soundChannel2 = 
    		self.soundChannel  = self.sound2:play()
     
    	end
     
     
    		if (self.buttonup2:hitTestPoint(event.x, event.y) == true)
    	or (self.buttonup:hitTestPoint(event.x, event.y) == true)then
    		if playingSound == false then
    			print("play")
    			playingSound = true
                            -- NOTE: change to self.channel or local if you don't need to use again
    			channel = page1Sound:play(0, 1)
    			channel:setVolume(1)
    		end
    	end
    	function onSoundComplete(event)
    		print("sound end")
    		playingSound = false
    	end
    	if playingSound then
                   -- NOTE: you probably want self.soundChannel here
    		soundChannel:addEventListener(Event.COMPLETE, onSoundComplete)
    	end
     
    end
  • myspsmysps Member
    Hmm. i guess thats my point. I have no clue where and how to stop the sound. I've been checking the sample code from the forums but don't have a clear understanding.

  • When do you want the sound to stop? You just need to call stop() on the sound channels like @petec said above. Just like your setVolume(1) call.
    self.soundChannel = self.sound2:play()
     
    function Page3:someEventHandler(e)
      if self.bDisableSound then
         self.soundChannel:stop()
      end
    end
  • myspsmysps Member
    Maybe I'm confusing myself? Maybe I should create a new function.

    Inside of the function Page3:onMouseDown(event)

    I have tried to add the sound channels.

    My idea is too add 7 mp3 sounds and on the mousedown I would like it to play the sound and if another button is clicked that sound will stop and the next will play on mousedown.

    does that make since?

  • zvardinzvardin Member
    Accepted Answer
    Ok, so you can only have one active sound? You can do something like this:
    -- this function is so we don't have to do the same check for each sound in our code
    function Page3:playSound(sound)
       if self.soundChannel and self.soundChannel:isPlaying() then
           self.soundChannel:stop()
       end
       self.soundChannel = sound:play()
    end
     
    function Page3:onMouseDown(event)
    	-- touch event begins here. you need to get the initial x and y, we will use them later
    	if (self.moon:hitTestPoint(event.x, event.y) == true) then 
    		self.moon.isMoving = true
    		self.moon.x0 = event.x
    		self.moon.y0 = event.y
    		print("moon")
     
    	elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then 
                    self:playSound(self.sound)
    	elseif (self.buttonup:hitTestPoint(event.x, event.y) == true) then 
    		self:playSound(self.sound2)
    	end
     
     
     
     
    --[[ You don't need this section to test if the buttons were hit again, if self.soundChannel is true that means we have an active sound channel
    		if (self.buttonup2:hitTestPoint(event.x, event.y) == true)
    	or (self.buttonup:hitTestPoint(event.x, event.y) == true)then
    		if playingSound == false then
    			print("play")
    			playingSound = true
                            -- NOTE: change to self.channel or local if you don't need to use again
    			channel = page1Sound:play(0, 1)
    			channel:setVolume(1)
    		end
    	end
    --]]
     
     
    end

    This way we know if self.soundChannel has a value then there is a soundChannel to check and then we can check the if it's playing using isPlaying(). Hope this helps!
  • myspsmysps Member
    ok. i think i have it a little now.

    The sound function must be called first function Page3:playSound(sound)

    "sound"

    As it is not an event correct?

    Then I must create the variable. sound playing and then stopped ..

    Still wrapping my head around. thanks for the explanation. on to study more.

    thanks so much!
Sign In or Register to comment.