Drop Down animation for Image on Tap - coronasdk

I have a image declared at some specific location.
local deselectButton = display.newImage ( "images/nutritional info/deselectButton.png" )
deselectButton.x = display.contentWidth / 2 - 15
deselectButton.y = display.contentHeight / 2 - 172
deselectButton.id = "0"
nutriinfo:insert(nutriNavBar)
When I tap on that image, I want another image to be displayed. That is, this second image should fade in and out every time I click on the above image.
local dropDown1 = display.newImage ( "images/nutritional info/dropDown.png" )
dropDown1.x = display.contentWidth / 2 - 75
dropDown1.y = display.contentHeight / 2 - 65
dropDown1:setReferencePoint(display.TopCenterReferencePoint)

After your code, just do as follows... This may help you:
local function addListener()
deselectButton:addEventListener("tap",clickFunction)
end
local clickCount = 0
function clickFunction()
deselectButton:removeEventListener("tap",clickFunction)
clickCount = clickCount + 1
if(clickCount%2==1)then
-- show the image
transition.to(dropDown1,{time=200,x=dropDown1.x,y=dropDown1.y+100,alpha=1,onComplete=addListener}) -- or parameters as you like
else
-- hide the image
transition.to(dropDown1,{time=200,x=dropDown1.x,y=dropDown1.y-100,alpha=0,onComplete=addListener})
end
end
deselectButton:addEventListener("tap",clickFunction)
Note: The above code provides you both drop down as well as fade-in/out effect. But, if you need fade in and fade out effect only, you can eliminate y parameter from transition, and if you want the drop down type effect, you can eliminate the alpha parameter.
Keep coding............... :)

Related

How to make an object move from top to bottom in Lua?

In a game I'm making, there's a square where the game is happening inside. I want the circles that spawn in to start at the top and travel down to the bottom. In the original program, they travel from left to right. How would I go about this? I don't know how to get it to start at the top and travel down instead of starting at the left and traveling to the right.
Here's the original code (I know it's a lot, sorry). I am trying to help a friend.
--Made by Joms or /u/jomy582
--Please credit me if using this in a battle.
spawntimer = 0
timerspawn = 0
storedtime = Time.time
bullets = {}
bulletsbig = {}
bulletswarn = {}
dir = 1
bigheight = {33, 98}
function Update()
spawntimer = spawntimer + (Time.dt*Time.mult)
timerspawn = timerspawn + (Time.dt*Time.mult)
--normal bullets
--change the number in the if statement to make them spawn more frequently/less frequently
--EX: spawntimer > 0.2 spawns them pretty fast
--EX2: spawntimer > 1 spawns them pretty slow
--Make sure to change the subtraction method in the if statement
if spawntimer > 0.16 then
spawntimer = spawntimer-0.16
local bullet = CreateProjectile("bullet", -Arena.width/2, math.random(-Arena.height/2, Arena.height/2))
bullet.SetVar("deadly", true)
table.insert(bullets, bullet)
end
--warning. spawns a warning every 5 seconds
--You could change it, but that may cause some bugs
if timerspawn > 2.2 then
timerspawn = timerspawn-2.2
dir = math.random(1,2)
local bulletwarn = CreateProjectile("warning", 0, Arena.height/2 - bigheight[dir])
bulletwarn.SetVar("warningtimer", 0)
bulletwarn.SetVar("soundtimer", 0)
bulletwarn.SetVar("animtimer", 0)
bulletwarn.SetVar("anim", 1)
table.insert(bulletswarn, bulletwarn)
end
--controlling normal bullets
--a simple method that moves the bullets 1 pixel each frame
--you can change it by editing the bullet.move
--EX: bullet.Move(5*Time.mult, 0) moves the bullets 5 pixels each frame
for i=1, #bullets do
local bullet = bullets[i]
if bullet.isactive then
bullet.Move(2*Time.mult, 0)
if bullet.y > -Arena.height/2 then
bullet.Remove()
end
end
end
--controlling warning timer
--a method that controls the warning timer
for i=1, #bulletswarn do
local bullet = bulletswarn[i]
if bullet.isactive then
local warningtimer = bullet.GetVar("warningtimer") + (Time.mult*Time.dt)
local soundtimer = bullet.GetVar("soundtimer") + (Time.mult*Time.dt)
local animtimer = bullet.GetVar("animtimer") + (Time.mult*Time.dt)
local bulletSprite = bullet.sprite
local anim = bullet.GetVar("anim")
--flashing colors
--change the animtimer > TIME where time is how often you want the warning to blink
--warnings last for 3 seconds. Can change that as well
--to get different colors, find the rgb value of the color you want and insert them below
if animtimer > 0.08 then
animtimer = animtimer-0.08
if anim == 1 then
local r = 0 --put Red value here
local g = 0 --put Green value here
local b = 169 --put Blue value here
bulletSprite.color = {r/255, g/255, b/255} -- changes the color to whatever you'd like. Uses RGB.
bullet.SetVar("anim", 2)
elseif anim == 2 then
local r = 0 --put Red value here
local g = 0 --put Green value here
local b = 255 --put Blue value here
bulletSprite.color = {r/255, g/255, b/255} -- changes the color to whatever you'd like. Uses RGB.
bullet.SetVar("anim", 1)
end
end
--plays a timer evert 10 frames for 3 seconds.
--change the soundname to change the sound
--change the soundtimer > 0.16 to change how often it plays
--can change how long it lasts as well by changing the less than statement
if soundtimer > 0.10 then
soundtimer = soundtimer-0.10
Audio.PlaySound("alarm")
end
--this controls when to spawn the bullets
--change the statement to change how long the warning timer is
if warningtimer > 2 then
warningtimer = warningtimer-2
Audio.PlaySound("shoot")
local bullet1 = CreateProjectile("leftbigbullet", Arena.width/2+30, Arena.height/2 - bigheight[dir]) --where to spawn them
bullet1.SetVar("speed", -4) --how fast
bullet1.SetVar("deadly", true)
local bullet2 = CreateProjectile("rightbigbullet", -Arena.width/2-30, Arena.height/2 - bigheight[dir]) --where to spawn them
bullet2.SetVar("speed", 4) --how fast
bullet2.SetVar("deadly", true)
table.insert(bulletsbig, bullet1)
table.insert(bulletsbig, bullet2)
bullet.Remove()
end
bullet.SetVar("warningtimer", warningtimer)
bullet.SetVar("animtimer", animtimer)
bullet.SetVar("soundtimer", soundtimer)
end
end
--controlling big bullets
--this method controls the big bullets
for i=1, #bulletsbig do
local bullet = bulletsbig[i]
if bullet.isactive then
local speed = bullet.GetVar("speed")
bullet.SendToBottom()
bullet.Move(speed*Time.mult, 0)
if bullet.absx > 700 or bullet.absx < -70 then
bullet.Remove()
end
end
end
end
function OnHit(bullet)
if(bullet.getVar("deadly")) then
Player.Hurt(3)
end
end

