Character Selection in Corona SDK - lua

I make an endless run game using Corona SDK and I need to make a character selection between 2 characters (boy/girl). I don't have any idea how I should start.
I tried to make 2 portraits of the character on the menu screen, but I don't know what to do on Event Touch on them. I tried to save them in a variable but I don't know how to load them in the game.lua. There I have:
local spriteSheet = sprite.newSpriteSheet("monsterSpriteSheet.png", 100, 100)
local monsterSet = sprite.newSpriteSet(spriteSheet, 1, 7)
sprite.add(monsterSet, "running", 1, 6, 600, 0)
sprite.add(monsterSet, "jumping", 7, 7, 1, 1)
local monster = sprite.newSprite(monsterSet)
monster:prepare("running")
monster:play()
monster.x = 60
monster.y = 200
monster.gravity = -6
monster.accel = 0
monster.isAlive = true
I've got a main.lua a menu.lua and a game.lua. I use director class for transition. Any ideas on how I can do this?

You can pass parameters through storyboard.gotoScene
local options = {
effect = "crossFade",
time = 500,
params = {
character = myCharacter,
}
}
storyboard.gotoScene( "game", options )
and in the game.lua
function scene:createScene( event )
local params = event.params
local character = params.character
end

You could also create a data file and point to that file.
For example:
data.lua
local data = {}
return data
Then in your selection scene require data.lua and save your chosen character to it.
data.chosenCharacter = chosenCharater
Then in your game scene require data.lua again and point your character to what is saved in data.
local character = data.chosenCharacter

Related

Unable to create a working sprite

I am unable to create a working animation sprite for this single area of my app.
I am using Corona SDK and have the following sprite:
This is names mainCharacter.png. I have a double sized version called mainCharacter#2x.png.
I have sheet options, 2 sequences, I'm building an image sheet and passing that to my sprite:
local playerSheetOptions =
{
width = 50,
height = 50,
numFrames = 17,
sheetContentWidth = 500,
sheetContentHeight = 100
}
local playerSequences = {
{
name = "idle",
start = 1,
count = 12,
time = 1200,
loopCount = 0,
loopDirection = "bounce"
},
{
name = "jump",
start = 13,
count = 5,
time = 600,
loopCount = 1
},
}
local playerSheet = graphics.newImageSheet( "resource/images/mainCharacter.png", playerSheetOptions )
local player = display.newSprite(gameSheet, playerSheet, playerSequences)
I am getting the following error:
display.newSprite() requires argument #2 to a table containing sequence data
If I print the relevant data:
print(gameSheet)
print(playerSheet)
print(playerSequences)
I get:
14:27:05.703 userdata: 12445228
14:27:05.703 userdata: 0CF42600
14:27:05.703 table: 0CF41FD0
Where am I going wrong? I have tried simplifying the sequences a lot, but still get the same thing.
Use
local player = display.newSprite(playerSheet, playerSequences)
instead of
local player = display.newSprite(gameSheet, playerSheet, playerSequences)
From Corona documentation
Once the image sheet(s) and sequences are set up, a new sprite object
can be created with the display.newSprite() API:
display.newSprite( [parent,] imageSheet, sequenceData )
For this API, the parent
parameter is optional and represents the display group in which to
insert the sprite. The imageSheet parameter defines the default image
sheet for the sprite, and sequenceData is the table that contains all
of the sequences for the sprite.
Read more about Sprite Animation.

CoronaSDK Touch Events

Currently creating a game with Corona SDK is it possible to have an image and when it is clicked it displays 3 images and once one them 3 images are clicked the score increases by 1. Also Im only a beginner at coding , this is a new language to me. Thanks.
local CButton = display.newImage("+5.jpg" , 100 , 600)
CButton.alpha = 0.5
CButton.name = "CButton"
local CButtonLabel = display.newText( { text = "", x = 0, y = 0, fontSize = 28 } )
CButtonLabel:setTextColor( 0 ) ; CButtonLabel.x = 100 ; CButtonLabel.y = 45
local function touchCListener( event )
local object = event.target
print( event.target.name.." TOUCH on the '"..event.phase.."' Phase!" )
local ChordCOne = display.newImage("+5.jpg", 900,300)
local ChordCTwo = display.newImage("+5.jpg", 1000,300)
local ChordCThree = display.newImage("+5.jpg", 1100,300)
end
--add "touch" listener -- LABEL IS FOR TESTING!
CButton:addEventListener( "touch", touchCListener)
ChordCOne:addEventListener( "touch", updateScore)
CButtonLabel.text = "touch"
Yes, new DisplayObjects can be created in a listener function and listeners can be added to those objects as well.
In your code, you have not added the DisplayObjects created in your listener to any GroupObject (such as scene.view), which will give unexpected results.
Since the variables pointing to the newly created DisplayObjects (ChordCOne, etc.) are local to the function where they are instantiated, you cannot call addEventListener() on them outside the function. You should add the listener when they are created.
Also, the updateScore() listener function isn't defined anywhere. Make sure updateScore is not nil when and wherever you give it as an argument to addEventListener().

