Corona SDK - How can i change color text with transition? - lua

Can i change color of text field with transition?
I try with normal transition like this but did not work.
explainer = display.newText("my text", 100,100,"Hiragino Maru Gothic Pro",30)
transition.to(explainer, { time=100, color="rgba(255,0,0)" })

You really can't do this with transition.to. You would have to do it in an enterFrame listener and increment your R, G, B values during each step.

Here's a code that let's you make a blink effect. You can remove the part that changes the increment and it will work for you:
local min_r,min_g,min_b=100,100,255 -- Initial color
local max_r,max_g,max_b=255,255,255 -- End color
local color_steps=15 -- Adjust this to make it more fast/slow
local current_step=0
local increment=1
local step_r=(max_r-min_r)/color_steps
local step_g=(max_g-min_g)/color_steps
local step_b=(max_b-min_b)/color_steps
function blink()
if (rowPlayerName and rowPlayerName["text"] and rowScore and rowScore["text"]) then
rowPlayerName:setTextColor( min_r+current_step*step_r, min_g+current_step*step_g, min_b+current_step*step_b )
rowScore:setTextColor(min_r+current_step*step_r, min_g+current_step*step_g, min_b+current_step*step_b )
current_step=current_step+increment
if (current_step>=color_steps) then
current_step=color_steps-1
increment=-increment
elseif (current_step<0) then
current_step=0
increment=-increment
end
timer.performWithDelay(50, blink)
end
end
blink()
In this case, rowPlayerName and rowScore are 2 display.newText that need to be changed together.

What you could also do, is just put another object over it with different color and set its alpha to 0. Then using transition.to method change this objects alpha property to 1.

Related

How do I set a part back to its original position and orientation? (Lua/Roblox)

Is there an easier way to set the part to its original position instead of having to copy and paste its new location? I have falling blocks for my parkour but I want them to reset back to its original position and orientation after 3 seconds?
You'll want to save the part's original CFrame on server start, or whenever the part's creation occurs.
local Part: Part = nil -- Change nil to the part.
local CoordinateFrame: CFrame = Part.CFrame
--[[
At this point, you'd write all of your code here.
Once finished, the script will then continue to return the part to it's previous CFrame which is stored into a variable (CoordinateFrame).
]]
Part.CFrame = CoordinateFrame

Trying to Reset Game in Lua

Very new to game development and Lua in general here. I'm making a platformer, and I want to be able to restart the game once a character collides with a certain object, and show a title before that. I also want to reset the character's position back to 0,0 once that happens, but I don't know how.
I made a global variable called WIN that's set to true if the character collides with the object, which works, but then going into my love.draw() function, I have this:
function love.draw()
-- begin virtual resolution drawing
push:apply('start')
-- clear screen using Mario background blue
love.graphics.clear(108/255, 140/255, 255/255, 255/255)
-- renders our map object onto the screen
love.graphics.translate(math.floor(-map.camX + 0.5), math.floor(-map.camY + 0.5))
map:render()
if WIN == true then
love.graphics.printf('NEXT LEVEL', 0, 30, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Continue to Next Level', 0, 45, VIRTUAL_WIDTH, 'center')
love.load()
end
-- end virtual resolution
push:apply('end')
end
When I actually collide with the object, I get the following error:
Error
push.lua:48: love.window.setMode cannot be called while a Canvas is active in love.graphics.
Traceback
[C]: in function 'windowUpdateMode'
push.lua:48: in function 'setupScreen'
main.lua:43: in function 'load'
main.lua:116: in function 'draw'
[C]: in function 'xpcall'
The error lines are in my love.load() function, which is as follows:
function love.load()
-- sets up a different, better-looking retro font as our default
love.graphics.setFont(love.graphics.newFont('fonts/font.ttf', 8))
-- sets up virtual screen resolution for an authentic retro feel
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = true
})
love.window.setTitle('Super Mario 50')
love.keyboard.keysPressed = {}
love.keyboard.keysReleased = {}
end
I'm guessing the issue is that I can't set up screen again after already doing that once, but I don't know how to fix this and create a fresh start. Any help is appreciated!
love.load() runs once. That one time is when your game starts up. Don't call love.load() (or love.update()) within love.draw() or vice versa. The Love2D game engine calls these functions, so you don't call them.
Update the start position within love.update() not love.draw(). love.draw() is only for drawing stuff. I believe it would be the View in a model-view-controller architecture.
To restart the position of your object, you would have to do something like
if WIN == true then
push.x = push.start.x
push.y = push.start.y
map.camX = map.start.camX
map.camY = map.start.camY
end
Or something along those lines, it's hard to know without seeing your variables. Your love.draw() will then draw the things at the coordinates that they are at.
To reset/restart the Game without reloading the executable use:
love.event.quit('restart')
But before doing this make a dark screen before with red dropping letters: GAME OVER

How do I make a blinking effect in corona SDK?

Is there any way I could make a ball, that blinks? I would like to use two colors, so it blinks first with one color, then the other. I have no idea how to do this.
local ball = display.newCircle(10,10,10)
Have You checked Corona SDK API Reference ?
To quote an example:
local square = display.newRect( 0, 0, 100, 100 )
transition.blink( square, { time=1000 } )
To address the second part of Your question, I guess, You could use onStart and onRepeat parameters. Changing the primary color accordingly to blink counter on start and increasing counter of blinks on repeat.