Spawn 3 objects but no duplicates in a row - Corona SDK

Im looking to spawn 3 objects (Red,Green,Blue) in seperate columns but should not duplicate. So somehow Im looking for it to check the colours in the other columns and place the one thats left over.
So if Blue and Red are already spawned, the last column will be a Green etc.
Should I need to specify specific orders inside a table and then everytime I spawn I just choose a random order from within that table, or is there a better way?
Cheers
You will always have to make sure you use the colour only once. How and when you do that is completely irrelevant.
Of course creating objects randomly is not very efficient as you would risk to create some you cannot use.
So best would be to create 3 different objects and remove one of them randomly every time or to spawn an object using a random colour, removed from a colour list.
You can create a list of colors and shuffle it. Something like that:
math.randomseed( os.time() )
local colors = {
{ 1,0,0 }, -- red
{ 0,1,0 }, -- green
{ 0,0,1 }, -- blue
}
local function shuffleTable( t )
local rand = math.random
assert( t, "shuffleTable() expected a table, got nil" )
local iterations = #t
local j
for i = iterations, 2, -1 do
j = rand(i)
t[i], t[j] = t[j], t[i]
end
end
shuffleTable( colors )
local px = display.contentCenterX
local py = display.contentCenterY - 200
for i = 1, #colors do
local rect = display.newRect( px, py + 100 * i, 200, 100 )
rect.fill = colors[i]
end

Fade text color in Corona SDK

I have a list of color codes and want to make a text fade the colors smoothly like in this picture:
Is this possible with Corona? I couldn't find a way to do this yet.
Try this function I made, might not be perfect but it works for me:
-- Transitions a display object from one color to another (based on 0 to 1 values)
function applyColorTransition(oDisplayObject, nDelay, nStartR, nEndR, nStartG, nEndG, nStartB, nEndB)
if oDisplayObject.setFillColor then
local nIterations = 10
local nStepsR = (nEndR - nStartR) / nIterations
local nStepsG = (nEndG - nStartG) / nIterations
local nStepsB = (nEndB - nStartB) / nIterations
local nI = 1
timer.performWithDelay( nDelay, function()
oDisplayObject:setFillColor(nStartR + nStepsR * nI, nStartG + nStepsG * nI, nStartB + nStepsB * nI)
nI = nI + 1
end, nIterations)
end
end

Simple Corona SDK App - Touch Event

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?

Make moving a rectangle (Remove/Reappear) Corona SDK

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 !

Resources