Can't Move Group backwards! Corona SDK - android

i'm new here, i'm trying to do a videogame for android with corona sdk, but i'm stuck with the creation of random particle on the background,
here is the code:
local menu = composer.newScene()
function menu:create( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
blackLayer = display.newImageRect( "black_layer.png", 1280, 300 )
blackLayer.y = display.contentCenterY + 150
blackLayer.x = display.contentCenterX
blackLayer.alpha = 0.6
background = display.newImageRect( "background.png", 1280 , 720 )
background.x = display.contentCenterX
background.y = display.contentCenterY
playBtn = widget.newButton( {
id="playBtn",
x=display.contentCenterX+350,
y=blackLayer.y,
labelColor = { default={0.922, 0.938, 0.941}, over={0.9, 0.3, 0.234, 0.6}},
label = "Play Now",
defaultFile = "redBtn.png",
overFile = "whiteBtn.png",
width = 200,
height = 200,
fontSize = 30,
font = "Fonts/Raleway-Bold.ttf",
--onRelease = composer.gotoScene( "game")
} )
settingsBtn = widget.newButton( {
id="playBtn",
x=display.contentCenterX,
y=blackLayer.y-50,
font = "Fonts/Raleway-Bold.ttf",
label = "Settings",
defaultFile = "rectWhiteBtn.png",
overFile = "rectRedBtn.png",
width =250,
height = 100,
fontSize = 30,
labelColor = { default={0.9, 0.3, 0.234} , over={0.922, 0.938, 0.941, 0.6} },
onRelease = gotoSettings
} )
highScoresBtn = widget.newButton( {
id="highScoresBtn",
x=display.contentCenterX,
y=settingsBtn.y + 100,
font = "Fonts/Raleway-Bold.ttf",
label = "High Scores",
defaultFile = "rectWhiteBtn.png",
overFile = "rectRedBtn.png",
width =250,
height = 100,
fontSize = 30,
labelColor = { default={0.9, 0.3, 0.234} , over={0.922, 0.938, 0.941, 0.6} },
--onRelease = composer.gotoScene("highScores")
} )
bg:insert(background)
sceneGroup:insert(blackLayer)
sceneGroup:insert(playBtn)
sceneGroup:insert(settingsBtn)
sceneGroup:insert(highScoresBtn)
function generateParticles()
local xy=math.random(30,70)
local newParticle = display.newImageRect( particlesGroup, objectSheet , math.random(13), xy, xy )
newParticle.x = (math.random(1280)*math.random(88932))%1280
newParticle.y = (math.random(720)*math.random(13546))%720
newParticle.alpha = 0
transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
end
function generateParticlesSmall()
local xy=math.random(5,15)
local newParticle = display.newImageRect( particlesGroup, objectSheet , 1, xy, xy )
newParticle.x = (math.random(1280)*math.random(88932))%1280
newParticle.y = (math.random(720)*math.random(13546))%720
newParticle.alpha = 0
transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
end
end
function menu:show( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
bigPTimer1=timer.performWithDelay( 1, generateParticles, 0 )
-- Start the timer wich call 'generateParticles' for each ms
smallPTimer=timer.performWithDelay( 1, generateParticlesSmall, 0 )
-- Start the timer wich call 'generateParticlesSmall' for each ms
particlesGroup:toBack()
elseif ( phase == "did" ) then
if event.phase=="began"then
end
if event.phase=="ended"then
end
end
end
function menu:hide( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
function menu:destroy( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
end
--Funzioni
local function gotoGame()
composer.gotoScene( "game" )
end
local function gotoHighScores()
composer.gotoScene( "highscores" )
end
function gotoSettings()
reproduceEffet1()
timer.cancel(bigPTimer1)
timer.cancel(smallPTimer)
composer.gotoScene( "settings", options)
end
--funzioni
local function removetotally( particle )
display.remove( particle )
end
function dimParticle( particle )
transition.to( particle, {alpha=0, time=500, onComplete = removetotally, onCompleteParams = particle})
end
--
menu:addEventListener( "create", menu )
menu:addEventListener( "show", menu )
menu:addEventListener( "hide", menu )
menu:addEventListener( "destroy", menu )
--
return menu
All functions are working perfectly, but the problem is that I can't move back the group of particles, between bg and sceneGroup.
Thank for listening me.

In your code you have only 1 display group.
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
You need to create particlesGroup as child in order to move it back:
Declare local variables on the top of the file:
local menu = composer.newScene()
local bg, particlesGroup
Create groups in menu:create
bg = display.newGroup()
particlesGroup = display.newGroup()
local sceneGroup = self.view
sceneGroup:insert(bg)
sceneGroup:insert(particlesGroup) -- here particlesGroup will be on the top
particlesGroup:toBack() --now particlesGroup is under bg

Related

storyboard.removeScene("scene name") is not working

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

scrollview corona sdk - object not show when dragged out from it

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

Corona newScrollView hideScrollBar still displays scroll bar

I declare Scroll View like this:
sv = widget.newScrollView{
top = properties.y,
left = properties.x,
width = properties.moreGamesPanelWidth,
height = properties.height,
scrollWidth = properties.moreGamesPanelWidth,
scrollHeight = lg.contentHeight,
hideBackground = true,
isBounceEnabled = false,
horizontalScrollDisabled = true,
isLocked = lock,
listener = widgetTouchListener,
hideScrollBar = true,
}
Unfortunately scroll bar is still visible, no matter what I set in "hideScrollBar".
Is there any possibility to hide the scroll bar?
I found an issue!
It happens when you declare two or more scrollViews. The last of them overrides the "hideScrollBar" setting. So there is no option to have two scrollViews, one with and one without scrollBar.
Here is the code which proves the issue
properties.lua
local properties = {}
--device dimensions
properties.width = display.contentWidth + display.screenOriginX * -2
properties.height = display.contentHeight + display.screenOriginY * -2
properties.x = display.screenOriginX
properties.y = display.screenOriginY
properties.center = { x = properties.x + properties.width / 2, y = properties.y + properties.height / 2 }
return properties
main.lua
display.setStatusBar( display.HiddenStatusBar )
local properties = require("properties")
local widget = require("widget")
local sceneGroup = display.newGroup()
local lg = display.newGroup()
local lg2 = display.newGroup()
local bg = display.newRect(properties.center.x, properties.center.y, properties.width, properties.height)
bg:setFillColor(0.2, 0.2, 0.1)
sceneGroup:insert(bg)
local menuRect1 = display.newRect(100, 50, 200, 100)
menuRect1:setFillColor(0.8, 0.1, 0.1)
lg:insert(menuRect1)
local menuRect2 = display.newRect(100, 160, 200, 100)
menuRect2:setFillColor(0.1, 0.1, 0.9)
lg:insert(menuRect2)
local menuRect11 = display.newRect(100, 50, 200, 100)
menuRect11:setFillColor(0.8, 0.1, 0.1)
lg2:insert(menuRect11)
local menuRect22 = display.newRect(100, 160, 200, 100)
menuRect22:setFillColor(0.1, 0.1, 0.9)
lg2:insert(menuRect22)
local sv
local sv2
sv = widget.newScrollView{
top = properties.center.y - lg.contentHeight * 0.5 - 100,
left = properties.center.x - lg.contentWidth * 0.5,
width = lg.contentWidth +20,
height = lg.contentHeight -80 ,
scrollHeight = lg.contentHeight,
hideBackground = true,
isBounceEnabled = false,
hideScrollBar = true,
horizontalScrollDisabled = true,
isLocked = false,
}
sv2 = widget.newScrollView{
top = properties.center.y - lg.contentHeight * 0.5 + 100,
left = properties.center.x - lg.contentWidth * 0.5 ,
width = lg.contentWidth +20,
height = lg.contentHeight -80 ,
scrollHeight = lg.contentHeight,
hideBackground = true,
isBounceEnabled = false,
hideScrollBar = false,
horizontalScrollDisabled = true,
isLocked = false,
}
sv:insert(lg)
sv2:insert(lg2)
sceneGroup:insert(sv)
sceneGroup:insert(sv2)
The issue is that corona's documentation is wrong, the property to set scroll bar visibility is not hideScrollBar = false it is autoHideScrollBar = false.
Had me stuck for days!

how to animate a ball in Corona sdk

hi i need help with this code. i am trying to make three ball bounce around by animating it. i i found some code that code be some help in the Corona sdk sample code. i try to change the image from a circle to a image i have in my folder but now luck it wont work. also i am using story board API i really need this thanks i am new to corona sdk.
this is the sample code
this code works for me but i want to add multiple balloons the would bounce around in their own direction and bounce in to each other and change direction. I am kinda stuck on this can someone help thanks :)....................
here the code you ask for sorry for taking so long
local function newBall( params )
local xpos = display.contentWidth*0.5
local ypos = display.contentHeight*0.5
local circle = display.newCircle( xpos, ypos, params.radius );
circle:setFillColor( params.r, params.g, params.b, 255 );
circle.xdir = params.xdir
circle.ydir = params.ydir
circle.xspeed = params.xspeed
circle.yspeed = params.yspeed
circle.radius = params.radius
return circle
end
local params = {
{ radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 },
{ radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 },
{ radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 },
-- newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 }
}
local collection = {}
-- Iterate through params array and add new balls into an array
for _,item in ipairs( params ) do
local ball = newBall( item )
collection[ #collection + 1 ] = ball
end
-- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX
function collection:enterFrame( event )
for _,ball in ipairs( collection ) do
local dx = ( ball.xspeed * ball.xdir );
local dy = ( ball.yspeed * ball.ydir );
local xNew, yNew = ball.x + dx, ball.y + dy
local radius = ball.radius
if ( xNew > screenRight - radius or xNew < screenLeft + radius ) then
ball.xdir = -ball.xdir
end
if ( yNew > screenBottom - radius or yNew < screenTop + radius ) then
ball.ydir = -ball.ydir
end
ball:translate( dx, dy )
end
end
Runtime:addEventListener( "enterFrame", collection );
can someone help me to change the images from circle to my balloon01.png ,balloon02.png and balloon03.png images in my folder. also this is the error i get when i add it to my game that include the story board API
level1.lua 157:attempt to call global "newBall" (a nil value)
i was trying to post an image but because i am new i can't. I have the code that crate the ball in the create scene that's apart of the Corona SDK story board API thanks ...:0 for your help
I've updated the code. Just try replacing all your code with the following code:
local xpos,ypos = {},{}
local xdirection,ydirection = {},{}
local xMultiplier = {2.8,3.0,4.0} -- these arrays should contain the values for each objects
local yMultiplier = {1.0,2.2,5.5} -- these arrays should contain the values for each objects
local totalImages = 3 -- no. of images/object that you need in the scene
local circle = {}
local diameter = {50,30,20} -- these arrays should contain the values for each objects
for i=1,totalImages do
xpos[i] = display.contentWidth*0.5
ypos[i] = display.contentHeight*0.5
xdirection[i] = 1
ydirection[i] = 1
circle[i] = display.newImageRect("balloon0"..i..".png",diameter[i],diameter[i])
circle[i]:setFillColor(255,0,0,255)
end
local function animate(event)
for i=1,totalImages do
xpos[i] = xpos[i] + ( xMultiplier[i] * xdirection[i] )
ypos[i] = ypos[i] + ( yMultiplier[i] * ydirection[i] )
if (xpos[i] > display.contentWidth - 20 or xpos[i] < 20) then
xdirection[i] = xdirection[i] * -1;
end
if (ypos[i] > display.contentHeight - 20 or ypos[i] < 20) then
ydirection[i] = ydirection[i] * -1;
end
circle[i]:translate( xpos[i] - circle[i].x, ypos[i] - circle[i].y)
end
end
Runtime:addEventListener( "enterFrame", animate )
Note: Make sure to place the following image files in the same folder where your main.lua resides:
balloon01.png
balloon02.png
balloon03.png
Keep Coding............ :)

Measure volume on tap of button

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.

Categories

Resources