Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Classes & mouse events — Gideros Forum

Classes & mouse events

NinjadoodleNinjadoodle Member
edited October 2015 in General questions
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

  • hgy29hgy29 Maintainer
    It looks like your lightSwitch Sprite is never added to the stage itself, hence it cannot catch events.
    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)

    B) 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).
    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)
            self:addChild(sprite)
    	container:addChild(self)
    end

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you! This helps a lot :)
  • So, the above works perfectly :) Now however I'm struggling to do the same with a MovieClip.
    LightSwitch = Core.class(Sprite)
     
    function LightSwitch:init(x, y, container)
    	local frames = {}
     
    	frames[1] = Bitmap.new(Texture.new("media/lightSwitch.png", true))
    	frames[2] = Bitmap.new(Texture.new("media/lightSwitchOn.png", true))
     
    	local mc = MovieClip.new{
    		{1, 1, frames[1]}, 
    		{2, 2, frames[2]}, 
    	}
    	mc:setPosition(x, y)
    	self:addChild(mc)
    	container:addChild(mc)
    end
    Thanks again for any help :)
  • hgy29hgy29 Maintainer
    edited October 2015 Accepted Answer
    Your last line should be:
    container:addChild(self)
    not
    container:addChild(mc)

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • Oh my god! I don't know how I missed that :)

    Thanks heaps! I think it's time to get away from the computer haha
Sign In or Register to comment.