It looks like you're new here. If you want to get involved, click one of these buttons!
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 |
Comments
No - only one mousedown etc will work, do all your logic there, try something like
evs
elseif (self.buttonup2:hitTestPoint(event.x, event.y) == true) then
self.sound:play()
end
Here's my code before:
where should I put your code?