I am creating a popup window in the main menu. In the popup screen do closePopUp (), and displayed the message 'could not change scene inside a popup'. Help solve the problem
closePopUp
function OnClickExitNo(event)
print("click");
if event.phase == "ended" then
director:closePopUp();
end
end
exitNo = ui.newButton{
default = "src/img/exitNo.png",
over = "src/img/exitNo.png",
onEvent = OnClickExitNo
}
and openPopUp
director:openPopUp("exit_screen", popClosed );
try this ....
u should place the director.lua file in your project folder
main.lua
local director=require("director")
local mainGroup=display.newGroup()
function clean(event)
print("clean")
end
function main()
display.setStatusBar(display.HiddenStatusBar)
mainGroup:insert(director.directorView)
director:changeScene("screen1")
end
main()
screen1.lua
module(...,package.seeall)
function clean(event)
print("clean")
end
new =function()
local localGroup=display.newGroup()
local circle=display.newCircle(display.contentWidth/2,display.contentHeight/2,80)
circle:setFillColor(220,25,220)
local text=display.newText( "click to openPopUp", 0, 0, native.systemFontBold, 16 )
text.x=circle.x
text.y=circle.y
localGroup:insert(circle)
local listener=function(event)
if(event.phase=="ended")then
director:openPopUp( "screen2","flip")
end
end
circle:addEventListener("touch",listener)
return localGroup;
end
screen2.lua
module(...,package.seeall)
function clean(event)
print("clean")
end
new =function(prams)
local localGroup=display.newGroup()
local circle =display.newCircle(display.contentWidth/2,display.contentHeight/2,100)
circle:setFillColor(255,0,0)
local text=display.newText( "click to closePopUp", 0, 0, native.systemFontBold, 16)
text.x=circle.x
text.y=circle.y
local listener=function(event)
if(event.phase=="ended")then
circle:removeSelf()
text:removeSelf()
director:closePopUp()
end
end
circle:addEventListener("touch",listener)
localGroup:insert(circle)
return localGroup;
end
Related
It looks like I am having problems with transitioning from my level select page to my first level. I think it might be that I am missing something in my level 1, but I don't know exactly.
levelSelect.lua
module(..., package.seeall)
local director = require ("director")
local physics = require("physics")
physics.start()
local widget = require( "widget" )
-- Function to handle button events
local function handleButtonEventLevel1( event )
local phase = event.phase
if "ended" == phase then
director:changeScene("lvl1")
end
end
--local function goLevel1()
--director:changeScene("lvl1")
--return true
--end
--widget.newButton:addEventListener("tap", goLevel1)
local function handleButtonEventToPage( event )
local phase = event.phase
if "ended" == phase then
director:changeScene("page")
end
end
-- Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
local background = display.newImage("bigtestsky.png")
background.x=150
background.y=250
local myButton = widget.newButton
{
left = 25,
top = 25,
width = 100,
height = 50,
defaultFile = "default.png",
overFile = "over.png",
label = "1",
font = "LS",
fontSize = 20,
labelColor = { default = {0,0,50}, over = {0,0,255} },
onEvent = handleButtonEventLevel1,
}
local myButton = widget.newButton
{
left = 25,
top = 415,
width = 100,
height = 50,
defaultFile = "default.png",
overFile = "over.png",
label = "BACK",
font = "LS",
fontSize = 20,
labelColor = { default = {0,0,50}, over = {0,0,255} },
onEvent = handleButtonEventToPage,
}
return localGroup
end
lvl1.lua
local physics = require("physics")
physics.start()
local widget = require( "widget" )
(This is what I mean about missing something in the first level. Could someone please help?)
stack traceback:
[C]: ?
.../myName/Desktop/Bubbles! App/director.lua:116: in function 'loadScene'
.../myName/Desktop/Bubbles! App/director.lua:394: in function 'changeScene'
...myName/Desktop/Bubbles! App/levelSelect.lua:14: in function '_onEvent'
?: in function '?'
?: in function <?:405>
?: in function <?:218>
These are due to some issues with your class: lvl1.lua.
If such error occurs, open director.lua analyse the error return line. This will help you to find what the real problem is.
Here, you must write more lines in your lvl1.lua as below:
module(...,package.seeall) -- If this line is not written, then the package will not be loaded.
function new() -- Module 'lvl1' must have a new() function
local localGroup = display.newGroup() -- The scene should create a display group
local physics = require("physics")
physics.start()
local widget = require( "widget" )
print("Inside lvl1...")
return localGroup; -- And the scene should return the previously created display group.
end
Keep Coding.............. :)
when i use the underneath code in my main.lua file it displays the ad as i want it to be.
but when i add "ads.hide()" (the ad stay on every scene) in the "exitScene" scene section i get this error in the terminal " attempt to index global 'ads' (a nil value)" which i understand as the ads don't display in the simulator but when i open the app op my phone (galaxy s4) non of the buttons respond, and it just stay on the main.lua file/scene
local provider = "admob"
local appID = "**********"
local ads = require "ads"
local screenGroup = self.view
local statusText = display.newText( "", 0, 0, native.systemFontBold, 22 )
statusText:setTextColor( 255 )
statusText:setReferencePoint( display.CenterReferencePoint )
statusText.x, statusText.y = display.contentWidth * 0.5, 160
local showAd
local function adListener( event )
local msg = event.response
print("Message received from the ads library: ", msg)
if event.isError then
statusText:setTextColor( 255, 0, 0 )
statusText.text = "Error Loading Ad"
statusText.x = display.contentWidth * 0.5
local screenGroup = self.view
showAd( "banner" )
else
end
end
if appID then
ads.init( provider, appID, adListener )
end
local sysModel = system.getInfo("model")
local sysEnv = system.getInfo("environment")
showAd = function( adType )
local screenGroup = self.view
local adX, adY = display.screenOriginX, 400
statusText.text = ""
ads.show( adType, { x=adX, y=adY } )
end
if sysEnv == "simulator" then
else
local screenGroup = self.view
showAd( "banner" )
end
Hi how can i close or destroy a "admob" ad on screen change ?
You need to require ads in every scene you create
Add this line to every Lua file that are using the ads plugin
local ads = require("ads")
ads.hide( )
ads:removeSelf()
ads=nil
you can insert above into any event listener or something else.
You need to call this function in every screen.
if ads then
ads.hide()
end
Note: When you are generating dynamic ads then there are time duration between the ads.
So if the ads is on the screen it will hide otherwise not.
But when you not check the if condition and move to next scene the ads will load again.
Hi for some reason corona is giving me this error: attempt to index global 'backC' (a nil value)
local randomBackC = function()
backC = display.newImage("Cloud"..tostring(math.random(1, 4))..".png")
backC.x = math.random (30, 450); backC.y = -20
physics.addBody( backC, { density=2.9, friction=0.5, bounce=0.7, radius=24 } )
end
timer.performWithDelay( 500, randomBackC, 0 )
end
local function cleanup()
if backC.y >100 then
backC:removeSelf()
end
end
Runtime:addEventListener("enterFrame", cleanup)
any ideas of what is causing this?
backC may be already removed because of the Runtime:addEventListener("enterFrame", cleanup)
enterFrame will call the cleanup() over and over again, so you have to remove the enterFrame after you remove the backC and if you want to create multiple objects, make it local only to the function because it may cause referencing problem.
Like this
local randomBackC = function()
local backC = display.newImage("Cloud"..tostring(math.random(1, 4))..".png")
backC.x = math.random (30, 450); backC.y = -20
physics.addBody( backC, { density=2.9, friction=0.5, bounce=0.7, radius=24 } )
local cleanup
cleanup = function()
if backC then
if backC.y >100 then
backC:removeSelf()
backC = nil
Runtime:removeEventListener("enterFrame", cleanup)
end
end
end
Runtime:addEventListener("enterFrame", cleanup)
end
timer.performWithDelay( 500, randomBackC, 0 )
I'm using director , to move from one scene to another. I have a problem that the buttons and text fields from intor still in the top of screen when moving to any scene.
How to remove items (text fields, btns from intro.lua screen) before move to next scene?
enter code here
-- into.lua
module(..., package.seeall)
function new()
--
-- Project: NativeKeyboard2
--
local widget = require( "widget" )
require("hijacks")
local tHeight -- forward reference
-------------------------------------------
-- General event handler for fields
-------------------------------------------
-- You could also assign different handlers for each textfield
local function fieldHandler( textField )
return function( event )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
-- In some cases you may want to adjust the interface when the keyboard appears.
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( "editing" == event.phase ) then
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
print( textField().text )
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
end
-- Predefine local objects for use later
local nameField, phoneField
local fields = display.newGroup()
-------------------------------------------
-- *** Create native input textfields ***
-------------------------------------------
-- Note: currently this feature works in device builds or Xcode simulator builds only (also works on Corona Mac Simulator)
local isAndroid = "Android" == system.getInfo("platformName")
local inputFontSize = 18
local inputFontHeight = 30
tHeight = 30
if isAndroid then
-- Android text fields have more chrome. It's either make them bigger, or make the font smaller.
-- We'll do both
inputFontSize = 14
inputFontHeight = 42
tHeight = 40
end
nameField = native.newTextField( 40, 120, 200, tHeight )
nameField.font = native.newFont( native.systemFontBold, inputFontSize )
nameField:addEventListener( "userInput", fieldHandler( function() return nameField end ) )
phoneField = native.newTextField( 40, 160, 200, tHeight )
phoneField.font = native.newFont( native.systemFontBold, inputFontSize )
phoneField.inputType = "phone"
phoneField:addEventListener( "userInput", fieldHandler( function() return phoneField end ) )
-- Add fields to our new group
fields:insert(nameField)
fields:insert(phoneField)
-------------------------------------------
-- *** Add field labels ***
-------------------------------------------
local defaultLabel = display.newText( "الاسم", 250, 120, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
local defaultLabel = display.newText( "رقم الجوال", 250, 160, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
-- -------------------------------------------
-- -- Create a Background touch event
-- -------------------------------------------
local listener = function( event )
-- Hide keyboard
print("tap pressed")
native.setKeyboardFocus( nil )
return true
end
-- Determine if running on Corona Simulator
--
local isSimulator = "simulator" == system.getInfo("environment")
if system.getInfo( "platformName" ) == "Mac OS X" then isSimulator = false; end
-- Native Text Fields not supported on Simulator
--
if isSimulator then
msg = display.newText( "الرجاء ادخال اسمك ورقم جوالك", 0, 280, native.systemFontBold, 12 )
msg.x = display.contentWidth/2 -- center title
msg:setTextColor( 255,0,0 )
end
-- -- Add listener to background for user "tap"
-- bkgd:addEventListener( "tap", listener )
-- display.remove( obj )
-- obj = nil
local introGroup = display.newGroup();
local background = display.newImage("graphics/intro_background.png")
local begin = display.newImage("graphics/begin_button.png")
begin.x = 160;
begin.y = 400;
begin.scene = "menu";
introGroup:insert(background);
introGroup:insert(begin);
begin:addEventListener("touch", changeScene)
return introGroup;
end
Corona provides a very good feature that is "Storyboard".I am giving you a brief explanation, Try this -
Storyboard - It is a scene (e.g. "screens" or "views") management library that provides developers an easy way to create and transition between scene modules in a Corona SDK app.
Syntax -
local storyboard = require "storyboard"
Example -
local scene1 = storyboard.newScene( "name of the Scene" )
Here are the different events used in the Storyboard -
1- Create Scene -
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
end
2- Enter Scene -
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
end
3- Exit Scene -
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
end
4 - Destroy Scene -
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
local group = self.view
end
It will help you.
My buttons and scene changes are bugging, the buttons on my titlescreen scene bellow both work and direct to the appropriate screens, but they will only do it once. So I cannot naviagte to options, then back to the the title screen, then back to options again - and I cannot work out why?
Here is my titlescreen file:
module(..., package.seeall)
local assetPath = "assets/"
local mainGroup = display.newGroup()
function new()
local ui = require("ui")
local titleScreen = display.newImageRect(assetPath .. "mainMenu.png", display.contentWidth, display.contentHeight)
titleScreen.x = display.contentWidth / 2
titleScreen.y = display.contentHeight / 2
mainGroup:insert(titleScreen)
local onPlayTouch = function( event )
if event.phase == "release" then
director:changeScene("gameScreen")
end
end
local playButton = ui.newButton{
defaultSrc = assetPath .. "playnowbtn.png",
defaultX = 222,
defaultY = 62,
overSrc = assetPath .. "playnowbtn-over.png",
overX = 222,
overY = 62,
onEvent = onPlayTouch
}
playButton.x = display.contentWidth / 2
playButton.y = 50;
mainGroup:insert( playButton )
local onOptionTouch = function( event )
if event.phase == "release" then
director:changeScene("optionsScreen")
end
end
local optionButton = ui.newButton{
defaultSrc = assetPath .. "playnowbtn.png",
defaultX = 222,
defaultY = 62,
overSrc = assetPath .. "playnowbtn-over.png",
overX = 222,
overY = 62,
onEvent = onOptionTouch
}
optionButton.x = display.contentWidth / 2
optionButton.y = 190;
mainGroup:insert( optionButton )
return mainGroup
end
My options file looks like this :
module(..., package.seeall)
function new()
local assetPath = "assets/"
local localGroup = display.newGroup()
local background = display.newImage (assetPath .."optionsScreen.png")
localGroup:insert(background)
local onBackTouch = function( event )
if event.phase == "release" then
director:changeScene("titleScreen")
end
end
local backButton = ui.newButton{
defaultSrc = assetPath .. "playnowbtn.png",
defaultX = 222,
defaultY = 62,
overSrc = assetPath .. "playnowbtn-over.png",
overX = 222,
overY = 62,
onEvent = onBackTouch
}
backButton.x = display.contentWidth / 2
backButton.y = display.contentHeight / 2
localGroup:insert(backbutton)
return localGroup
end
Now the button is displayed on the options scene and responds to touch, but does not direct back to the titlescreen.
I think I am getting confused with groups and only assigning images to scenes not the whole game?
Can anyone help me out, Thanks.
EDIT:
I'm also getting these runtime errors when clicking the buttons.
Runtime error
/Users/Lewis/Desktop/proj/optionsScreen.lua:30: ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
stack traceback:
[C]: ?
[C]: in function 'insert'
/Users/Lewis/Desktop/proj/optionsScreen.lua:30: in function 'new'
/Users/Lewis/Desktop/proj/director.lua:118: in function 'loadScene'
/Users/Lewis/Desktop/proj/director.lua:415: in function 'changeScene'
/Users/Lewis/Desktop/proj/titlescreen.lua:67: in function 'onEvent'
/Users/Lewis/Desktop/proj/ui.lua:94: in function
?: in function
Runtime error
/Users/Lewis/Desktop/proj/director.lua:151: attempt to call field 'unloadMe' (a nil value)
stack traceback:
[C]: in function 'unloadMe'
/Users/Lewis/Desktop/proj/director.lua:151: in function '_listener'
?: in function
?: in function
I don't use director.lua myself so I'm not 100% sure, but in your options.lua you put the following two lines inside the new() function:
local assetPath = "assets/"
local localGroup = display.newGroup()
However in your titlescreen.lua those lines are above the new() function, and I think that's how it needs to be.
In general you should make your indentation consistent, so that it's easy to notice which code is inside which code-blocks.
In your error print-out, it's asking for an unload me function.
Try adding this to your code (above the return statement) and see if it makes a difference:
function unloadMe()
print( "test" )
end
And see if that gets rid of the error. Another option is to ditch ui.lua and use the new widget library's widget.newButton() function instead. Here's the doc page for that (syntax is almost the same as what you already have, so shouldn't be much changes necessary):
http://developer.anscamobile.com/reference/index/widgetnewbutton