Related
I want the balloons in this scene, which are DisplayObjects, to disappear when the scene ends. I have added them to the local sceneGroup = scene.view by passing this as the first argument to display.newImageRect() and assumed this would be enough for them to be removed as they are listed in the destroy.scene bit at the bottom. However when it transitions to the next scene they are still there. Why are these DisplayObjects not being removed?
local composer = require( "composer" )
local scene = composer.newScene()
local Balloons
local positioninsheetOptions = 100
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*2,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*3,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*4,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*5,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*6,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*7,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*8,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*9,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*10,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*11,
width = 71,
height = 100
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
Balloons = {}
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local function gotoGame()
composer.gotoScene( "game" )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local balloonBlue1 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local balloonYellow1 = display.newImageRect(sceneGroup, objectSheet, 8, 71, 100)
local balloonRed1 = display.newImageRect(sceneGroup, objectSheet, 10, 71, 100)
local balloonBlue2 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local Balloons = display.newGroup()
Balloons:insert( balloonBlue1 )
Balloons:insert( balloonYellow1 )
Balloons:insert( balloonRed1 )
Balloons:insert( balloonBlue2 )
Balloons:addEventListener( "tap", gotoGame )
balloonBlue1.x = (display.contentWidth/8)
balloonBlue1.y = balloonBlue1.height
balloonYellow1.x = (display.contentWidth/8)*3
balloonYellow1.y = balloonBlue1.height
balloonRed1.x = (display.contentWidth/8)*5
balloonRed1.y = balloonBlue1.height
balloonBlue2.x = (display.contentWidth/8)*7
balloonBlue2.y = balloonBlue1.height
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
You need to add the Balloon group (of which the balloons are children) to your sceneGroup.
In scene:destroy, make sure you remove any listeners and cancel any transitions on the things you want destroyed. If you have, when the scene's view is destroyed, the Balloon group and all it's children will also be destroyed.
I'm building a game where I have a start screen and clicking the start button it should take me to the FIRST level. Some stuff happens and then I will be taken to the SECOND level.
However I'm running into problems and can't seem to find a solution.
When I click the start button it seems to get stuck in the create function because i prints out something. I was told you don't have to place anything in the create function and just put everything in the SHOW FUNCTION. Was i mislead?
START SCENE
local composer = require("composer")
local widget = require("widget")
local options = {effect = "fade", time = 800}
local startBtn;
local function start(event)
-- load first scene
composer.gotoScene( "level1", options);
startBtn:removeSelf();
print("Start Game")
end
startBtn = widget.newButton(
{
left = 75,
top = 100,
id = "startBtn",
label = "Start",
onEvent = start
}
)
When I click the START button it should take me to the FIRST LEVEL which is here
and is where i'm running into problems.
local composer = require( "composer" );
local scene = composer.newScene();
local widget = require ("widget");
function scene:create( event )
local sceneGroup = self.view;
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
local params = event.params;
print(params);
if (phase == "will") then
print("Will")
elseif (phase == "did") then
print("Did")
local bg = display.newImage ("bg.png",
display.contentCenterX, display.contentCenterY);
------- ALEX KIDD ---------------------------------
local options =
{
frames = {
{ x = 1, y = 2, width = 16, height = 25}, --frame 1
{ x = 18, y = 2, width = 16, height = 25}, --frame 2
{ x = 35, y = 2, width = 16, height = 25}, --frame 3
{ x = 52, y = 2, width = 16, height = 25}, --frame 4
{ x = 1, y = 54, width = 16, height = 24}, --ready1
{ x = 19, y = 54, width = 16, height = 24}, --ready2
{ x = 37, y = 54, width = 29, height = 24}, -- rock
{ x = 67, y = 54, width = 33, height = 24}, -- scissor
{ x = 101, y = 54, width = 33, height = 24}, -- paper
}
};
local sheet = graphics.newImageSheet( "kidd.png", options );
-- Create animation sequence for animation
local seqData = {
{name = "normal", start=1 , count = 4, time = 800},
{name = "faster", frames={1,2,3,4}, time = 400},
{name = "shake", frames={5,6}, time = 500},
{name = "rock", frames={7}},
{name = "paper", frames={9}},
{name = "scissor", frames={8}},
}
local alex = display.newSprite (sheet, seqData);
alex.x = display.contentCenterX-100;
alex.y = display.contentCenterY+83;
alex.anchorX = 1;
alex.anchorY = 1;
---------- JANKEN ---------------------------------
local jankenOpt =
{
frames = {
{x= 154, y= 13, width= 39, height= 48 }, -- shake1
{x= 195, y= 13, width= 39, height= 48 }, -- shake2
{x= 236, y= 13, width= 32, height= 48 }, -- set
{x= 270, y= 13, width= 16, height= 48 }, --r/p/s
{x= 287, y= 13, width= 16, height= 48 }, --r/p/s
{x= 305, y= 13, width= 15, height= 48 }, --r/p/s
{x= 69, y= 13, width= 41, height= 48 }, --flap1
{x= 110, y= 13, width= 40, height= 48 }, --flap2
}
};
local jankenSheet = graphics.newImageSheet( "chars.png", jankenOpt );
-- Create animation sequence janken
local seqDataJanken = {
{name = "flap", frames={7,8}, time = 500},
{name = "shake", frames={1,2}, time = 500},
{name = "set", frames={3}, time = 10, loopCount=1},
}
local janken = display.newSprite (jankenSheet, seqDataJanken);
janken.x = display.contentCenterX+100;
janken.y = display.contentCenterY+83;
janken.anchorX = 1;
janken.anchorY = 1;
-------------- button setup
local btnOpt =
{
frames = {
{ x = 3, y = 2, width=70, height = 22}, --frame 1
{ x = 78, y = 2, width=70, height = 22}, --frame 2
}
};
local buttonSheet = graphics.newImageSheet( "button.png", btnOpt );
local function foo (wow)
alex:play();
janken:play();
end
local function bar (wow)
alex:pause();
janken:pause();
--alex:setFrame(2); --keep it at this frame
-- NEXT: generate more buttons
end
-- Function to handle button event
local function go( event )
-- event.target is the button widget
print (event.phase, event.target.id)
transition.to(alex, {time=2000, x=170, onStart = foo, onComplete = bar})
end
local btnGo = widget.newButton(
{
x = 200,
y = 20,
id = "btnGo",
label = "Go!",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = go,
}
);
-------------- play buttons
local function shoot (buttonID)
local randomHand = math.random(4,6);
-- position Janken and draw his hands
janken:setSequence("set");
hand = display.newImage (jankenSheet, randomHand, display.contentCenterX+61, display.contentCenterY+60);
if (buttonID == "btnRock") then
alex:setSequence("rock"); -- just show rock for now
elseif (buttonID == "btnScissor") then
alex:setSequence("scissor"); -- just show rock for now
else
alex:setSequence("paper"); -- just show rock for now
end
end
local function play (event)
if (event.phase == "ended") then
alex:setSequence ("shake");
alex:play();
janken:setSequence("shake");
janken:play();
local t = timer.performWithDelay (1500, function() shoot(event.target.id) end, 1);
print (event.target.id); -- which button was it?
end
end
local btnRock = widget.newButton(
{
x = 80,
y = 20,
id = "btnRock",
label = "Rock",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local btnPaper = widget.newButton(
{
x = 80,
y = 50,
id = "btnPaper",
label = "Paper",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local btnScissor = widget.newButton(
{
x = 80,
y = 80,
id = "btnScissor",
label = "Scissor",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local scoreAlex = display.newText ( {text="Alex: 0", x=230, y=60, fontSize=20});
scoreAlex:setFillColor (0,0,0);
scoreAlex.anchorX = 1;
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
transition.cancel(enemy);
elseif ( phase == "did" ) then
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )
scene:addEventListener( "hide", scene )
return scene
Well, you are adding a listener scene:addEventListener( "enter", scene ) which should be scene:addEventListener( "show", scene ) (Change enter to show)
Your listener is not even being fired, because it was not added with the correct name.
Composer uses the following events: create, destroy, show, hide.
I am new to corona, all the work is from a scratch of others. Let see, I have 3 images that entitled shop1price shop2price shop3price. Now I want it to be simplified as the code below
local options =
{
{ defaultFile = 'images/shop1price.png' },
{ defaultFile = 'images/shop2price.png' },
{ defaultFile = 'images/shop3price.png' },
}
local priceTag = {}
for i = 1,3 do
priceTag[i] = widget.newButton{
options[i],
overColor = {128,128,128,255},
width = 73,
height = 38,
left = (centerX-155) + (i-1)*118,
top = centerY * 0.88,
id = i,
onEvent = function (e)
if e.phase == 'ended' then
onTouchBuy(e.target.id)
end
return true
end
}
-- priceTag[i] : setReferencePoint( display.CenterReferencePoint )
priceTag[i] : scale( 0.8 , 0.8 )
buttonGroup : insert( priceTag[i] )
end
But the button does not appear, I think the wrong is in options[i]. But the problem always is I don't know how the right. I know I can make the code themselves one by one, but it is certainly very tiring. What if I have for example of 100 buttons.
Any help would be appreciated.
local options = {}
[#options+1] = 'images/shop1price.png'
[#options+1] = 'images/shop2price.png'
[#options+1] = 'images/shop3price.png'
local priceTag = {}
for i = 1,#options do
priceTag[i] = widget.newButton{
defaultFile = options[i],
overColor = {128,128,128,255},
width = 73,
height = 38,
left = (centerX-155) + (i-1)*118,
top = centerY*0.88,
id = i,
onEvent = function (e)
if e.phase == 'ended' then
onTouchBuy(e.target.id)
end
return true
end
}
-- priceTag[i] : setReferencePoint( display.CenterReferencePoint )
priceTag[i] : scale( 0.8 , 0.8 )
buttonGroup : insert( priceTag[i] )
end
Try this , should work fine.
I have a corona app, with two storyboard scenes (scene1.lua, addDesire.lua).
I show addDesire.lua as an overlay:
function onAddPurchase( event )
if event.phase == "ended" then
local options = {
effect = "fromBottom",
time = 500,
isModal = true,
}
storyboard.showOverlay( "addDesire", options )
end
end
In Corona Simulator everything works, but in xCode simulator a pink background appears in several cases.
1) When addDesire.lua appears after onAddPurchase it looks like:
When it should look like:
2) When I close addDesire.lua (tapping on Cancel button) this appears:
There is something even stranger going on under the hood:
addDesire.lua has 2 textFields and 1 textBox which is created in function scene:createScene( event ). If I comment out the code that creates these objects everything works perfectly.
function scene:createScene( event )
local group = self.view
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local bg = display.newImageRect( "assets/dollar.png", 360, 570 )
bg:toBack()
bg.x, bg.y = _W/2, _H/2
bg:addEventListener( "tap", function() native.setKeyboardFocus(nil); end)
group:insert(bg)
-- Rounded Rect Alpha
roundedRect = display.newRoundedRect( 5, 5, _W*0.9, _H*0.8, 10 )
roundedRect.x, roundedRect.y = centerX, centerY
roundedRect:setFillColor( 0/255, 0/255, 0/255, 170/255 )
group:insert(roundedRect)
-- Label Title
titleLabel = display.newText( "Purchase", 0, 0, "AmericanTypewriter-Bold", 20 )
titleLabel.x, titleLabel.y = centerX, _H*0.15
group:insert(titleLabel)
-- Label Fam
nameLabel = display.newText( "Name", 0, 0, "AmericanTypewriter", 18 )
nameLabel.x, nameLabel.y = centerX, _H*0.20
group:insert(nameLabel)
-- Edit Fam
nameText = native.newTextField(_W/2, _H*0.26, 240, 30)
nameText.font = native.newFont(native.systemFont, 18)
nameText:addEventListener( "userInput", inputListener )
group:insert(nameText)
-- Label Name
descriptionLabel = display.newText( "Description", 0, 0, "AmericanTypewriter", 16 )
descriptionLabel.x, descriptionLabel.y = centerX, _H*0.31
group:insert(descriptionLabel)
-- Edit Name
descriptionText = native.newTextBox(_W/2, _H*0.44, 240, 100)
descriptionText.font = native.newFont(native.systemFont, 14)
descriptionText.isEditable = true
descriptionText:addEventListener( "userInput", inputListener )
group:insert(descriptionText)
-- Label Deposit
costLabel = display.newText( "Cost", 0, 0, "AmericanTypewriter", 16 )
costLabel.x, costLabel.y = centerX, _H*0.57
group:insert(costLabel)
-- Edit Deposit
costText = native.newTextField(_W/2, _H*0.64, 240, 30)
costText.font = native.newFont(native.systemFont, 18)
costText.inputType = "number"
costText.align = "center"
costText:addEventListener( "userInput", inputListener )
group:insert(costText)
-- Button Save & Start game
btnSave = widget.newButton {
width = _W*0.4,
height = 50,
--defaultFile = "buttonDefault.png",
--overFile = "buttonOver.png",
label = "Buy",
onEvent = onSaveData
}
btnSave.x, btnSave.y = _W/2, costLabel.y+150
group:insert(btnSave)
-- Button Cancel
btnCancel = widget.newButton {
width = _W*0.8,
height = 50,
--defaultFile = "buttonDefault.png",
--overFile = "buttonOver.png",
label = "Cancel",
onEvent = onExit,
}
btnCancel.x, btnCancel.y = _W/2, btnSave.y+50
group:insert(btnCancel)
end
What is going on?
The answer is here guys: native.textField
I'm trying to make a game for the Android Google Play store. But, when I was making the main menu I ran into an issue.
Before I added the storyboard and the scene functions, everything worked fine. But now I can't see a thing when I run it.
Also, I get no errors.
-- Requires
local widget = require "widget"
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
function scene:createScene(event)
screenGroup = self.view
-- Background
local background = display.newImage("images/bg.png")
screenGroup:insert(background)
-- Title
local title = display.newImage("images/title.png")
title.x = display.contentCenterX
title.y = display.contentCenterY - 110
screenGroup:insert(title)
-- Play game
local button1 = widget.newButton {
label = "Play Game",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button1.x = display.contentCenterX
button1.y = display.contentCenterY - 47
screenGroup:insert(button1)
-- How To Play
local button2 = widget.newButton {
label = "How To Play",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button2.x = display.contentCenterX
button2.y = display.contentCenterY + 13
screenGroup:insert(button2)
-- Level Select
local button3 = widget.newButton {
label = "Level Select",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button3.x = display.contentCenterX
button3.y = display.contentCenterY + 73
screenGroup:insert(button3)
-- About Us
local button4 = widget.newButton {
label = "About Us",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button4.x = display.contentCenterX
button4.y = display.contentCenterY + 133
screenGroup:insert(button4)
end
function start(event)
if event.phase == "began" then
storyboard.gotoScene("level1", "fade", 400)
end
end
function scene:enterScene(event)
button1:addEventListener("touch", start)
end
return scene
Try this:
main.lua
local storyboard = require "storyboard"
storyboard.gotoScene( "scene1")
scene1.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
function scene:createScene( event )
local screenGroup = self.view
-- Background
local background = display.newImage("images/bg.png")
screenGroup:insert(background)
-- Title
local title = display.newImage("images/title.png")
title.x = display.contentCenterX
title.y = display.contentCenterY - 110
screenGroup:insert(title)
-- Play game
local button1 = widget.newButton {
label = "Play Game",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button1.x = display.contentCenterX
button1.y = display.contentCenterY - 47
screenGroup:insert(button1)
-- How To Play
local button2 = widget.newButton {
label = "How To Play",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button2.x = display.contentCenterX
button2.y = display.contentCenterY + 13
screenGroup:insert(button2)
-- Level Select
local button3 = widget.newButton {
label = "Level Select",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button3.x = display.contentCenterX
button3.y = display.contentCenterY + 73
screenGroup:insert(button3)
-- About Us
local button4 = widget.newButton {
label = "About Us",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button4.x = display.contentCenterX
button4.y = display.contentCenterY + 133
screenGroup:insert(button4)
end
function start(event)
if event.phase == "began" then
storyboard.gotoScene("level1", "fade", 400)
end
end
function scene:enterScene(event)
button1:addEventListener("touch", start)
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
return scene
I think you should insert background after inserting all the objects or you can also insert all the objects on the background which you have added on the screenGroup. May be background image is hiding all the objects.
Try above solution, if it will not help you. Please let me know what exactly is the problem after using my solution.
Good luck!