Corona SDK widget.newButton() not appearing in simulator when inside scene:create

I have the following code:
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local options1 = {
x = display.contentWidth / 2,
y = 200,
onRelease = button,
lableAlign = "center",
emboss = true,
id = "1",
label = "Button1"
}
local options2 = {
y = 300,
x = display.contentWidth / 2,
onRelease = button,
lableAlign = "center",
emboss = true,
id = "2",
label = "Button2"
}
local button1 = widget.newButton(options1)
local button2 = widget.newButton(options2)
sceneGroup:insert(button1)
sceneGroup:insert(button2)
end
When I place this code in a standalone file (not a scene) the buttons show up as supposed. However now I am turning this standalone file into a scene and for some reason the above code results in nothing in the simulator. Any ideas?
You have two choices:
move your scene the a yourScene.lua file (named as you wish) in which you call storyboard.newScene() (no argument) and in your main.lua you use storyboard.goto('yourScene')
you can create the scene in main.lua via storyboard.newScene('yourScene') and goto it from within main.lua via storyboard.goto('yourScene')
Basically your scene can be in a separate module, corona automatically names it based on module name, and main.lua goes to it, or your scene can be in same module, but then you have to give it a name yourself, and main.lua can "goto" it (within same module). You can even have multiple scenes in same module.
I recommend you have your scenes in separate modules, it just makes for better modularization. Conjecture: maybe the method 2 was the original, and method 1 was added latter when developers found their scenes were getting large and so needed to move them to separate modules.
The code posted above is within the main.lua file. Therefore, the scene is never called. The solution is to rename the file above to something like menu.lua and then call this scene from main.lua.

Corona SDK display.remove() in lua

this code (lua) gives you a random value from the table "local a" by pressing the text "new". Unfortunately the new random value just appears above the old one. I've tried to remove the old value e.g. with display.remove(mmDis), but it doesn't work.
The second problem is that sometimes I also get back the value "nil" and not only the four entries from the table.
Both things must be easy to solve, but as newbie to lua and working on these small things for almost 4 hours now I just don't get what to change to make it work.
-- references
local mmDis
-- functions
function randomText(event)
display.remove(mmDis)
local a = {"Banana!","Apple!","Potato","Pie"}
com = (a[math.random(0.5,#a)])
local mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Calibri", 60)
end
-- menu button
local textnew = display.newText("New", 0, 0, "Calibri", 40)
textnew.x = display.contentWidth*0.2
textnew.y = display.contentHeight*0.9
textnew:addEventListener ("tap", randomText )
It's hard to understand what you are trying to do because when you create textnew you have it in one position and size, while in randomeText() you seem to want to replace that text object with a new one, but you put it at a different pos and size. It appears you want to change the object's text every time you press on tap; in this case, you don't need to replace the text object, you just replace its text.
Also:
you have two "local mmDis", the second one will hide the first, not sure what you're after there
read carefully the docs for math.random
com is already a string so you don't need tostring
Try this code and let me know if this is not what you are after:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
textnew.text = com
-- if you want to change position too:
-- textnew.x = display.contentWidth*0.2
-- textnew.y = display.contentHeight*0.9
-- if you want to change size too, but only used for multiline text:
-- textnew.width = display.contentWidth*0.9
-- textnew.height = display.contentHeight*0.8
end
textnew:addEventListener ("tap", randomText )
Sometimes it looks like the button doesn't do anything when you tap but that's because the random number happens to be same as previous, you could put a loop to guard against that. Ig you actually want to change the pos and/or width, then the above code makes it clear where you should do that.
So, that's the basic code (without any extras at the moment :)) how it should be. Big thanks to Scholli!:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
local replacement = display.newText(menuTextOptions)
replacement.y = display.contentHeight*0.5
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
replacement.text = com
end
textnew:addEventListener ("tap", randomText )

Corona "Attempt to remove an object that has already been removed"

I'm working on a simple "breakout" game and I have problem reloading a map.
for example: if I start with level1, break some bricks and lose, than I'm loading the same map again. next time that the ball collides with the same brick I "touched" before, will give me an error Attempt to remove an object that has already been removed
local map = lime.loadMap("maps/" .. currentLevel .. ".tmx")
local layer = map:getTileLayer("bricks_1")
local visual = lime.createVisual(map)
local physical = lime.buildPhysical(map)
function removeBricks(event)
if event.other.isBrick then
local brick = event.other
transition.to(brick, {time = 20, alpha = 0})
score = score + brick.scoreValue
ScoreNum.text = score
-- remove brick
brick:removeSelf()
brick = nil
...
i think the second time you go to your game the event.other is not created are you using storyboard if so you can try to remove the scene after the game is over so when you go to your game again it will recreate all the object
Have you try this?
transition.to(brick, {time = 20, alpha = 0, onComplete = function()
if brick then
brick:removeSelf()
brick = nil
end
end})
If you are using physics you also have to do a physics.removeBody(brick) before you remove the object itself so that it detaches from the physics engine. If not physics thinks it's still there.

Resources