I'm an extreme beginner to Corona SDK, and I am currently attempting to make a sound board, where the screen displays multiple buttons, and each button you tap makes a different sound. I am using a process of duplicating an image and having each duplicate play a sound, but I ran into some problems.
Is there a way that I can create "clones" of display objects? What I mean, is that I want to spawn multiple images on the screen, each having some sort of unique value so when one of them is clicked, I will be able to recognize which one.
Try this out:
local function onClickButton( event )
local button = event.target
if event.phase == "ended" then
audio.stop() -- Stop ALL current channels
audio.play( button.stream )
end
end
local function createButton( params )
local x = params.x or 0
local y = params.y or 0
local audio_location = params.audio or "my_sound.mp3"
local button = display.newRect( x, y, 50, 50 )
button.stream = audio.loadStream( audio_location )
button:addEventListener( "touch", onClickButton )
end
createButton( { x = 100, y = 100, audio = "my_sound.mp3" } )
createButton( { x = 200, y = 100, audio = "my_sound_2.mp3" } )
createButton( { x = 100, y = 200, audio = "my_sound_3.mp3" } )
createButton( { x = 200, y = 200, audio = "my_sound_4.mp3" } )
Windows Phone doesn't support MP3's so have that in mind if you're planning on target that market as well:
https://docs.coronalabs.com/guide/media/audioSystem/index.html
You can also use modular classes with metatables but I don't think it's necessary in this case but here is more information about that:
https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/
Related
So, I have this function:
local function addMainMenu()
local widget = require( "widget" )
-- Function to handle button events
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
scene = "GAME"
end
end
-- Create the widget
local button1 = widget.newButton(
{
label = "button",
onEvent = handleButtonEvent,
emboss = false,
-- Properties for a rounded rectangle button
shape = "roundedRect",
width = 200,
height = 40,
cornerRadius = 2,
fillColor = { default={0.9,0.9,0.9,1}, over={1,0.1,0.7,0.4} },
strokeColor = { default={0,0,0,1}, over={0.8,0.8,1,1} },
strokeWidth = 5
}
)
-- Center the button
button1.x = display.contentCenterX
button1.y = display.contentCenterY
-- Change the button's label text
button1:setLabel( "Start Game" )
end
That adds the button to start the game, and then I have:
local function enterFrame()
local dt = getDeltaTime()
if (scene == "MAIN_MENU") then
addMainMenu()
elseif (scene == "GAME") then
if (running == false) then
startGame()
else
moveBg(dt)
moveEnemy(enemy)
updateScore()
end
elseif (scene == "GAME_OVER") then
local gameOverLabel = display.newText( "Game Over!", 50, 20, native.systemFont, 16)
gameOverLabel:setFillColor(1, 1, 1)
end
end
As you can see, once I click on the button to start, the scene changes to "GAME", and the button should go away. The thing is: It stays there. And reading the docs, I can't find a way to set its visibility to false. How can I cease displaying a widget?
To make it not visible use .isVisible:
button1.isVisible = false
To hide it use .alpha
button1.alpha = 0.00
-- or hide just a little by 50%
button1.alpha = 0.50
To remove it:
display.remove( button1)
button1= nil
A ButtoWidget inherits from GroupObject which inherits from DisplayObject which provides the property isVisible
Overview
Controls whether the object is visible on the screen. The
property is also readable. The default is true.
Example
local rect1 = display.newRect( 100, 100, 50, 50 )
rect1:setFillColor( 0.7 )
local rect2 = display.newRect( 150, 100, 50, 50 )
rect2:setFillColor( 1, 0, 0, 0.6 )
rect2.isVisible = false
button1.isVisible = false will hide the button. It will disappear with the next screen update.
In case you don't need the button anymore you can as well just remove it by calling button1:removeSelf() or by removing it from it's parent group
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().
I need some help with the MapView in Corona SDK. I want to put the map as background (but with all functions) and, in this case, a button to the front. Here is my code:
function scene:createScene( event )
local group = self.view
if ( system.getInfo( "environment" ) == "simulator" ) then
local simulatorMessage = "Maps not supported in Corona Simulator.\nYou must build for iOS or Android to test native.newMapView() support."
local label = display.newText( simulatorMessage, 36, 40, 270, 0, native.systemFont, 12 )
label.anchorX = 0
label.anchorY = 0
end
myMap = native.newMapView( 20, 20, display.contentWidth, display.contentHeight )
if ( myMap ) then
myMap.mapType = "normal"
myMap.x = display.contentCenterX
myMap.y = display.contentCenterY
myMap:setCenter( currentLatitude, currentLongitude )
myMap:addEventListener("mapLocation", mapLocationListener)
end
backBtn = widget.newButton
{
id="backButton",
label="Back",
fontSize="30",
labelColor={ default={1, 1, 1}, over={0, 0, 0, 0.5} },
defaultFile="images/button.png",
width = dispWidth * 0.25, height = dispHeight * 0.1,
onRelease = onBackBtnRelease
}
backBtn.anchorX = 0.5
backBtn.anchorY = 0.5
backBtn.x = display.contentWidth * 0.8
backBtn.y = dispHeight * 0.8
backBtn:toFront()
group:insert( backBtn )
group:toFront() --second try to put that button to front
end
Since the MapView is a native object I can't add it to any group and 'mapView:toBack()' doesn't work as well ('attempt to index upvalue 'myMap' (a nil value)'). In Corona SDK the button appears without the map, as expected. On device I get the map without my button, not as expected.
Is it any possible to put a native object to the back, or to force something to be in the front?
What you are trying to do with the map and the button is not currently possible with CoronaSDK.
Indeed the MapView is a native object. According to Corona's documentation, you cannot place other objects on top of a MapView.
The documentation says:
Map objects, like other native display objects, cannot be inserted into groups and are always displayed on top of regular Display Objects (groups, vector, images, and text).
See documentation here
Good luck coding.
First Check the Following :
If button doesn't appear on device most common problem will be , you might misspell the Image names(Check for Caps) or check the path of the file is proper or not.
To know in detail about the mapView : http://docs.coronalabs.com/api/library/native/newMapView.html
I know you might have visited already, its better you look again.
Thanks,
I am trying to move a display object that is hiding over the right side of the screen, into the scene. It works wonderfully with images (i.e. the background), but not with texts (the coords seem correct from debugging them with print(), but they never display, I already tried the obj:toFront).
I thought that they may work inside display objects, so I put everything in a display Object: Nothing. Just the text? Neither. Anyone knows why/how to override this?
function tscreen:init()
local textGroup = display.newGroup()
local menuBackground = self:getBtn("src/bgMenu.png")
menuBackground.isVisible = false
menuBackground.anchorX = 0.5
menuBackground.anchorY = 0.5
self.menuBackground = menuBackground
local optionsText = {
parent = textGroup,
text = "Hello World",
x = centerX,
y = centerY,
width = 128,
font = native.systemFontBold,
fontSize = 14,
align = "center"
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 0, 0 )
setPos(textGroup, W, 0)
--setPos() is a custom function that assigns x and y coords
textGroup.isVisible = false
self.textGroup = textGroup
end
function tscreen:show()
local menuBackground = self.menuBackground
local textGroup = self.textGroup
local inTime = 1200
setPos(menuBackground, 2*W + centerX, centerY)
menuBackground.isVisible = true
setPos(textGroup, W, 0)
textGroup.isVisible = true
self:cancelTween(menuBackground)
self:cancelTween(textGroup)
menuBackground.tween = transition.to(menuBackground, {time = inTime, transition = easing.outExpo, x = centerX,
onComplete = function()
tscreen:cancelTween(menuBackground)
end
})
textGroup.tween = transition.to(textGroup, {time = inTime, transition = easing.outExpo, x = 0,
onComplete = function()
tscreen:cancelTween(textGroup)
print(getPos(textGroup), textGroup.width, textGroup.height)
end
})
end
I have the starters edition of Corona, so I don't have the recently implemented Composer API.
Maybe this isn't the most appropriate site to post this query since there already is a Corona SDK forum, but I'm trying anyway.
I'm not seeing anything wrong, but a group should not be necessary. Verify that the text can be seen ever: in the init(), do
local optionsText = {
text = "Hello World",
x = 0,
y = 100,
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 1, 1 )
If you can't see the text then something else is going on, maybe your init() is not being called or such. Once you see it, change the parameters to what you want (fill color etc), and test. If still works, add a transition, right after, in the init():
local easeXto100 = {
time = 2000,
transition = easing.outExpo,
x = 100,
onComplete = function() print('did you see text move from x=0 to 100?') end
}
transition.to(workText, easeXto100)
If you see it move, then move the relevant parts of code to your show(), if now it disappears this will give you clue.
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