I need an on/off button. After clicking it , i want to activate/deactivate some property (and if posible change the image). Is there an example which does this job? Thanks in advance.
In switch.lua,i copy pasted your code .It works alone. This is it:
Switch = Core.class(Sprite)--constructor for Buttonfunction Switch:init(button1, button2)
self.button1 = button1
self.button2 = button2
self:addChild(button1)
self.state =false
self.button1:addEventListener("click", self.changeState, self)
self.button2:addEventListener("click", self.changeState, self)
self.onChange = Event.new("onChange")--stage:addChild(switch)endfunction Switch:changeState()
self.state =not self.state
if self.state thenif self:contains(self.button1)then
self:removeChild(self.button1)end
self:addChild(self.button2)elseif self:contains(self.button2)then
self:removeChild(self.button2)end
self:addChild(self.button1)end
self.onChange.state = self.state
self:dispatchEvent(self.onChange)endlocal button1 = Button.new(Bitmap.new(Texture.new("resources/images/button1_up.jpg")), Bitmap.new(Texture.new("resources/images/button1_down.png")))local button2 = Button.new(Bitmap.new(Texture.new("resources/images/button2_up.png")), Bitmap.new(Texture.new("resources/images/button2_down.png")))
switch = Switch.new(button1, button2)function Switch:getState()return self.state
end
switch:addEventListener("onChange", function(e)print(e.state)--stage:addChild(switch)if e.state then--show popupelse--hide popuoend--stage:addChild(switch)end)
stage:addChild(switch)---------I think we should put this line to another body
But when i attempt to integrate change:scene such as the code above , a coincidence appears . I encountered the similar problems before. I added a sample photo.Checkbox page and on/off state page coincide(However , i get no error).To solve this problem i changed the location of the line
stage:addChild(switch)
I put this line inside functions to call it when the result page works.But it didn't work. How can i solve it ?
hello firstly object switch was not meant to be a scene. So you can't use it in scene manager as scene. You should create separate scene, and then add switch to that scene.
And if you are using scene manager, you don't need to add anything to stage. Only scene manager should be added to stage. Everything else should be added to the proper scene instance, where you want it to appear.
if(tit == xx)then
sceneManager:changeScene("switch", 1, SceneManager.flipWithFade, easing.outBack)print"------We are inside----"end
It prints ------We are inside---- namely, it can enter if block. I think , i should do something another inside switch.lua but don't know what i should do.
I changed the switch.lua as this:
These lines you're declaring globals and the switch line may actually interfere with your switch scene. At very least, I would not name a global variable the same as your class.
@ar2rsawseen said 'firstly object switch was not meant to be a scene. So you can't use it in scene manager as scene.' but you still have it as a scene:
When I started with Gideros I had terrible trouble with classes and could hardly understand any of the example code - I certainly couldn't follow the switch code above. So I set to with simpler examples. For example, when I was trying to get to grips with scenemanager, I put simple objects in different scenes (a single sprite with a bitmap, different on each scene so I could tell which scene I was in) and added mouse down eventlisteners to the sprites to change from scene to scene until I got the hang of it.
I did a similar thing to get the hang of using classes. I made the simplest of classes to just add a sprite with a graphic to the scene and played around to find out how to do use the class to add the sprite to any scene I wanted.
I think using someone else's code as you have makes learning very difficult. When things don't work, it's hard to know what to do as it's hard to make sense of the code. I would really recommend that you try making the most simple examples of each process you are trying to get working and then, when you've got those under control, start upping the complication. There's nothing like a good play to help clarify things!
and the vital point is that : i call this function in results.lua:
if(tit == xx)then
sceneManager:changeScene("switch", 1, SceneManager.flipWithFade, easing.outBack)print"------We are inside----"--i called the function which i defined in switch.lua-end
Comments
http://www.giderosmobile.com/forum/discussion/1456/button-onoff-state
In other page , i call changeScene in this line:
How can i solve it ?
firstly object switch was not meant to be a scene. So you can't use it in scene manager as scene.
You should create separate scene, and then add switch to that scene.
And if you are using scene manager, you don't need to add anything to stage. Only scene manager should be added to stage.
Everything else should be added to the proper scene instance, where you want it to appear.
It prints ------We are inside---- namely, it can enter if block. I think , i should do something another inside switch.lua but don't know what i should do. I changed the switch.lua as this:
What should i do ?
I did a similar thing to get the hang of using classes. I made the simplest of classes to just add a sprite with a graphic to the scene and played around to find out how to do use the class to add the sprite to any scene I wanted.
I think using someone else's code as you have makes learning very difficult. When things don't work, it's hard to know what to do as it's hard to make sense of the code. I would really recommend that you try making the most simple examples of each process you are trying to get working and then, when you've got those under control, start upping the complication. There's nothing like a good play to help clarify things!
i solved the problem after writing a function which contains the below code