I can take a photo using this code :
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local isXcodeSimulator = "iPhone Simulator" == system.getInfo("model")
if (isXcodeSimulator) then
local alert = native.showAlert( "Information", "Camera API not available on iOS Simulator.", { "OK"})
end
local sessionComplete = function(event)
local image = event.target
if image then
image.x = centerX
image.y = centerY
local w = image.width
local h = image.height
end
end
local listener = function( event )
if media.hasSource( media.Camera ) then
media.capturePhoto( { listener = sessionComplete } )
else
native.showAlert("Corona", "Camera not found.")
end
return true
end
trigger:addEventListener( "tap", listener )
But if the picture is to big it doesn't zoom out or scale down, i have tried local w =display.contentWidth but nothing's happening. How can i scale the photo to the exact screen size, probably need to scale in the Y axis to fill the screen
If you simply wish to scale the picture down there are two ways you can do that.
1: Use scale on the display object (http://docs.coronalabs.com/api/type/DisplayObject/scale.html)
object:scale(xScale, yScale)
or the second solution which I suspect is what you are looking for
2: Set the image size directly.
Set the image width/height using object.width and object.height and setting the respective contentWidth and ContentHeight of your display object.
Change the sessionComplete function to the following
local sessionComplete = function(event)
local image = event.target
if image then
image.x = centerX
image.y = centerY
image.width = display.contentWidth
image.height = display.contentHeight
end
end
Solved the problem by using this equation
image.contentWidth / image.contentHeight * display.contentHeight
local sessionComplete = function(event)
local image = event.target
if image then
imgWidth = image.contentWidth / image.contentHeight * display.contentHeight
image.x = centerX
image.y = centerY
image.width = imgWidth
image.height = display.contentHeight
end
end
Related
I am looking for a bit of help with this lua script I am making. I want to use Quaternion's to rotate an object based on your mouse movement/drag. I am using this library for my Quaternion https://github.com/topameng/CsToLua/ . I have got the object to move but all the rotations are messed up, here is a link to a video of what I have so far http://img.bcdojrp.net/videos/uploads/2021-03-17%2003-58-07_Trim.mp4 I have spent hours looking this over and can't find what to change... any help is appreciated, thanks!
function GetCursor()
local sx, sy = GetActiveScreenResolution()
local cx, cy = GetNuiCursorPosition()
local cx, cy = (cx / sx) + 0.008, (cy / sy) + 0.027
return cx, cy
end
if dragging then
-- this is the screen position
local intx, inty = GetCursor()
local deltaMove = {
['x'] = intx-previousMousePosition.x,
['y'] = inty-previousMousePosition.y
}
local deltaRotationQuaternion = Quaternion.Euler(deltaMove.y * 40, deltaMove.x * 40, 0)
local x,y,z,w = GetEntityQuaternion(curObject)
local quatNew = deltaRotationQuaternion.__mul(Quaternion.New(x,y,z,w), deltaRotationQuaternion)
local valX, valY, valZ, valW = quatNew.x, quatNew.y, quatNew.z, quatNew.w
SetEntityQuaternion(curObject, valX, valY, valZ, valW)
--cube.quaternion.multiplyQuaternions(deltaRotationQuaternion, cube.quaternion);
previousMousePosition.x = intx
previousMousePosition.y = inty
else
previousMousePosition = {
['x'] = 0,
['y'] = 0
}
end
So I tried everything an can't seem to get the image to take up the full screen and be centered. I have attached the image, and how it looks when I run on corona.
I tried this and got what I attached:
local background = display.newImageRect( "bg.png",
display.contentCenterX, display.contentCenterY)
Thanks in advance for the help!
background image
how it looks on corona
example your config.lua is like below
application = {
content = {
width = 640,
height = 960,
scale = "letterBox",
fps = 30,
},
}
then use like below to display image in fullscreen
local background = display.newImageRect( "bg.png",
640 , 960 )
// then center it like below
background.x = display.contentCenterX
background.y = display.contentCenterY
According to documentation you need to pass width and height, not x and y. Try this:
--take screen width and height, so image can take whole screen
local width = display.contentWidth
local height = display.contentHeight
local background = display.newImageRect( "bg.png", width, height)
--setting coordinates to center
background.x = display.contentCenterX
background.y = display.contentCenterY
Edit: If you are using letterbox scaling mode you should ajust width and height
width = display.contentWidth - 2 * display.screenOriginX
height = display.contentHeight - 2 * display.screenOriginY
If you want preserve aspect ratio of image try
local _CX = display.contentCenterX
local _CY = display.contentCenterY
local imgW = 440 -- width of image
local imgH = 623-- height of image
local imgR = imgH / imgW
local screenW = display.contentWidth - 2 * display.screenOriginX
local screenH = display.contentHeight - 2 * display.screenOriginY
local screenR = screenH / screenW
local factor = imgR > screenR and screenW / imgW or screenH / imgH
local background = display.newImageRect( 'bg.jpg', imgW * factor, imgH * factor )
background .x, background .y = _CX, _CY
I tested code against letterbox mode.
Other solution from Corona forum How to make an image full screen?:
if you want to fill the screen with an image, it's better to make the
image larger than any potential screen aspect ratio, then accept some
amount of clipping on certain screens when the image goes beyond the
edges.
Ok, so I'm trying to create hexagons for my game. The first option I had is to have several images of hexagon, but I'm having problems with clickable area since these images are positioned side-by-side.
So i guess my only option is to create objects using polygons. Here is a code from corona sdk's website:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, }
local o = display.newPolygon( halfW, halfH, vertices )
o.fill = { type="image", filename="mountains.png" }
o.strokeWidth = 10
o:setStrokeColor( 1, 0, 0 )
That code is for creating a star. But I don't know how to create a hexagon using vertices.
Try this to create the vertices array:
local R = 45
local N = 6
local vertices = {}
local i = 0
for t = 0, 2*math.pi, 2*math.pi/N do
i=i+1; vertices[i]= R*math.cos(t)
i=i+1; vertices[i]= R*math.sin(t)
end
And this to draw the hexagon:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local hexagon = display.newPolygon( halfW, halfH, vertices )
hexagon.fill = { type="image", filename="mountains.png" }
hexagon.strokeWidth = 10
hexagon:setStrokeColor( 1, 0, 0 )
I chose R=45 to produce a polygon of the same size of your star.
You could always use a graphics.newMask() to apply a mask to each image hex that would make the outside area not touchable.
I am totally new to programming. I've tested how to script an other things like that... but I tried to use animations in my "game". I used the "AnAl" library. All worked good. But then where I liked to use "moving" (or how it's called ;P) the animations doesn't worked and the character rotated. I don't know what I need to do...
And I used the Lua language, btw.
require ("AnAl")
function love.load()
-- Shortcuts
lg = love.graphics
lkid = love.keyboard.isDown
local img = lg.newImage ("img.png")
anim = newAnimation(img, 100, 100, 0.1,5,0)
image = {
x = 250,
y = 150,
rotation = math.rad (0),
moveSpeed = 200
}
end
function love.draw()
anim:draw(figur, image.x, image.y, image.rotaion, 0.5, 0.5)
end
function love.update(dt)
if lkid("w") then image.y = image.y - image.moveSpeed * dt end
if lkid("s") then image.y = image.y + image.moveSpeed * dt end
if lkid("a") then image.x = image.x - image.moveSpeed * dt end
if lkid("d") then image.x = image.x + image.moveSpeed * dt end
anim:update(dt)
end
I have no idea what figur refers to in your code.
The arguments to anim:draw should be x, y, rotation, scalex, scaley. Since you have added figur before the arguments for some reason, you are setting the rotation to be the y position.
anim:draw(image.x, image.y, image.rotation, 0.5, 0.5)
How to get newWebView to zoom to fit screen for difference devices?
So I am create some simple HTML pages with a table, saving this to file (with the below meta tag) and then. I've discovered the meta tag but even with the below, whilst it works well with iPhone 5, with iPad it only take up 1/2 the width of the screen?
<head>
<meta name = "viewport" content = "width = device-width">
</head>
Not sure if this is relevant however my config.lua has:
local aspectRatio = display.pixelHeight / display.pixelWidth
local width = aspectRatio > 1.5 and 320 or math.ceil( 480 / aspectRatio )
local height = aspectRatio < 1.5 and 480 or math.ceil( 320 * aspectRatio )
application = {
content = {
width = width,
height = height,
scale = "letterBox",
fps = 60,
imageSuffix = {
["#2x"] = 1.5,
["#3x"] = 3.0,
},
},
}
EDIT: Setup code:
local displayW, displayH = display.contentWidth, display.contentHeight
local webViewHeight = displayH - navBar.height
webView = native.newWebView(
displayW/2, navBar.height + webViewHeight/2,
displayW, webViewHeight
)
scene.view:insert(webView)
I also use the same code in your config.lua and to make it fit here is what I use for my webviews:
local _H = display.contentHeight
local _W = display.contentWidth
local SBH = display.topStatusBarContentHeight -- Status bar height
webView = native.newWebView( 0, SBH, _W, _H-SBH )
webView.anchorX = 0
webView.anchorY = 0
webView:request( "file.html", system.DocumentDirectory ) -- make sure you choose the right location of your file
The API:
native.newWebView( centerX, centerY, width, height )
As you can see from my code, I have set the anchor point to TopLeft because it's easy to position it that way. Then, width takes the whole screen and the height is the contentHeight minus the status bar height.
This solution will work on all devices, iOS & Android.