I'm new to coronaSDK and I am facing a problem now. I just wanted to know how to check if an image is visible or not.
local function buttonListener1( event )
local lockedImage = display.newImage("locked.png")
lockedImage.x = 240
lockedImage.y = 170
lockedImage.isVisible = true
local myClosure = function() lockedImage.isVisible = false end
timer.performWithDelay(1000,myClosure)
end
What I want to do is to check first if lockedImage is visible or not. If true then lockedImage won't appear again once triggered. Else, it will appear and perform timer.performWithDelay(1000,myClosure). Any help is sincerely appreciated. Sorry for my english. Thanks in advance!
How about:
if myImage.isVisible then
... do something ...
end
But to do what you describe the lockedImage must be created outside the buttonListener1, so the above technique implies something like:
local lockedImage = display.newImage("locked.png")
lockedImage.x = 240
lockedImage.y = 170
lockedImage.isVisible = false
lockedImage:addEventListener...
... presumably some listener might toggle lockedImage.isVisible
to true at some point ...
-- some button listener:
local function buttonListener1( event )
if lockedImage.isVisible == false then
-- show for one second
lockedImage.isVisible = true
local myClosure = function() lockedImage.isVisible = false end
timer.performWithDelay(1000, myClosure)
end
end
Related
I've done my app and it works great on the Corona Simulator, but, once I run it on my device, it doesn't run a bunch of instruction.
These are:
function onCollision( event )
if ( event.phase == "began" ) then
if event.object1.myName == "ground" and event.object2.myName == "spaceShip" then
local a = score.get()
print(a)
local b = score.load()
print(b)
if (a < b) then
best.alpha = 1
scoreToBeat.text = score.load()
scoreToBeat.alpha = 1
else
score.save()
newRecord.alpha = 1
end
timer.cancel(tmrScore)
gameOver.alpha = 1
tapToReplay.alpha = 1
replay.alpha = 0.01
fade.alpha = 0
timer.cancel(tmrIS)
spaceShip.alpha = 1
if(playEffects) then
media.playEventSound( "sounds/gameover.mp3" )
playEffects = false
end
speed = 0
end
end
end
Runtime:addEventListener( "collision", onCollision )
Particularly, it performs only "speed = 0" and only the first time I run the app, which means that if I start a new game, it won't even work "speed = 0".
I'm 100% sure that the app is running on the device is the same that runs on the simulator (I tried to change some text).
What can I do?
There could be multiple reasons: the physics not started when get back to scene; the objects aren't dynamic; one of them no longer exists or is no longer a physics object; the handler was removed when game ended and not restarted.
To figure out, add some print statements to see what parts execute:
function onCollision( event )
print 'entered collision'
if ( event.phase == "began" ) then
print 'collision began'
if event.object1.myName == โgdโ and event.object2.myName == โheroโ then
print 'collision objects are gd and hero'
speed = 0
if (score.get() < score.load()) then
print 'score smaller'
...
else
print 'score not smaller'
...
end
else
print( 'collision objects are', event.object1.myName, event.object2.myName )
end
end
end
Update:
I had a typo score = 0, should have been speed = 0.
does someone use gameNetwork provided by Google Play? Im having a little problem with it, im trying to fetch personal best score and its not working well.
Here is the code that i use
function loadScoreCallback(event)
oldScore = event.data[5].formattedValue
end
gameNetwork.request( "loadScores",
{
leaderboard =
{
category = "thecategory id",
playerScope = "Global", -- Global, FriendsOnly
timeScope = "AllTime", -- AllTime, Week, Today
range = {1,5},
playerCentered = true,
},
listener = loadScoreCallback
})
I also tried something like
function loadScoreCallback(event)
oldScore = event.data
end
gameNetwork.request( "loadScores",
{
leaderboard =
{
playerID = playerName,
category = "CgkIptXi1qgCEAIQAw",
playerScope = "Global", -- Global, FriendsOnly
timeScope = "AllTime", -- AllTime, Week, Today
playerCentered = true,
},
listener = loadScoreCallback
})
Also didnt work :/
I had some trouble figuring this out, but I found a solution that works for me.
this is the listener that is called when the user submits a score.
local function onGameNetworkRequestResult( event )
if event.type == "setHighScore" then
local function tempScoresFct(event)
if event.data then
local playerRank = event.data[1].rank
local currentValue = event.data[1].value
if lb.tempScore <= event.data[1].value then
native.showAlert("Score submitted", "You score is lower or equal than your current score in the leaderboard("..currentValue.."), nothing changed. Your global, all time rank is:\n" .. playerRank .. "\nTo see how you are doing among your friends and in other time scales, click the button \"show leaderboard\".", {"Ok"})
else
native.showAlert("Score submitted", "Your score was successfully uploaded to the leaderboard. Your global, all time rank is:\n" .. playerRank .. "\nTo see how you are doing among your friends and in other time scales, click the button \"show leaderboard\".", {"Ok"})
end
end
end
gameNetwork.request( "loadScores",
{
leaderboard =
{
category = event.data.category,
playerScope = "Global", -- Global, FriendsOnly
timeScope = "AllTime", -- AllTime, Week, Today
range = {1,1}, --Just get one player
playerCentered = true, -- and this player is the player that is logged in
},
listener = tempScoresFct
})
end
end
and this ist the function that submits the score:
function lb.submitScoreFct(event)
if event.phase == "ended" then
if gameNetwork.request("isConnected") then
--show leaderboards
print("submitting score")
local myCategory
local categoryName
if event.target.category == 1 then
myCategory = "ategoryID" --palo alto
categoryName = "Palo Alto"
elseif event.target.category == 2 then
myCategory = "ategoryID" --groningen
categoryName = "Groningen"
elseif event.target.category == 3 then
myCategory = "ategoryID" --sp2
categoryName = "SP2"
end
--local score = mRand(100,1000)
local score = event.target.score
lb.tempScore = score -- i use this to verify if the uploaded score was higher or lower then the value that is already there
print("Added " .. score .. " to " .. categoryName)
gameNetwork.request( "setHighScore",
{
localPlayerScore = { category=myCategory, value=tonumber(score) },
listener = lb.onGameNetworkRequestResult
})
end
else
print("user needs to log in")
-- Tries to login the user, if there is a problem then it will try to resolve it. eg. Show the log in screen.
gameNetwork.request("login",
{
listener = loginListener,
userInitiated = true -- if that is false, than this process is silent ie dont pop up a log in if the user is not logged in
})
end
return true
end
Oh and one thing I figured out, is that this listener functions are fragile. If there is one error inside, they quit and sometimes dont give any error.
For example if you try to print(event.data.rank) (instedt of print(event.data[1].rank)) there is no error but the function ends at this point and the rest of the function is not executed...
Well so i found a better idea, since not everyone is online all the time i made my own saving system, and it fetches high score from there. But i still dont know why it doesnt work, well at least i solved it with this.
I'm pretty new to corona and I can't seem to find a solution for my problem:
I have levels in my game and I am using storyboards, when the player clicks on the nextlevelbutton they get sent to the level2 scene, the scene starts with storyboard.removeALL() but that doesn't remove the previous scene and my background is messed up.
function nextlevel(event)
storyboard.gotoScene( "level2" )
end
function win ()
nextlevelbutton = display.newRoundedRect( display.contentCenterX, 285, 120, 30, 3 )
nextlevelbutton:setFillColor( 61/255,61/255,61/255 )
nextleveltxt = display.newText( { text = "Next Level", font = native.systemFontBold, fontsize = 20, x= display.contentCenterX, y = 285 } )
nextlevelbutton:addEventListener( "tap", nextlevel )
end
This is all inside the scene:enterScene function.
This is the start of the level2 scene:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
storyboard.removeAll( )
I tried putting the nextlevel event inside the win function but nothing happened.
Thanks
This should help you:
http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/
If your loading a menu and you want to remove EVERYTHING from all other classes then use:
storyboard.purgeAll()
that should stop all the listeners and remove all the objects.
Another thing you could do is add this code:
function scene:didExitScene( event )
storyboard.purgeScene( "sceneyouareleaving" )
end
scene:addEventListener( "didExitScene" )
All you need to do is call that function and your done :)
Hope this helps!
Also, as your 'messed up' background might suggest, please remember to add all the objects you create to view group:
function scene:createScene(event)
local group = self.view
local params = event.params
And then when you create objects, eg:
background = display.newImageRect("gfx/bg.png", 1425, 900)
background:setReferencePoint(display.TopLeftReferencePoint)
background.x = 0
background.y = 0
background:setFillColor(0, 255, 255)
Remember to add it to group:
group:insert(background)
I somehow managed to solve it. I think I did something wrong with the positions of the functions and groups. Anyway thanks for helping!
I'm new in Android games using corona and I use a timer to make a local display of coin with a 50x repetition.
What I'm trying to do is if the character collide in on of coin the coin will disappaer, the problem is how can I hide that certain coin?
here's my code how I creating the coin.
function coins()
coin1 = display.newImage( "coin1.png")
coin1.x = math.random(0, 600)
coin1.y = math.random(0, 400)
coin1.myName = "wewe"
physics.addBody(coin1, {friction = 1, density = 1})
end
timer.performWithDelay(
1000, coins, 100 )
have something like this
local function removeCoin(self,event)
if(event.phase == "began") then
self:removeSelf()
end
end
And in coins() add the following
coin1.collision = removeCoin
coin1:addEventListener("collision",coin1)
This should make it that upon a coin experiencing a collision removeCoin is invoked, which removes it's caller, in this case a coin.
You can stop both objected being removed by doing something like this:
if(event.phase == "began" and self.myName == 'coin') then
self:removeSelf()
end
I'm new to Coronoa and Lua, and I'm trying to figure out how to close a map and textbox. The map and text box appear over the home screen, and I was able to create a button (just a genreic black x) and make it close but I can not get the map or text box to close. Below is the code snippet I am using, but I am stuck. I have searched Google, and read through their documentation, and I am just missing something.
local obj = display.newImageRect( "closeButton.jpg" ,25,25 )
obj.x = 60
obj.y = 410 -- replaced with newImageRect for dynamic scaling (adjust X & Y as required)
obj.touch = function (event)
local btn = event.target
if event.phase == "ended" then
btn.alpha = 0 -- example to show the function doing something
myMap.alpha = 0
textBox.alpha = 0
end
end
-- begin detecting touches
obj:addEventListener( "touch", obj.touch)
myMap = native.newMapView( 25, 0, 275, 180 )
myMap.mapType = "hybrid" -- other mapType options are "satellite" or "hybrid"
myMap.isScrollEnabled = true
myMap.isZoomEnabled = true
myMap.isLocationUpdating = true
isVisible = myMap.isLocationVisible
myMap:setCenter( 38.354614, -81.726351 )
myMap:addMarker( 38.354614, -81.726351)
-- Adding the Text Box that contains the Directions
textBox = native.newTextBox( 22, 183, 280, 225 )
textBox.text = "blah blah blah boring directions."
local group = display.newGroup()
group:insert( obj )
I keep getting "attempt to index local 'myMap' (a nil value)", and the same error for textBox. So if anyone can help, its appreciated.
Locally declare your 'MapView' and 'textBox' above the 'obj.touch' function, as below:
local myMap;
local textBox;
Note: Corona mapView does not supported in the simulator.
Keep coding................. ๐