Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Events, Frames, Stages.. oh my! — Gideros Forum

Events, Frames, Stages.. oh my!

myspsmysps Member
edited June 2012 in General questions
hey guys..

still learning.. ok i'm working from the e-book template, on page1.lua there is a toy moving.. and worked well until i added sound to the page from page4.lua.. i've read and read, classes and events however, i guess i don't get it... here is my code, i'm guessing there is a conflict with those two events? i can have more than one mousedown correct?
Page1 = gideros.class(Sprite)
 
 
 
function Page1:init( pageNo, parent)
	self.no = pageNo
 
	self.background = Bitmap.new(TextureRegion.new(Texture.new("gfx/background.png", true)))	
 
	self.toy =  Bitmap.new(TextureRegion.new(Texture.new("gfx/doll.png", true )))	
	self.toy:setX(400)
	self.toy:setY(300)
 
	self.ball =  Bitmap.new(TextureRegion.new(Texture.new("gfx/ball.png", true)))	
	self.ball:setX(600)
	self.ball:setY(100)	
	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(645)
 
	 self.buttonup2 =  Bitmap.new(TextureRegion.new(Texture.new("gfx/2-up.png", true)))	
	self.buttonup2:setX(140)
	self.buttonup2:setY(645)
 
 
	self:addChild(self.background)
	self:addChild(self.toy)	
	self:addChild(self.buttonup)
	self:addChild(self.buttonup2)
	self:addChild(self.ball)
 
	self.sound = Sound.new("music/Japanese/cat.mp3")
 
 
	self.toy.isMoving = false
 
	self:addEventListener(Event.ADDED_TO_STAGE, self.onAddedToStage, self)	
	self:addEventListener(Event.REMOVED_FROM_STAGE, self.onRemovedFromStage, self)	
 
end
 
function Page1:onAddedToStage()
	-- we need mouse functions to interact with the toy
	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 Page1:onRemovedFromStage()
 
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end	
 
 
 
 
 
 
function Page1:onMouseDown(event)
	-- touch event begins here. you need to get the initial x and y, we will use them later
	if (self.toy:hitTestPoint(event.x, event.y) == true) then 
		self.toy.isMoving = true
		self.toy.x0 = event.x
		self.toy.y0 = event.y
		print("toy")
	end
 
end		
 
function Page1:onMouseUp(event)
	-- touch event ends here. we must get out of the mode.
	if (self.toy.isMoving == true) then  self.toy.isMoving = false end
 
end		
 
function Page1:onMouseMove(event)
 
	if (self.toy.isMoving == true) then  
		-- calculate the difference
		local dx = event.x - self.toy.x0
		local dy = event.y - self.toy.y0
		--store the new x,y of touch coordinates
		self.toy.x0 = event.x
		self.toy.y0 = event.y
		--set the new coordinates
		self.toy:setX(dx + self.toy:getX())
		self.toy:setY(dy + self.toy:getY())
	end
 
end		
 
 
 
 
 
function Page1:onMouseDown(event)
	-- touch event begins here. you need to get the initial x and y, we will use them later
	if (self.buttonup:hitTestPoint(event.x, event.y) == true) then 
		self.sound:play()
 
	end
 
end
 
 
function Page1:onMouseDown(event)
	-- touch event begins here. you need to get the initial x and y, we will use them later
	if (self.buttonup2:hitTestPoint(event.x, event.y) == true) then 
		self.sound:play()
 
	end
 
end
 
 
 
function Page1:onEnterFrame(event)
	self.ball.scaleX = self.ball.scaleX  + self.ball.inc
	if (self.ball.scaleX < 0.85) 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

right? thanks for any tidbits!

Dislikes: marbolec, loves_oi

+1 -1 (+0 / -2 )Share on Facebook

Comments

  • evsevs Member
    @mysps

    No - only one mousedown etc will work, do all your logic there, try something like
    function Page1:onMouseDown(event)
    	-- touch event begins here. you need to get the initial x and y, we will use them later
    	if (self.toy:hitTestPoint(event.x, event.y) == true) then 
    		self.toy.isMoving = true
    		self.toy.x0 = event.x
    		self.toy.y0 = event.y
    		print("toy")
     
    	elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then 
    		self.sound:play()
     
    	end
     
    end
    cheers

    evs
  • myspsmysps Member
    gotcha!! thanks so much .. i'll get there sooner or later :D
  • how to avoid sound overlapping when the button is touched twice / repeatedly?

    elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then
    self.sound:play()

    end
  • talistalis Guru
    edited August 2012
    You can create a boolean paremeter and check that one. In addition sound:play() is returning a channel for you to keep track of it.
    soundplay=false
     
    if(soundplay==false) then
    		  channel=sound:play(0,math.huge)
    		  soundplay=true
    else
    		  channel:stop()
    		  soundplay=false
    end
  • I am not aware of a function like channel:isplaying() but you can easilly write one your self.
  • @mysps did you able to solve your problem?
  • @talis : I have tried your code, but still not work :(
    Here's my code before:
    where should I put your code?
     -- create a mouse joint on mouse down
    function self:onMouseDown(event)
    	if sprite:hitTestPoint(event.x, event.y) then
    		local jointDef = b2.createMouseJointDef(ground, body1, event.x, event.y, 100000)
    		mouseJoint = self.world:createJoint(jointDef)
    		self.channel  = self.sound:play()
    		self.channel:setVolume(0.3)
    	end
    end
Sign In or Register to comment.