Hey below is a code that measure volume at runtime. I want a button which when tapped once starts volume as in this code and then when tapped again stops measuring.
local g = display.newGroup()
local function newBar()
local bar = display.newLine( 0, 0, 1, 0 )
bar:setColor( 0, 55, 100, 20 )
bar.width = 7
bar.y=400
bar.x=20
return bar
end
local volumeBar = newBar()
volumeBar.y = 309
volumeBar.x = 320
function volumeBar:enterFrame ( event )
local v = 20*math.log(r:getTunerVolume())
local MINTHRESH = 20
local LEFTMARGIN = 20
local ONE = -1
local v2 = MINTHRESH + math.max (v, -MINTHRESH)
v2 = (display.contentWidth - 20 ) * v2 / MINTHRESH
local neg = math.max ( 20, v2 )
volumeBar.xScale = neg * ONE
if (v >= -4) then
volumeBar:setColor( 110, 110, 20, 200 )
elseif (v < -4) then
volumeBar:setColor( 235, 80, 80, 233)
end
end
Runtime:addEventListener("enterFrame", volumeBar)
g:insert(volumeBar)
Help please cause here there are 2 taps that need to be done.
Add a tap event listener to the button and call Runtime:removeEventListener("enterFrame", volumeBar) in the tap function.
Related
I'm just learning the Corona SDK for coding some useful things for private use on my Xperia Z1 compact with Android 5.1.1
I stumbled across the functions for location and gave it a try (with massive help from the reference and the examples).
Latitude, longitude and accuracy and work just fine. The coordinates are very close to my actual position (about 10 metres) and they are within the given accuracy (31.5 metres). However, Altitude, Speed and Direction aren't quite right.
When I start the application, the altitude is "right" (maybe 50 metres too high) at first, but after a few seconds it switches to zero and stays there. This also happens when I "minimise" the application for a short moment and re-open it again.
The speed and direction stay zero all the time, even when I move around.
Here is "my" (mostly copied) code:
local display = require ("display")
local t0 = display.newText("Latitude", 100, 50, native.systemFont, 16)
local t1 = display.newText("Longitude", 100, 100, native.systemFont, 16)
local t2 = display.newText("Altitude", 100, 150, native.systemFont, 16)
local t3 = display.newText("Accuracy", 100, 200, native.systemFont, 16)
local t4 = display.newText("Speed", 100, 250, native.systemFont, 16)
local t5 = display.newText("Direction", 100, 300, native.systemFont, 16)
local latitude = display.newText( "-", 200, 50, native.systemFont, 16 )
local longitude = display.newText( "-", 200, 100, native.systemFont, 16 )
local altitude = display.newText( "-", 200, 150, native.systemFont, 16 )
local accuracy = display.newText( "-", 200, 200, native.systemFont, 16 )
local speed = display.newText( "-", 200, 250, native.systemFont, 16 )
local direction = display.newText( "-", 200, 300, native.systemFont, 16 )
local locationHandler = function( event )
local latitudeText = string.format( '%.4f', event.latitude )
latitude.text = latitudeText
local longitudeText = string.format( '%.4f', event.longitude )
longitude.text = longitudeText
local altitudeText = string.format( '%.3f', event.altitude )
altitude.text = altitudeText
local accuracyText = string.format( '%.3f', event.accuracy )
accuracy.text = accuracyText
local speedText = string.format( '%.3f', event.speed )
speed.text = speedText
local directionText = string.format( '%.3f', event.direction )
direction.text = directionText
end
Runtime:addEventListener( "location", locationHandler )
THANKS!
It all seems right.
1- Altitude with this type of gps wont be accurate, 50m is good accuracy.
2- You have to add direction listener for direction to work:
local directionHandler1 = function( event )
local magneticText = string.format( '%.1f', event.magnetic )
magnetic.text = magneticText
end
Runtime:addEventListener( "heading", directionHandler1 )
You need all these three:
Runtime:addEventListener( "location", locationHandler1 )
Runtime:addEventListener( "heading", directionHandler1 )
Runtime:addEventListener('accelerometer', processor)
I am new to corona sdk and trying to build sample in it. i want to get back to main screen automatically after some time by destroying my scene(i mean that scene destroys itself after some particular time and user get back to main screen).I know that for some this may sound silly but i beginner to Corona
Here is my code of main.lua :-
local storyboard = require "storyboard"
local widget = require( "widget" )
--/**/ widget.setTheme( "widget_theme_android" )
local json = require "json"
local particleDesigner = require( "particleDesigner" )
local physics = require( "physics" )
physics.start()
-------------------------------------------------------------------------------
local sky = display.newImage( "sky.jpg",contentCenterX,display.contentCenterY)
local emitter
emitter = particleDesigner.newEmitter("air_stars.json")
emitter.x = (contentCenterX)
emitter.y = (contentCenterY)
local button = {}
y= -50
for count = 1,3 do
y = y + 110
x = 20
for insideCount = 1,3 do
x = x + 110
button[count] = display.newImage("1.png")
button[count].x = x
button[count].y = y
local container = display.newContainer( 0, 0 )
container:translate(button[count].x-40, button[count].y-40)
local bkgd = display.newImage( container, "2.png" )
function buttonTap(self)
button[count].touch = transition.to(container,{time=3000, height = button[count].height+x, width = button[count].width+y, onComplete = StartGame})
function StartGame()
storyboard.purgeScene("main")
if count == 1 and insideCount == 1 then
storyboard:gotoScene("bridge")
elseif count == 1 and insideCount == 2 then
storyboard:gotoScene("Heli")
end
end
end
button[count]:addEventListener( "touch", buttonTap)
end
end
return storyboard
And Here is my bridge.lua file :- (http://piratepad.net/ep/pad/view/ro.JR1Bpt1qkA$/latest)
Any help will be appreciated
Thanks
Hi sid here you go:
_W = display.contentWidth;
_H = display.contentHeight;
local button = {}
x = -20
for count = 1,3 do
x = x + 90
y = 20
for insideCount = 1,3 do
y = y + 90
button[count] = display.newImage("imgs/one.png");
button[count].x = x;
button[count].y = y;
local container = display.newContainer( 0, 0 )
container:translate(button[count].x, button[count].y)
local bkgd = display.newImage( container, "imgs/two.png" )
function buttonTap(self)
button[count].touch = transition.to(container,{time=3000, height = button[count].height+x, width = button[count].width+y})
end
button[count]:addEventListener( "touch", buttonTap )
end
end
just add this to the bridge.lua
local function goBack()
storyboard:gotoScene("buttons")
end
timer.performWithDelay( 3000, goBack, 1 )
and this to main.lua
storyboard.purgeOnSceneChange = true
mate, i need help for using scrollview in corona sdk, fyi im new in corona sdk..
im trying to make some simple puzzle game here
so here we go pieces code:
local widget = require("widget")
_W = display.contentWidth -- Get device screen width
_H = display.contentHeight -- Get device screen height
imgPiece = 3
imgPieces = {}
imgPieceStartingY = { 80,230,380 }
imgPieceWidth, imgPieceHeight = 120, 120
-- Get some background image here
bg = display.newImageRect("bg.png", _W, _H)
bg.x = _W/2
bg.y = _H/2
-- Create some scrollview
scrollView = widget.newScrollView{
top=100,
left = _W/2,
height=_H/2,
width = 160,
hideBackground = true,
scrollWidth = 50 ,
scrollHeight = 1000 }
-- Set image inside the scrollview
for i = 1, imgPiece do
imgPieces[i] = display.newImageRect("s"..i..".jpg", imgPieceWidth, imgPieceHeight)
imgPieces[i].x = 85 -- Set Starting X position for img piece
imgPieces[i].y = imgPieceStartingY[i] -- Set Starting Y position for img piece
imgPieces[i].id = i
imgPieces[i].place = "start"
scrollView:insert(imgPieces[i])
end
the problem is, when i try to drag the image out from the scrollview the image will dissapear.
how can i make the image still visible when object is leaving the scrollview boundary box(which i set in 160 width) while user still dragging and hold the image on?
thx
I had the same problem and I solved with the image inserted in the main group in "began" phase.
local function dragElements(event)
local phase = event.phase
local obj = event.target
if phase == "began" then
display.getCurrentStage():setFocus(obj)
grp:insert(obj)
obj.StartMoveX = event.x
obj.StartMoveY = event.y
elseif phase == "moved" then
obj.x = (event.x - event.xStart) + obj.StartMoveX
obj.y = (event.y - event.yStart) + obj.StartMoveY
elseif phase == "ended" or phase == "canceled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
i want my bird to have a up and down movement when it plays here is my code
function updateMons2()
for a = 1, mons2.numChildren, 1 do
physics.addBody(mons2[a],"kinematic")
if(mons2[a].isAlive == true) then
(mons2[a]):translate(speed * -1, 0)
if(mons2[a].x < -80) then
mons2[a].x = 1000
mons2[a].y = 500
mons2[a].isAlive = false
end
end
end
end
this code only does is from right to left i want my bird to move up and down while it is going left can someone help me?
Here is a sample. try this:
local mons2 = {}
local yPos = {}
for i=1,2 do
mons2[i] = display.newImageRect("1.png",50,50)
mons2[i].x = 100
mons2[i].y = 100+(100*(i-1))
mons2[i].isAlive = true
yPos[i] = mons2[i].y
end
speed = 10
count_ = 0
function updateMons2()
count_ = count_ + 1
for a = 1, 2, 1 do
physics.addBody(mons2[a],"kinematic")
if(mons2[a].isAlive == true) then
mons2[a]:translate(speed * -1, 0)
transition.to(mons2[a],{time=50,y=yPos[a]+(20*(count_%2)*-1)})
if(mons2[a].x < -80) then
mons2[a].x = 350
end
end
end
end
timer.performWithDelay(100,updateMons2,-1)
Keep Coding........... :)
I'm having some troubles when I try to reload a scene.
In my program, users are able to draw a line with their finger (it's an iOS/Android app), and it works pretty good, but, when I try to reload the scene, the console returns
"Attempt to index field 'parent' (a nil value)"
at line 78
The code I've used to perform the "drawline" and the reload (replay in this case) features is
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = true
local i = 1
local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end
local function drawLine(e)
if(e.phase == "began") then
for i = #lines, 1, -1 do
if (lines[i]) then
if (lines[i].parent) then
lines[i].parent:remove(lines[i])
end
lines[i] = nil
end
end
lines = {}
line_number = 100
prevX = e.x
prevY = e.y
isDrawing = true
elseif(e.phase == "moved") then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 100) then
if(lines[i]) then lineGroup:remove(i) end
lines[i] = display.newLine(prevX, prevY, e.x, e.y)
lines[i]:setColor(255, 255, 0)
lines[i].width = 3
lines[i].myName = "lines"
if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
if (lines[i].parent) then
lines[i].parent:remove(lines[i])
end
lines[i] = nil
end
end
end
local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], "static",
{ density = 1, friction = 0.5, bounce =
-0.8, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end
elseif(e.phase == "ended") then
isDrawing = true
end
return lineGroup
end
Runtime:addEventListener("touch",drawLine)
where line 78 is:
lineGroup:insert(lines[i])
I restart my game using
local replayBTN = display.newImageRect("images/replay.png", 25, 25)
replayBTN.alpha = 1
replayBTN.x = _W/2
replayBTN.y = _H/2
localGroup:insert( replayBTN)
function replay(event)
director:changeScene("game")
return true
end
replayBTN:addEventListener("touch", replay)
How can I fix my problem? THANKS ;)
the moment you try to go to another scene don't reset all the values because you set it to nil so when you go back to your scene again you get an error and i see your using director class to change scene and go to the same scene have you try using storyboard i don't know if it will be much convenient to you because i'm thinking that your just trying to reset the value of the line storyboard can recreate and reset all the value as you remove the scene and go to another scene. you can visit this link to compare director class and storyboard.
http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/