Fade text color in Corona SDK - coronasdk

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

Related

Garry's Mod trying to make a ring like health bar with seperators smooth

So I have a ring looking like health bar in GMod, and I'm trying to make the health bar go down smoothly as I lose health, and obviously I got no idea how to do that, I've tried math approach and lerping but it didn't work (probably my poor coding was at fault) so your suggestions with those methods are still welcome
This is the function that draws my health
local function healthBar()
local hp = ply:Health()
local maxHp = ply:GetMaxHealth()
surface.SetDrawColor(225,225,225,255)
for i = 0, 180, 45 do
function HpAng(i, maxAng)
local curSeg = (i / maxAng) + 1
local segAng = (maxHp / 5)
local segMax = segAng * curSeg
if segMax <= hp then
return i + maxAng
end
return (i + maxAng) * (hp/segMax)
end
draw.JRing(ScrW() / 2 + 750, ScrH() / 2 + 260, 75, 8, i + 2, HpAng(i, 45))
end
end
This is how the health bar looks like:
https://i.stack.imgur.com/TsKzm.jpg
math.Approach moves one value towards another value by a given increment, and so the idea is that you want to increment the health currently displayed towards whatever the player's current health is, in each frame. Assuming your healthBar() function is run inside a rendering function such as HUDPaint, this should work:
local lerpHp = 0
local lerpSpeed = 0.1
local function healthBar()
local hp = ply:Health()
local maxHp = ply:GetMaxHealth()
if (lerpHp != hp) then
lerpHp = math.Approach(lerpHp, hp, math.abs(hp - lerpHp) * lerpSpeed)
end
surface.SetDrawColor(225,225,225,255)
for i = 0, 180, 45 do
function HpAng(i, maxAng)
local curSeg = (i / maxAng) + 1
local segAng = (maxHp / 5)
local segMax = segAng * curSeg
if segMax <= lerpHp then
return i + maxAng
end
return (i + maxAng) * (lerpHp/segMax)
end
draw.JRing(ScrW() / 2 + 750, ScrH() / 2 + 260, 75, 8, i + 2, HpAng(i, 45))
end
end
The lerpHp variable is outside the function, since we need to keep track of what the old health was between frames.
math.abs(hp - lerpHp) increases the speed of the animation proportional to the difference in health, meaning the health bar will move faster if you have lost a larger amount of health. This is optional but this is the behaviour you'll see in most Garry's Mod HUDs.

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 !

Cannot change value of object in lua

i have a table(array) where i keep the references of some images. Here is the code:
local rowcount = 8
local colcount = 4
local blockWidth = display.contentWidth / (colcount*4)
local blockHeight = display.contentWidth / (rowcount*2)
local row
local col
local pan = 3
local i=0
for row = 1, rowcount do
for col = 1, colcount do
local x = (col - 1) * blockWidth + pan + 30
local y = (row + 1) * blockHeight + pan
local random= math.random(1,6)
random = revisarTres(i, random)
local image = display.newImage(images[random], 0, 0)
image.x = x
image.y = y
print (x)
print (y)
image.value = random
image:addEventListener("touch", blockTouch)
block[i] = image
i=i+1
end
end
I keep the references in block of my 31 images.
Then i make a transition for one image to another, conversely. And i want to change the value, the coordenates and the reference of this two. I'm making this:
block[old].value, block[new].value = block[new].value, block[old].value
block[old].x, block[new].x = block[new].x, block[old].x
block[old].y, block[new].y = block[new].y, block[old].y
block[old], block[new] = block[new], block[old]
Old is the position of one of the images i want to change and new of the other one.
The reference is changing but the value doesn't.
Please can someone help me,
Thanks!
One small note (addition?):
if You ever find a table, where You can't directly change values - check if it's not readonly one. More about this kind of technique using Lua metatables: lua-users Wiki - Read Only Tables

Drop Down animation for Image on Tap

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............... :)

Having an id for images in Lua (Corona SDK)

I'm trying to make a game similar to candy crush in Lua. Here is the code:
local images = {
"images/beer.png",
"images/beef.png",
"images/canned_food.png",
"images/cup_ice_cream.png",
"images/french_fries.png",
"images/pepper.png"
}
local rowcount = 8
local colcount = 4
local blockWidth = display.contentWidth / (colcount*4)
local blockHeight = display.contentWidth / (rowcount*2)
local row
local col
local pan = 3
for row = 1, rowcount do
for col = 1, colcount do
local x = (col - 1) * blockWidth + pan
local y = (row + 1) * blockHeight + pan
local block = display.newImage(images[math.random(1, 6)], x, y)
block:addEventListener("touch", blockTouch)
end
end
I need to know which image is moving, to know if with the new position they made 3 in a line.
So my question is, how can i have an id or a identifier to know which image the user is moving in the table?
Thanks for your help
you must put ID in each object you create for example:
local function getID(event)
t = event.target
print(t.id)
end
local beef = display.newImage("images/beef.png",)
beef.id = "beef"
local canned_food= display.newImage("images/canned_foods.png",)
canned_food.id = "cannedfoods"
local fries = display.newImage("images/fench_fries.png",)
fries.id = "fries"
beef:addEventListener("tap", getID())
canned_food:addEventListener("tap", getID())
fries:addEventLister("tap", getID())
hopes this helps :)
I would put your blocks into a table to keep track of each of them. But to answer your specific question, Lua allows you to add any method or attribute to an object, so you can do:
block.name = "Beer"
block.color = "Green"
block.gobbldygook = 400
Then in your tap/touch handler, your "event.target" is the object, so you can say:
print(event.target.gobbldygook)

Resources