Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Button Class not working with Wax? — Gideros Forum

Button Class not working with Wax?

ljp1203ljp1203 Member
edited November 2012 in General questions
Hi all!

I have run into a slight issue. I am using Wax by @bowerandy and I have added the lines of code needed to incorporate Wax into SceneManager. Now I want to use the button.lua class by @ar2rsawseen, but the buttons won't show up after I added the lines of code from BhWaxSceneDemo.

Below is the code I am using:
mainmenu = Core.class(Sprite)
function mainmenu:init()
 
	--Create Event Button	
	local createBtn = Bitmap.new(Texture.new("Images/btn1.png"))
	local createBtn_over = Bitmap.new(Texture.new("Images/btn1_over.png"))
 
	--The Actual Button and Listener
	local button1 = Button.new(createBtn, createBtn_over)
	button1:addEventListener("click",
		function()
			self.sceneManager:changeScene("createEvent", 1, SceneManager.flipWithFade)
		end)
 
	--Add Button To Scene
	button1:setPosition(70, 200)
	stage:addChild(button1)	
	-------------------------------------------------------------------------------
	--Create Event Button	
	local viewBtn = Bitmap.new(Texture.new("Images/btn2.png"))
	local viewBtn_over = Bitmap.new(Texture.new("Images/btn2_over.png"))
 
	--The Actual Button and Listener
	local button2 = Button.new(viewBtn, viewBtn_over)
	button2:addEventListener("click",
		function()
			self.sceneManager:changeScene("ViewEdit")
		end)
 
	--Add Button To Scene
	button2:setPosition(70,450)
	stage:addChild(button2)
	-------------------------------------------------------------------------------
	--Create Event Button	
	local settingsBtn = Bitmap.new(Texture.new("Images/btn3.png"))
	local settingsBtn_over = Bitmap.new(Texture.new("Images/btn3_over.png"))
 
	--The Actual Button and Listener
	local button3 = Button.new(settingsBtn, settingsBtn_over)
	button3:addEventListener("click",
		function()
			self.sceneManager:changeScene("Settings")
		end)
 
	--Add Button To Scene
	button3:setPosition(70, 700)
	stage:addChild(button3)
	-------------------------------------------------------------------------------	
	stage:addChild(self.sceneManager)
	mainmenu.sharedInstance=self
 
end
Hopefully someone can help!

Thanks
-Landon

Dislikes: wretch

