Hi guys
I've been away for a while working with HTML5, but just came back to check out whats happening with Gideros. It's awesome to see it growing
I have a quick question about mouse events when using 'classes'.
I have a 'classes.lua' file with ...
function createContainers(scene)
layerBG = Sprite.new()
layerMG = Sprite.new()
layerFG = Sprite.new()
scene:addChild(layerBG)
scene:addChild(layerMG)
scene:addChild(layerFG)
end
LightSwitch = Core.class(Sprite)
function LightSwitch:init(x, y, container)
local sprite = Bitmap.new(Texture.new("media/lightSwitch.png", true))
sprite:setAnchorPoint(0.5, 0.5)
sprite:setPosition(x, y)
container:addChild(sprite)
end |
Then I'm trying to create the switch in one of my levels.
Scene1 = gideros.class(Sprite)
function Scene1:init()
require "classes"
createContainers(self)
lightSwitch = LightSwitch.new(160, 160, layerBG)
lightSwitch2 = LightSwitch.new(240, 160, layerBG)
local function lightSwitchTouch(button, event)
if lightSwitch:hitTestPoint(event.x, event.y) then
sceneManager:changeScene('scene2', 2, SceneManager.fade, easing.outBack)
lightSwitch:removeEventListener(Event.MOUSE_UP, lightSwitchTouch, lightSwitch)
end
end
lightSwitch:addEventListener(Event.MOUSE_UP, lightSwitchTouch, lightSwitch)
end |
The problem is, that it doesn't seem to be responding to any mouse/touch events.
If anyone could help, that would be awesome
Thanks in advance!
Comments
In your LightSwitch:init, you link the Bitmap sprite directly to the container, the LightSwitch is never made part of the scene. There are many things you can do:
A) Make your LightSwitch object inherit from Bitmap and let it be the real switch visual (issue is that you can't change the constructor signature, it will have to match Bitmap constructor)
Redefine LightSwitch:addEventListener so that it calls the inner sprite addEventListener()
C) Add the inner Bitmap into the LightSwitch sprite, and add the LightSwitch sprite to the stage (probably the simplest way).
Likes: Ninjadoodle
Likes: Ninjadoodle
Thanks heaps! I think it's time to get away from the computer haha