Text alignment to left in Corona - lua

My problem is that I can't align my text to left. I need to set a position where the text will start and it will go to the left and it wont be centered. Its like in html, you make position and it goes from that position to the right, eventually it makes another line. Maybe a picture example will make you understand better because its hard to describe it.
How it should look and where it should start all the time if I add more text it should just continue to the right.
How it looks if I add more text, and that's not how I want it
Here's a bit of the code
text = display.newText("Fair", 0, 0, "HelveticaNeue-Light", 22)
text.x = halfW - 300
text.y = halfH + 110
text.align = "left"

Are you using the Current Public Release (2014.2100) + ?
Try using text.anchorX = 0 instead of text.align = "left"
So anchorX = .5 is same as "middle" and anchorX = 1 is same as "right".

Related

Mouse cylinder behaviour (dual-screen config)

I've already asked a question about this in Unix & Linux SE and I so actually use xdotool.
The idea is that the mouse goes at the left of the left screen when it reaches the right of the right screen and vice versa.
But I recently saw the mouse.coords table in the awesome API, so I'd like to give up xdotool to set mouse coordinates as do xdotool with this possibility.
I suppose I should add a signal to the root to know when the mouse is on the edge, but I don't konw how to do this...
I give a try to my idea, and it works. Here's the code for my right wibox :
s.myjumpbox = awful.wibar({
position = "right",
screen = s,
width = 1,
opacity = 0,
ontop = true,
-- bg = beautiful.noir
})
s.myjumpbox:connect_signal("mouse::enter", function(w)
mouse.coords {
x = 2 ,
y = mouse.coords().y
}
end
)
Edit : add Uli's suggestions

ScrollView or NewText is not infinite

