Is there a fill_parent or wrap_content function in corona just like in android? So it will fit the background image in every Screen Orientation. Or it have a different approach to to this.
thanks in advance.
There isn't any wrap_content because you don't have view groups in corona. You have simple groups but you can emulate wrap_content. There are also text modules wich will help you, check http://developer.coronalabs.com/code/
Yes you can implement fill_parent on every Screen Orientation:
local W = display.contentWidth
local H = display.contentHeight
local background
local function drawInterface()
background = display.newRect(0, 0, W, H)
background.x = display.contentCenterX
background.y = display.contentCenterY
background:setFillColor(99, 165, 204)
end
drawInterface()
local function removeInterface()
if background then background:removeSelf() end
end
local function onOrientationChanged (event)
print("orientation changed")
W = display.contentWidth
H = display.contentHeight
removeInterface()
drawInterface()
end
Runtime:addEventListener("orientation", onOrientationChanged )
Related
I have just started to learn LUA in school, but i cannot find many helpful tutorials o the internet to aid in my learning. I have made a simple game (which doesn't work yet, i realize that) and a main menu. However, when i try to start the app, it gives me this error:
/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: attempt to index global 'showCredits' (a nil value)
stack traceback:
/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: in main chunk
[Finished in 9.4s]
I have looked the error, and I cannot seem to understand how to fix it. The other questions have said something about returning the function returning a nil value, and that I should add a return statement to the end, but that doesn't work either.
Here is the code # line 47.
function showCredits.touch(e)
playButton.isVisible = false
creditsButton.isVisible = false
creditsView = display.newImage('credits.png', 0, display.contentHeight)
lastY = name.y
transition.to(name, {time = 300, y = display.contentHeight * 0.5 - title.height - 25})
transition.to(creditsView, {time = 300, y = display.contentHeight * 0.5 + creditsView.height, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end
Here is my full code, in case the problem lies elsewhere:
display.setStatusBar(display.HiddenStatusBar)
radius = 40
smallTime = 200
bigTime = 800
score = 0
scoreInc = 2000
--HomePage
local name
local playButton
local creditsButton
local homePage
--Credits
local creditsPage
--Sounds
local circleSpawn = audio.loadSound( "circle_spawn.wav" )
local circleTap = audio.loadSound( "circle_tap.wav" )
function Main()
name = display.newImage('title.png', display.contentWidth / 2, 53)
name:scale( .5, .5 )
playButton = display.newImage('playButton.png', display.contentWidth / 2, 245)
playButton:scale( .5, .5 )
creditsButton = display.newImage('creditsButton.png', display.contentWidth / 2, 305)
creditsButton:scale( .5, .5 )
homePage = display.newGroup(name, playButton, creditsButton)
startButtonListeners('add')
end
function showCredits.touch(e)
playButton.isVisible = false
creditsButton.isVisible = false
creditsView = display.newImage('credits.png', 0, display.contentHeight)
lastY = name.y
transition.to(name, {time = 300, y = display.contentHeight * 0.5 - title.height - 25})
transition.to(creditsView, {time = 300, y = display.contentHeight * 0.5 + creditsView.height, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end
function hideCredits.touch(e)
transition.to(creditsView, {time = 300, y = display.contentHeight, onComplete = function() creditsButton.isVisible = true playButton.isVisible = true creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})
transition.to(name, {time = 300, y = lastY});
end
function startButtonListeners(action)
if(action == 'add') then
playButton:addEventListener('touch', playGame)
creditsButton:addEventListener('touch', showCredits)
else
playButton:removeEventListener('touch', playGame)
creditsButton:removeEventListener('touch', showCredits)
end
end
Main()
printScore = display.newText("Score: " .. tostring(score), display.contentWidth-80, 40, native.systemFontBold, 20)
-- A function that creates random circles
function generateCircle ()
-- Creates a new circle between 0 (the left most bounds) and the width of the display (being the content width), and also
-- 0 (the upper most bounds) and the height of the display (being the content height). The radius of the circle is 'radius'
x = math.random(radius, display.contentWidth-radius)
y = math.random(80, display.contentHeight)
score = score + scoreInc
myCircle = display.newCircle( x, y, radius )
myCircle:setFillColor( math.random(), math.random(), math.random() )
delayTime = math.random(smallTime, bigTime)
score = score + scoreInc
printScore.text = "Score:"..tostring(scores)
local spawnChannel = audio.play( circleSpawn )
timer.performWithDelay( delayTime, generateCircle )
end
generateCircle()
function myCircle:touch( event )
local tapChannel = audio.play( circleTap )
myCircle:removeSelf()
end
myCircle:addEventListener( "touch", myCircle )
The answer by greatwolf will work. But just a tip from my experience. One way I like to create functions is to try to define the name of the function first near the top of the lua file. Then I will define the function later on in the file. Something like this:
--function preallocation
local onPlayTap
local onSoundOnTap
local onSoundOffTap
local onCreditsTap
local onHelpTap
---------------------------------------------------------------------------------
-- Custom Function Definitions
---------------------------------------------------------------------------------
--Called when Sound On Button is tapped, turn off sound
onSoundOnTap = function(event)
end
--Called when Sound Off Button is tapped, turn on sound
onSoundOffTap = function(event)
end
--Called when Credits button is tapped, shows credits
onCreditsTap = function(event)
end
--Called when Help button is tapped, shows help
onHelpTap = function(event)
end
--Callback to Play button. Moves scene to Level Picker Scene
onPlayTap = function(event)
end
What this does is allow each function to be called by any other function in the file. If you do it the way you are doing it by adding the function name before the function like so:
local showCredits = {}
function showCredits.touch(e)
end
local hideCredits = {}
function hideCredits.touch(e)
end
your showCredit function will not be able to call the hideCredits function below it because the hideCredits variable has not been defined yet when the showCredit function was defined. Although this may not effect your current game, in future apps or games, you may need to call functions inside of other functions. To make this work properly, predefine all your function variables first, then define all your function afterwards. Hope this helps.
I am new to Lua and am currently working on a project called Red VS Blue. It is basically for two teams, red team and blue team, and it functions as a scoreboard. If I click the left side, red gets 1 point. If I click the blue side, blue gets one point. The screen resolution is 320 x 480, but it widens according to the device.
Here is a screenshot:
http://i40.tinypic.com/5lqosk.jpg
this is my code:
display.setStatusBar(display.HiddenStatusBar);
local redscore = 0
local bluescore = 0
local background = display.newImage("images/background.png");
local redc = display.newImage("images/redc.png", 0, 0);
local bluec = display.newImage("images/bluec.png", 240, 0);
local redtext = display.newText(redscore, 55, 100, native.systemFont, 32*4);
local bluetext = display.newText(bluescore , 290, 100, native.systemFont, 32*4);
local function redt( event )
redscore = redscore + 1;
return true
end
redc:addEventListener( "tap", redt )
local function bluet( event )
bluescore = bluescore + 1;
return true
end
bluec:addEventListener( "tap", bluet )
redc and bluec are blank pictures that act as sensors
If you want to scale the picture to always fit perfectly the half of the screen, do this:
--[[
Take half the width of the device and adjust for larger screens, dividing by
the current width of the picture to produce the value it needs to be scaled
by to fit
]]
local wScale = ((display.layoutWidth/2) - display.screenOriginX) / redc.contentWidth
--Do the same for the height
local hScale = ((display.layoutHeight/2) - display.screenOriginY) / redc.contentHeight
--Scale the image
redc:scale(wScale,hScale)
--[[
Position the image. Since the default is a central reference point, you take half of
the image's width/height and add the adjustment for larger screens
]]
redc.x = (redc.contentWidth/2) + display.screenOriginX
redc.y = (redc.contentHeight/2) + display.screenOriginY
And to make the scoreboard update when the box is touched, structure your event listener as follows:
local function redt( event )
redscore = redscore + 1;
redtext.text = tostring(redscore)
return true
end
redc:addEventListener( "tap", redt )
Repeat for the blue box. Anything else you wanted to do with it?
Hi Everyone,
I would like know one thing :
I want to make this rectangle moving from the left side to the right side with physics and loop, and make it disappear when is out the leftscreen, and make reappear it in the right side,
This is what I've made in my code but I would like know if there is another easiest way to do it.
Is it better to use transition.to ?
Ps : My game is an endless runner game where the player jump from the floor to rectangles in the air
if someone have tutorial for this, I get it ! thank you
thanks every one for all
local physics = require( "physics" )
physics.start()
physics.setDrawMode( "hybrid")
local _x = display.contentCenterX
local _y = display.contentCenterY
local speed = 10
local GroupMur1 = display.newGroup()
local Mur1 = display.newRect(680,25,680,25)
Mur1.x = _x +900
Mur1.y = _y +80
physics.addBody(Mur1, "static")
GroupMur1:insert(Mur1)
local a =1
local function update ()
if(a==1)then updateMur1() end
if(a==2)then updateMur2() end
end
function updateMur1 ()
GroupMur1.x =GroupMur1.x - speed
if(GroupMur1.x < -2000) then
GroupMur1:remove(1)
a=2
end
end
function updateMur2()
GroupMur1:insert(Mur1)
physics.addBody(Mur1, "static")
GroupMur1.x = _x + 900
a=1
end
timer.performWithDelay(1, update, -1)
Well it's true that the transition.to is really useful and easy to use, but if you want to use physics, then use the applyLinearImpulse(), which is going to be easier if you want to use also collision detection. And once the rectangle is off screen, use the if and then function. It would be something like this:
if object.x > 500 then -- for example with a rectangle of 20 by 20
object.x = -20
end
To extend what #Fannick said, which should work by the way, the generic way to trigger this (for any object of any size) would be the code below. For this, I assume you have object.anchorX = 0 or object:setReferencePoint(display.CenterLeftReferencePoint) depending on your SDK version (the former is for v2 and the latter is for v1 - This link should help clear up differences)
--if the object is past the edge (screenOriginX used to scale for bigger devices)
if object.x > (display.contentWidth - display.screenOriginX) then
object.x = 0 + display.screenOriginX - object.contentWidth
end
If you have object.anchorX = 0.5 or object:setReferencePoint(display.CenterReferencePoint) (or haven't set either - both of these are the defaults) it would be:
if object.x > (display.contentWidth + (object.contentWidth/2) - display.screenOriginX) then
object.x = 0 + display.screenOriginX - (object.contentWidth/2)
end
For object.anchorX = 1 or object:setReferencePoint(display.CenterRightReferencePoint) it would be:
if object.x > (display.contentWidth + object.contentWidth - display.screenOriginX) then
object.x = 0 + display.screenOriginX
end
Hope you find the answer if you haven't already!
Cush
thanks both of you guy;
I've used the velocity to make the rectangle start to move and a collision to make 'em disappear
All your answers help me to understand better corona SDK !
Hi I'm new to Corona SDK. I hope someone can help me out.
I have an object (called furry) dropping from the sky, using the physics engine of the Corona SDK.
When this object Furry is half way the screen I would like to play a sound effect (wee.mp3).
I'm trying to update furry.x and furry.y to control for the updated location. But I'm not sure how to do this as the physics engine updates the location based on gravity.
I hope the code below is sufficient to help me.
centerX = display.contentWidth * .5
centerY = display.contentHeight * .5
local furry = display.newImage("flurry.png")
furry.x = centerX-300
furry.y = centerY-1000
physics.addBody(furry, "dynamic", physicsData:get("flurry"))
if furry.x == centerX then
audio.play(wee)
end
So how can I get after the game has started the updated locations (x, y) of my object Furry?
you can use Runtime Listener enterFrame to detect the object location see the code below
physics = require("physics")
physics.start()
centerX = display.contentWidth * .5
centerY = display.contentHeight * .5
local function listener( event )
local furry = display.newCircle(10,10,10)
furry.x = math.random(1, display.contentWidth)
furry.y = 10
physics.addBody(furry, "dynamic")
local function removeObject()
if furry.y > centerY then
furry:removeSelf()
Runtime:removeEventListener("enterFrame", removeObject)
end
end
Runtime:addEventListener("enterFrame", removeObject)
end
timer.performWithDelay( 500, listener ,0)
with my first game I have a doodle I control with the accelerometer. In many games I've seen that when the device is titled the object (in my case a doodle fish) rotate towards the tilt so it gives an illusion the "fish" swim down and if the device is tilted up and down the "fish" swims up and down.
How do I write that in lua when working with Corona SDK?
Here is my code for moving the doodle so far;
display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval( 50 )
_W = display.contentWidth
_H = display.contentHeight
local bg = display.newImageRect("images/bg.png", 480, 320)
bg:setReferencePoint(display.CenterReferencePoint)
bg.x = _W/2
bg.y = _H/2
local player = display.newImageRect("images/doodle.png", 128, 64)
player:setReferencePoint(display.CenterReferencePoint)
player.x = _W/2
player.y = _H/2
-- Set up the Accelerometer values in Landscape
local motionX = 0
local motionY = 0
local function onAccelerate( event )
motionX = 10 * event.yGravity;
motionY = 10 * event.xGravity;
end
Runtime:addEventListener ("accelerometer", onAccelerate);
-- Make the player move on tilt.
local function movePlayer (event)
player.x = player.x + motionX;
player.y = player.y + motionY;
end
Runtime:addEventListener("enterFrame", movePlayer)
local function screenBoundaries (event)
-- Left side boundaries
if player.x < 0 + player.width/2 then
player.x = 0 + player.width/2
end
-- Right side boundaries
if player.x > display.contentWidth - player.width/2 then
player.x = display.contentWidth - player.width/2
end
-- Upper boundaries
if player.y < 0 + player.height/2 then
player.y = 0 + player.height/2
end
-- Lower boundaries
if player.y > display.contentHeight - player.height/2 then
player.y = display.contentHeight - player.height/2
end
end
Runtime:addEventListener("enterFrame", screenBoundaries)
EDIT;
I've tried to make the player rotate to tilt but it's not working, it doesn't return any errors when I run it in the simulator, what am I doing wrong?
Basically what I want to do is when the accelerometer y value increase/decrease the player(fish) swim up and down but I only want it to tilt moderately (0 to +/- 45 degrees) or what ever seems realistic. Perhaps 25 -35 degrees is optimal?
My math is not up to date so I apologize for that, can someone please help me with this?
display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval( 50 )
_W = display.contentWidth
_H = display.contentHeight
local ceil = math.ceil
local pi = math.pi
local atan2 = math.atan2
local playerRotation
-- Put the doodle fish in the center if the screen.
local player = display.newImageRect("images/doodle.png", 128, 64)
player:setReferencePoint(display.CenterReferencePoint)
player.x = _W/2
player.y = _H/2
-- My rotation function, not sure how to do it right.
local function getPlayerRotationAngle(xGravity, yGravity)
angle = math.atan2(xGravity, yGravity)
angle = angle*(180/pi)
playerRotate = ceil(360 - angle(xGravity, yGravity))
return playerRotate
end
-- Set up the Accelerometer values in Landscape
local motionX = 0
local motionY = 0
local function onAccelerate( event )
motionX = 5 * event.yGravity;
motionY = 5 * event.xGravity;
-- Should I put the rotation here or down in the movePlayer function?
player.rotation = playerRotate(event.yGravity, event.xGravity);
end
Runtime:addEventListener ("accelerometer", onAccelerate);
-- Make the player move on tilt.
function movePlayer (event)
player.x = player.x + motionX
player.y = player.y + motionY
end
Runtime:addEventListener("enterFrame", movePlayer)
Your overall code looks good.
Several games I did use something similar.
What jumps out is you mentioning
I've tried to make the player rotate to tilt but it's not working, it doesn't return any errors when I run it in the simulator, what am I doing wrong?
Are you actually running the code on a device?
The Corona SDK simulator does not support simulation for the accelerometer so the event will never fire.
If you are running on the device, include the Crawl Space Library.
This will allow you to print() any form of data including tables.
Then add
print(event)
to your onAccelerate() function, hook your device up to your development machine and watch the console for the data you actually receive. (Assuming you're developing for iOS using a Mac, use iPhone Configuration Utility to get the data). Then debug from there.
Include
io.output():setvbuf("no")
at the beginning of your main.lua to disable console output caching on the device first. Otherwise the console output of your device will be buffered and hard to debug.