JLS
Lead Coder and Designer
+1 -1 (+0 / -1 )Share on Facebook
«1

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited November 2012
    Nope, not mine. Button's class is @atilim's.

    You need to use self when adding buttons as:
    self:addChild(button1)
    Because you are adding button to scene and want tot keep it with the scene and not to the global stage.

    ;)

  • Hi @ar2rsawseen ,

    That is the second time I've mixed you to up :( .
    I tried the line of code you showed me, but it still isn't showing up for some reason.
    And this is really hampering me from continuing my app :(

    Hopefully you can figure out why! :D

    Thanks!
    -Landon
    JLS
    Lead Coder and Designer
  • bowerandybowerandy Guru
    edited November 2012
    @ljp1203, I think you need to do what @ar2rsawseen suggests *and* check how you are invoking the "mainmenu". Assuming "mainmenu" is a scene then it should also be in your scene list table and you need to invoke it using scenemanager:changeScene() somewhere in your program (presumably in main.lua).

    best regards
  • Hi @bowerandy @ar2rsawseen ,

    Here is my main.lua , and maybe you can find the problem, because I can't seem to fix the problem with:
    self:addChild(button1)
    main.lua:
    UIApplication:sharedApplication():setStatusBarHidden_animated(false, false)
     
     
    --Background Image
    local background = Bitmap.new(Texture.new("Images/background.png"))
    stage:addChild(background)
     
    main=Core.class(Sprite)
    function main:init()
     
    	--SceneManager
    	self.sceneManager = SceneManager.new({
    		["login"] = login,
    		["mainmenu"] = mainmenu,
    		["createEvent"] = createEvent,
    		["Party"] = Party,
    		["Vacation"] = Vacation,
    		["Other"] = Other,
    		["ViewEdit"] = ViewEdit,
    		["Settings"] = Settings,
    		["Help"] = Help,
    		["FAQ"] = FAQ,
    		["About"] = About
    	})
     
    	stage:addChild(self.sceneManager)
    	main.sharedInstance=self
     
    	self.sceneManager:changeScene("mainmenu")
     
    end
    Thanks!
    -Landon
    JLS
    Lead Coder and Designer
  • @ljp1203 you want to say that before adding BhWax lines everything worked fine?
  • @ar2rsawseen yes that is what I am saying :)

    Thanks!
    -Landon
    JLS
    Lead Coder and Designer
  • @ljp1203, unfortunately it's virtually impossible to guess what's going on if we only see part of te picture. Why don't you zip up the entire project (or preferably a simplified version of it that still goes wrong) and post it to this thread?

    Best regards
  • Hi @bowerandy @ar2rsawseen ,

    Here is my attached app file. Thanks for the help, and don't steal my idea :p

    Have a good evening!
    -Landon

    JLS
    Lead Coder and Designer
  • @ljp1203

    i also had this problem when i was using uikit and my problem was in uikit there is also button class and b'coz of that whenever i calls button.new() it was calling the class in uikit and not the our lua one and it was creating the problem might be you also have similar problem can you try giving any new class name to the button class (our lua one)

    i had not tried your example and giving suggestions blindly so sorry if it will not work

    :)
  • bowerandybowerandy Guru
    edited November 2012 Accepted Answer
    @ljp1203, I've just taken a quick look. Commenting out the BhWax stuff doesn't get it working so its not that. I think the main problem is that you aren't ever creating your main object. The first thing to do is to add:
    main.new()
    as the last line in main.lua. Then you have the following two lines at the end of "main menu.lua" which give an error and I think are redundant. Remove them.
    stage:addChild(self.sceneManager)
    mainmenu.sharedInstance=self
    Now you need to alter all of the places where you need to reference your scenemanager object. I think there is some confusion over where you hold this. Part of the code keeps it as a field off your main object and the rest of the code expects it in your mainmenu object. I think the correct way to find it (in your case) is to use "main.sharedInstance.scenemanager". So, for example, change "mainmenu.lua" to:
    mainmenu = Core.class(Sprite)
    function mainmenu:init()
     
    	--Create Event Button	
    	local createBtn = Bitmap.new(Texture.new("Images/btn1.png"))
    	local createBtn_over = Bitmap.new(Texture.new("Images/btn1_over.png"))
     
    	--The Actual Button and Listener
    	local button1 = Button.new(createBtn, createBtn_over)
    	button1:addEventListener("click",
    		function()
    			main.sharedInstance.sceneManager:changeScene("createEvent", 1, SceneManager.flipWithFade)
    		end)
     
    	--Add Button To Scene
    	button1:setPosition(70, 200)
    	self:addChild(button1)
    	-------------------------------------------------------------------------------
    	--Create Event Button	
    	local viewBtn = Bitmap.new(Texture.new("Images/btn2.png"))
    	local viewBtn_over = Bitmap.new(Texture.new("Images/btn2_over.png"))
     
    	--The Actual Button and Listener
    	local button2 = Button.new(viewBtn, viewBtn_over)
    	button2:addEventListener("click",
    		function()
    			main.sharedInstance.sceneManager:changeScene("ViewEdit", 1, SceneManager.flipWithFade)
    		end)
     
    	--Add Button To Scene
    	button2:setPosition(70,450)
    	self:addChild(button2)
    	-------------------------------------------------------------------------------
    	--Create Event Button	
    	local settingsBtn = Bitmap.new(Texture.new("Images/btn3.png"))
    	local settingsBtn_over = Bitmap.new(Texture.new("Images/btn3_over.png"))
     
    	--The Actual Button and Listener
    	local button3 = Button.new(settingsBtn, settingsBtn_over)
    	button3:addEventListener("click",
    		function()
    			main.sharedInstance.sceneManager:changeScene("Settings", 1, SceneManager.flipWithFade)
    		end)
     
    	--Add Button To Scene
    	button3:setPosition(70, 700)
    	self:addChild(button3)
    end
    This also adds the necessary parameters to the changeScene() calls that were missing. If you run this you'll see that the main menu comes up and the "View/Edit Events" button works fine. The other buttons give errors but I'll leave you to sort those out in the other scene files.

    Just as an aside, I think you would do well to use a capital letter to start all class names. It is common practice in Lua to use capitals for global variables and since class names are also global this fits well. This would allow you to more easily distinguish between Lua variables that are actually classes and those that are instances of those classes (which would continue to start with lowercase letter).

    best regards
  • ljp1203ljp1203 Member
    edited November 2012
    Hi @bowerandy ,

    Thanks for the response. Yet I can't seem to replicate what I had before I added the BhWax lines, and the View/Edit Button doesn't appear as you said. I copied all of what you gave me and put main.new() in my main.lua file also. I attached my file again as a new one so you can look at it again, because it isn't working still.

    And again, thanks for your help!
    -Landon
    JLS
    Lead Coder and Designer
  • bowerandybowerandy Guru
    edited November 2012
    @ljp1203,

    Here is the zipped up project that "works". I only made the changes that I detailed above. It includes the BhWax line to show the status bar, so that is working too.

    If it doesn't work for you then perhaps your Gideros player is broken? Did you read @hgvyas123 's response above? Have you made sure that you aren't including the UIKit plugin in your player?

    If you have checked these things and nothing works then you need to start playing detective. This is what every programmer has to learn to do. Start again with a blank project. Add one button. Does it appear? Add the BhWax line, does it work? Add the scenemanager and one scene. Does it still work? In short, try building the project up from scratch, line by line, until you find where it fails.

    best regards
    zip
    zip
    PLANIT.zip
    738K
  • ljp1203ljp1203 Member
    edited November 2012
    @bowerandy @hgvyas123

    I took your advice and tried to make a new Project from scratch, and I keep getting stuck at the button. I can see the background I added. But I can't seem to get my buttons on the screen still. I have changed their position and it still doesn't work. I have also changed the button.lua class name to PlanitBtn.lua and changed all of the lines inside it to PlanitBtn that had to be changed from Button. I am using Retina Display with my app if that helps in any way.

    I also tried to comment out the BhWax lines and also disabled init.lua from execution and that still didn't work. I am dead in the water :p

    I have attached my sample project with all of the pictures and everything. Its very minimalistic because @bowerandy told me to build it up to the point where it doesn't work.

    Have a great evening!
    -Landon
    JLS
    Lead Coder and Designer
  • bowerandybowerandy Guru
    edited November 2012 Accepted Answer
    Hey @ljp1203, when people help you out, you ought to at least look at what they do for you. I posted my version of your project. Did you look at it?

    In your recent cut down project you have *at least* two issues. The first is that you have put the main.new() line inside the main:init() function. Hence it never gets called. Do you remember me saying it should go as the last line in main.lua? To avoid doubt, your main.lua should look like this:
    UIApplication:sharedApplication():setStatusBarHidden_animated(false, false)
     
    --Background Image
    local background = Bitmap.new(Texture.new("Images/background.png"))
    stage:addChild(background)
     
    main=Core.class(Sprite)
    function main:init()
    	self.sceneManager = SceneManager.new({
    		["Mainmenu"] = Mainmenu
    	})
     
    	stage:addChild(self.sceneManager)
    	main.sharedInstance=self		
    	main.sharedInstance.sceneManager:changeScene("Mainmenu")
     
            -- Move this from here (how can it ever get called?)
            -- main.new()
    end
     
    -- To here..
    main.new()
    The next issue is that you haven't modified your PlanitBtn class correctly. Yes, you changed the filename from Button.lua to PlanItBtn.lua but this actually makes no difference. You have to go through the file and change all references to Button to PlanitBtn. You can do this using the Replace All command in the editor.
  • Hi @bowerandy

    I didn't mean to attach that file, I ment to attach the one that you had showed me. Sorry :(

    I have another question. I wanted to know how I would add a datePicker to my app. I followed this tutorial by @OZApps .

    http://howto.oz-apps.com/2012/10/using-date-pickers.html

    But everytime I try to add it to my project it crashes, and says:

    WARNING: No object named 'CGRect' found.
    Party.lua:21: attempt to call global 'CGRect' (a nil value)
    stack traceback:
    Party.lua:21: in function 'init'
    [string "property.lua"]:31: in function 'new'
    scenemanager.lua:277: in function 'changeScene'
    CreateEvent.lua:12: in function

    I have attached my working Project up to this point so you could take a look at it. Also, where would I find documentation to add such things as a textBox, imagePicker, etc?

    Again thanks for all your help!
    -Landon
    zip
    zip
    PLANIT.zip
    714K
    JLS
    Lead Coder and Designer
  • OZAppsOZApps Guru
    Accepted Answer
    @landon,
    you will have to go a little slow than the pace you are currently on. You are jumping from topic to topic or rather issue to issue very fast and not reading the resolutions.

    @Andy's demo, my tutorial, etc are all based on running samples that we got working at our end. Which means that if you are unable to achieve the same, they you are missing some steps.

    The CGRect is a structure and is made available via the structs.lua file which is available in the lib/stdlib folder from Corey (which is part of the wax plug-in)

    If you go step by step and read line by line, you might get things working. While other developers will help you, there is only so much time that they would be willing to allocate. So use that wisely... just a suggestion.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Hi @OZApps ,

    I am very sorry for going at faster than a pace than I can go. Its always been a bad habit of mine. I am only 16 yrs old, and I have highschool work and other things and I'm just trying to stay on top of this my friend and I have planned for a very long time.

    Also, sorry for asking so much from other developers including @bowerandy @OZApps @ar2rsawseen . I've learned the LUA language very well, but when it comes down to Objective-C, its like I've never coded before in my life.

    We aren't making a game, rather we are making a Social/Travel app as you can tell by the title, Planit. We searched for the best place for LUA and Gideros was it. We saw that you could "bridge" LUA and Objective-C together to include native widgets and such.

    I see many developers telling others how to use Wax and they are giving all this code, and I've been wondering where exactly they've been finding all of this, but no one has given me a definitive answer. We just need DatePickers and TextBoxes and that's really it.

    @OZApps as you said, I added the structs.lua file to my app and now the DatePicker is showing up. But I am using SceneManager and the DatePicker isn't "abiding" by what SceneManager is telling everything else to do. I guess because of:
    getRootViewController():view():addSubview(datePicker)
    But when I try to make it:
    self:addChild(datePicker)
    It gives me an error saying that it found "Userdata" rather than a "Sprite". As I said before, Objective-C is very hard for me and I've tried to learn it and I can't, and yes I can be "Impulsive" sometimes, maybe most of the time. :p

    And again @OZApps @bowerandy , I am truly sorry for all of what I had done.

    Thanks for still helping
    -Landon
    JLS
    Lead Coder and Designer
  • @Landon,
    Gideros uses OpenGL and everything that is native to Gideros is drawn on this OpenGL Surface. This is the same with other cross platform frameworks too.

    iOS SDK uses it's own views, OpenGL is one such view. So, you can add the Gideros 'stage' to the iOS views, but you cannot add the UIView to the Gideros Stage or objects.

    All of the native elements created could either be over or under the OpenGL Stage, but never part of it. Hope that helps you resolve that issue.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @OZApps so your saying I can't use these native widgets with Scene manager?
    JLS
    Lead Coder and Designer
  • @OZApps or Scene manager with these native widgets?
    JLS
    Lead Coder and Designer
  • yes, you cannot use Native Widgets with Scene Manager, atleast not in the traditional sense.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @OZApps so what would be the best method to be able to manage scenes and yet at the same time be able to use Native Widgets?

    Thanks.
    JLS
    Lead Coder and Designer
  • I haven't worked with native widgets, but I think you need to listen to scene events as when scene enters and when exits and manually add or remove elements.
  • @ar2rsawseen

    How would I make it so that the widgets "listen" to the scene events? Also, how would I manually add and remove elements?

    Sorry for asking so much, I'm just really confused.

    Thanks.
    JLS
    Lead Coder and Designer
  • I haven't worked with native widgets either.

    I suggest you read near the end of here http://appcodingeasy.com/Gideros-Mobile/Manage-Scenes-in-Gideros-Mobile to see how different stages of scene manager dispatches events that you can use to remove items and here http://www.giderosmobile.com/documentation/reference_manual.html#Sprite:removeChild for an explanation of how you remove a sprite which might help you with removing elements.

    And maybe try adding a sprite to the stage (rather than to the scene) and removing it manually as the scene changes in a very simple test project with no plugins etc first ;)
  • bowerandybowerandy Guru
    edited December 2012
    yes, you cannot use Native Widgets with Scene Manager, atleast not in the traditional sense.
    @OZApps, @ljp1203, well actually you can. Perhaps you missed this video that I posted some time ago. It show a native button and image (UIButton and UIImage) being placed in a Gideros scene and then obeying the scene transitions and another scene is brought into play. This is achieved by wrapping the BhWax objects for the button and image in a Gideros object called a BhUIViewFrame.


    The code to set up the scene looks like this:
    function DemoScene1:init()
    	local bg=Shape.bhMakeRect(0, 0, application:getContentWidth(), application:getContentHeight(), nil, 0xff0000)
    	self:addChild(bg)
    	self:addEventListener("enterEnd", self.onSceneEnterEnd, self)
     
    	local button=UIButton:buttonWithType(UIButtonTypeRoundedRect)
    	button:setTitle_forState("Press me to go to France", UIControlStateNormal)
    	button.delegate=ControlDelegate.new(button, UIControlEventTouchUpInside, self.onButtonClicked, self)
    	local buttonFrame=BhUIViewFrame.new(button, CGRect((768-200)/2, (1024-100)/2, 200, 100))
    	self:addChild(buttonFrame)
     
    	local image=UIImage:imageWithContentsOfFile(getPathForFile("|R|Images/uk.png"))
    	local imageView=UIImageView:initWithImage(image)
    	local imageFrame=BhUIViewFrame.new(imageView, CGRect((768-200)/2, 100, 216, 216))
    	self:addChild(imageFrame)
    end
    The demo is included in an updated version of the BhWax code available from the GitHub repository. Look for the file BhWaxSceneDemo.

    PS: As an aside, the demo also shows how to use the Google Text to Speech service to get speech into your apps for free - again using some simple BhWax calls.

    Best regards

  • @OZApps, @ljp1203, well actually you can.
    The UIView is stored as a pointer in the gideros object, not really added to the hierarchy, which is what I said, "not in the traditional way" which is still true, right @Andy?
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @OZApps, yes that's right but it can give the effect that the UIViews can be handled by SceneManager.

    There are still two hierarchies. As you point out, the UIViews all live in a plane above all the Gideros scene (OpenGL) objects. There is no way around this. All that the BhUIViewFrame does is put a stub object in the Gideros scene that can be moved about and tweened like any other Gideros sprite. The frame just points to its associated UIView and makes sure that it moves/fades along with it.

    best regards
  • bowerandybowerandy Guru
    edited December 2012
    I am very sorry for going at faster than a pace than I can go. Its always been a bad habit of mine. I am only 16 yrs old, and I have highschool work and other things and I'm just trying to stay on top of this my friend and I have planned for a very long time.
    @ljp1203, you should have let us know your age before. I think people (including myself) maybe a bit more sympathetic knowing this.

    But I have to say you appear a bit out of your depth here, especially with the BhWax stuff. In order to use it you *will* need to learn how Objective C works, if only to be able to read the code successfully. Perhaps an online tutorial might help?

    If I wanted to bring up an iOS component using Wax I would first go to StackOverflow and type in a query to find if anyone else has asked how to use this component in Objective C. If you find a suitable answer then there is a fairly straightforward way of converting the Objective C into Lua. I provided a little tutorial on this here:

    http://bowerhaus.eu/blog/files/activity_spinner.html

    You will also need to read and understand this link:

    https://github.com/probablycorey/wax/wiki/Overview

    best regards
  • ljp1203ljp1203 Member
    edited December 2012
    Hi everyone,

    Sorry I haven't been on lately. I've just gotten over the flu and it was horrible :( . But atleast I'm back and back in business :D .

    Anyways, @bowerandy , thanks for understanding. I don't mind people being like that, it shows me that I have to provide more on the problem and look at it more rather then shoot responses back without "thoroughly" looking through the answer. I've looked over your links and I'm still a little startled at the Objective-C side compared to LUA. But hopefully I will get down the concept sometime in the next week or 2.

    Also, @OZApps , I looked over your tutorial here about tweeting from your app:
    http://howto.oz-apps.com/2012/09/tweet-tweet-right-from-your-lua-app.html

    I looked over it, and there didn't seem to be any need for any extra files. So what I did was take your code and pasted it into my app like so:
    	--Twitter Icon	
    	local twitter1 = Bitmap.new(Texture.new("Images/twitter.png"))
    	local twitter2 = Bitmap.new(Texture.new("Images/twitter.png"))
     
    	local twitter = PlanitBtn.new(twitter1, twitter2)
    	twitter:addEventListener("click",
    		function()
    			twit = TWTweetComposeViewController:init()
    			twit:setInitialText("The tweet here")
    			getRootViewController():presentModalViewController_animated(twit, true)
    		end)		
    	--Add PlanitBtn To Scene
    	twitter:setPosition(550, 70)
    	self:addChild(twitter)
    Then whenever I click on the button, the player stops the app and gives me this error:

    WARNING: No object named 'TWTweetComposeViewController' found.
    Mainmenu.lua:12: attempt to index global 'TWTweetComposeViewController' (a nil value)
    stack traceback:
    Mainmenu.lua:12: in function

    I have all of the files for Wax and HotWax and they are all up to date. I can't seem to find the problem as I am still trying to understand Objective-C and LUA "binding" like I stated above.

    Thanks in advance.
    -Landon
    JLS
    Lead Coder and Designer
Sign In or Register to comment.