I am making a changelog for my app and I inserted a bunch of text into display.newText. Is there a limit to the amount of characters display.newText can take or is it a problem in ScrollView? My code:
local widget = require( "widget" )
scrollView = widget.newScrollView
{
hideBackground = true,
hideScrollBar = true,
left = 0,
top = 170,
width = display.contentWidth,
height = contH - 300,
topPadding = 150,
bottomPadding = 20,
horizontalScrollDisabled = true,
verticalScrollDisabled = false,
}
ChangeLogText = [[ ...a bunch of text here... ]]
ChangeLog = display.newText(ChangeLogText, contW / 2, 4730, contW - 100, 0, "Fonts/visitor1.ttf", 40)
scrollView:insert(ChangeLog)
I can see ~164 lines of text, but it just stops there... Is there a way to make it infinite?
I had the same problem just recently - too bad that such limitations are detailed nowhere in the API.
I started with this github repository, witch contains the full widget library of Corona SDK (it's no longer developed AFAIK), but managed to find nothing.
The workaround I eventually settled with, is to start with only the first part of the text inserted into the text display object, and then use scrollView scrollListener to reload text whenever top/bottom limit is reached.
Part of scrollListener (event.direction == "up" is basically the mirror image of this):
elseif ( event.direction == "down" ) then
--check if limit is reached or close to it, y is from scrollView:getContentPosition(), -800 is arbitrary
if ( event.limitReached ) or y > - 800 then print( "Reached top limit" )
--firstS stores number of first visible sign of a string
if chat.firstS > 1 then
--copy string
local shownText = stringText
--update display object
chat.text = shownText
local firstS, lastS
--do while display object is too high - I found that scrollView stops scrolling ~25000 height
while (chat.height) > 24000 do
--basically cut the text in half
firstS = chat.firstS - #shownText/4
if firstS < 1 then firstS = 1 end
lastS = firstS + #shownText/2
shownText = string.sub( stringText, firstS, lastS)
chat.text = shownText
end
chat.firstS = firstS
chat.lastS = lastS
--scroll to halfway position in no time for seamless transition
mainView:scrollToPosition({y = -(chat.height)/2, time = 0})
end
end
end
This is an early version and needs some refinement, but I hope it helps. If you find anything better, let me know. Also, I display text from bottom, so you might need to adjust it accordingly.

Get x,y from Top, Left, Width and Height for Corona Objects

Good day
I just started using Corona and I'm kinda confused with this x and y properties. Is it possible to perhaps get the x and y values using Top, Left, Width and Height properties if these are provided? For example, I want an an object to be at Left=10, Top=0, Width=40 and Height=40. Can someone please advise how I can do this, this could be for images, text, textfield etc
Of course. There are several methods to do this.
Example 1:
local myImage = display.newImageRect( "homeBg.png", 40, 40)
myImage.anchorX = 0; myImage.anchorY = 0
myImage.x = 10 -- Left gap
myImage.y = 0 -- Top gap
localGroup:insert(myImage)
Here, setting the anchor points to (0,0) will make the reference point of your images' geometric center to its top left corner.
Example 2:
local myImage = display.newImageRect( "homeBg.png", 40, 40)
myImage.x = (myImage.contentWidth/2) + 10
myImage.y = (myImage.contentHeight/2)
localGroup:insert(myImage)
Here, the center-X position of your image is calculated by adding Left gap to the image's half width itself. And the center-Y position is calculated by adding Top gap to the image's half height
You can position the objects with any of such methods. If you are a beginner in corona, then the following topics will be useful for you to get more knowledge about Displaying Objects with specific size, position, etc.
Corona SDK : display.newImageRect()
Tutorial: Anchor Points in Graphics 2.0
Corona uses like a Cartesian Coordinate System But the (0,0) is on the TOP LEFT you can view more here:
https://docs.coronalabs.com/guide/graphics/group.html#coordinates
BUT: You can get the screen corners based on the image width and height using this codes:
NOTE THAT YOU SHOULD YOU SHOULD CHANGE IT WITH YOUR IMAGE
local image = display.newImageRect(“images/yourImage.png”, width, height)
--TOP:
image.y = math.floor(display.screenOriginY + image*0.5)
--BOTTOM:
image.y = math.floor(screenH - display.screenOriginY) - image.height*.5
--LEFT:
image.x = (screenOffsetW*.5) + image*.5
--RIGHT:
image.x = math.floor(screenW - screenOffsetW*.5) - image.width*.5
Corona SDK display objects have attributes that can be read or set:
X = myObject.x -- gets the current center (by default) of myObject
width = myObject.width
You can set these values too....
myObject.x = 100 -- centers the object at 100px left of the content area.
By default Corona SDK's are based on their center, unless you change it's anchor point:
myObject.anchorX = 0
myObject.anchorY = 0
myObject.x = 100
myObject.y = 100
by setting the anchor's to 0, then .x and .y refer to the top left of the object.

How can I make an image fall down randomly as if meteors in Corona SDK?

What I want is that the images fall from the top side of the screen and start falling down accelerating, they would only be falling straight down, alternating positions around the width of the screen, meaning one on the right, then another one in the middle, and then another one on the left side and so on in the different positions, until they disappear at the bottom of the screen.
I tried this
function moveMeteors()
for i = 1, math.random(1, 2) do
meteors = display.newImage("meteor.png")
screenGroup:insert(meteors)
meteors.x = (math.random(display.contentWidth))
meteors.y = centerY - 340
transition.to(meteors, {time = math.random(3500 - speedMeteor, 4500 - speedMeteor),
y = 1000 + speedMeteor, onComplete = clear })
speedMeteor = speedMeteor + 10
end
end
But, sometimes the images appear one over the other and I do not want that, I mean, each image appear and go from the top to the bottom of the screen in his own line. I hope that I've explained this well.
You should look into utilizing the built in physics of Coronasdk. CoronaDocs:Physics.
As an example this code should easily simulate the effect you a trying to get, you will have to add functions to take care of removing objects as they leave the screen etc.
local physics = require("physics")
physics.start()
function SpawnMeteor()
local meteor = display.newImage( "meteor.png", math.random(display.contentWidth), centerY - 340)
physics.addBody( meteor)
end
timer.performWithDelay( 2000, SpawnMeteor)

Corona SDK: How do I get an object to float from the bottom of a screen to the top?

I'm pretty new to the sdk so forgive me. I want an object to float/transition from the bottom of the screen to the top and keep going until it is out of the device. How do I do that without hard coding the values since all screens have different heights?
First place your object on the bottom of the screen:
object.y = (display.contentHeight + display.screenOriginY * -2) + object.contentHeight * 0.5
//if starting outside of the screen
object.y = (display.contentHeight + display.screenOriginY * -2) - object.contentHeight * 0.5
//if starting at the bottom of the screen
then perform transition.to
transition.to(object, { time = 500, y = 0 - display.screenOriginY })
I wrote it from my memory, so it may not work by copy + paste, but idea stays the same.
object - this is your object you want to transform
display.screenOriginY - this is the distance from the top of the actual screen to the top of the content area (more info here: https://docs.coronalabs.com/api/library/display/screenOriginY.html )
You may also need to read about transitions: http://docs.coronalabs.com/api/library/transition/to.html

Resources