Multiple light sources in a tile-based lighting system

I have made a rudimentary tile based lighting system in ROBLOX by changing color based on distance from lightsource. I want to be able to put multiple lightsources, but I've ran into some problems:
The performance goes down a bit (from full 60 to 45-50) when I try to add another light source.
It doesn't work correctly, the lightsources cut out eachother's light and such.
my code:
local sp = script.Parent
local lightBrightness
local lightPower = 1
local grid = game.ReplicatedStorage.Folder:Clone()
grid.Parent = workspace.CurrentCamera
local lightSource = game.ReplicatedStorage.LightSource:Clone()
lightSource.Parent = workspace.CurrentCamera
lightSource.Position = grid:GetChildren()[math.random(1,#grid:GetChildren())].Position
for _, v in pairs(grid:GetChildren()) do
if v:IsA("Part") then
game["Run Service"].RenderStepped:connect(function()
local lightFact = (lightSource.Position-v.Position).magnitude
lightBrightness = lightPower - (lightFact*10)/255
if lightFact < 35 then
v.SurfaceGui.Frame.BackgroundColor3 = v.SurfaceGui.Frame.BackgroundColor3.r >= 0 and Color3.new((100/255)+lightBrightness*.85,(50/255)+lightBrightness*.85,10/255+lightBrightness) or Color3.new(0,0,0)
elseif lightFact > 35 and lightFact < 40 and v.SurfaceGui.Frame.BackgroundColor3.r > 0 then
v.SurfaceGui.Frame.BackgroundColor3 = Color3.new(0,0,0)
end
end)
end
end
The reason your code is running slowly is because the RenderStepped event runs every frame render, which is (typically) every 1/60th of a second. Try using the Stepped event to improve performance, as it only fires ever 1/30th of a second.
As for your light sources cancelling each other out, it appears from looking over your code that each light source affects the tiles around it without checking what light sources were affecting the tile before.
For example, light source A has a lightFact of 37 affecting tile A, so tile A changes it's appearance. Then, light source B, which is later in the loop, has a lightFact of 32. Well, the final lightFact value for that tile is going to be 32, effectively cancelling out the initial light sources effect.
In order to fix this issue, you need to store the current lightFact variable for each tile as you progress through the loop. My recommendation (alas, maybe not the fastest) would be to create a new Instance of IntValue inside of each tile, storing the current affecting lightFact value on the tile. Then, whenever a light source is about to check the tile, check if it has a child lightFact, and if it does, add that value to the lightFact value in your code. Therefore, the lightFact value you use to apply to the tile doesn't cancel out the last light source, because it incorporates that other lightsources lightFact value in its own calculation. Make sense?

How to use the character property in robox?

for index, each in pairs (game.Players:GetPlayers()) do
if each.TeamColor == BrickColor.new("White") then
each.TeamColor = BrickColor.new("Bright red")
each.Character:BreakJoints()
redCount = redCount + 1
-- Wont load character appearance when they start the game
each.CanLoadCharacterAppearance = false
each.Character.Torso.BrickColor = BrickColor.new("Bright red")
each.Character["Right Arm"].BrickColor = BrickColor.new("Bright red")
each.Character["Left Arm"].BrickColor = BrickColor.new("Bright red")
The problem is where I need to change what the user looks like but when I try it the character doesn't change to the new colour. The character appearances doesn't load but it doesn't change their color. This is laser tag game, so this is at the beginning of the game when its chooses teams and gives them color. I was wondering if someone could explain it to me.
Also when I say
each.CanLoadCharacterAppearance = false
each.Character.Torso.BrickColor = BrickColor.new("Bright red")
each.Character["Right Arm"].BrickColor = BrickColor.new("Bright red")
will I have to say that every time they respawn or will they have the same colour after they die.
The Character property of the Player class is not an Object in itself. But it is a pointer to the character model that usually resides in the Workspace (the name of the model is the same as the playername).
Thus every change to Player.Character will reset when the player's character respawn.
In your case you first kill the character
each.Character:BreakJoints()
And then change the color of the character's torso
each.Character.Torso.BrickColor = BrickColor.new("Bright red")
To be noted here is that the character do NOT respawn immediately when it dies, thus your changes to the color is made on the remains of the character (the ones that gets removed after a respawn) and not the new one.
A good way to fix this would be to use:
Game.Players.PlayerAdded:connect(function(Player)
Player.CharacterAdded:connect(function(Character)
Character.Torso.BrickColor = Player.TeamColor
end)
end)
The event Player.CharacterAdded is fired every time the Player's character are respawned, thus this code makes sure that the player's character's torso always is the same color as the player's team.
When you then want to respawn the players just do:
for i,Player in ipairs(Game.Players:GetPlayers()) do
Player:LoadCharacter()
end
Note that Player:LoadCharacter() respawn the characters immediately (you can use Character:BreakJoints() instead, but Player:LoadCharacter() is neater).
Also remember that you still need to change the Player.TeamColor before you respawn the character.
Hope this helps!

Resources