I started learning Corona yesterday so sorry for my ignorance. Can someone please explain why it is "expecting table" on line 108 (physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )). All I want it to do is take the image from the object sheet and display it.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
-- Configure image sheet
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 112,
height = 142
},
{
x = 0,
y = 142,
width = 112,
height = 284
},
{
x = 0,
y = 284,
width = 112,
height = 568
},
{
x = 0,
y = 568,
width = 112,
height = 710
},
{
x = 0,
y = 710,
width = 112,
height = 852
},
{
x = 0,
y = 852,
width = 112,
height = 994
},
{
x = 0,
y = 994,
width = 112,
height = 1136
},
{
x = 0,
y = 1136,
width = 112,
height = 1278
},
{
x = 0,
y = 1278,
width = 112,
height = 1420
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
local tapCount = 0
local platform
local balloon
local balloon2
local balloon3
local tapText
local function createBalloon()
newBalloon = display.newImageRect( mainGroup, objectSheet, 1, 112, 142 )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )
newBalloon.myName = "Bigballoon"
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 30
newBalloon.y = math.random( display.contentHeight )
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 60
newBalloon.y = math.random( display.contentHeight )
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 90
newBalloon.y = math.random( display.contentHeight )
end
end
local function pushBalloon()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon.gravityScale = 10
balloon:applyLinearImpulse( 0.1, 0, balloon.x, balloon.y )
end
local function pushBalloon2()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon2.gravityScale = 10
balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end
local function pushBalloon3()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon3.gravityScale = 10
balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local platform = display.newImageRect( "platform.png", 300, 50 )
platform.x = display.contentCenterX
platform.y = display.contentHeight-25
balloon = display.newImageRect( "balloon.png", 112, 112 )
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.8
balloon2 = display.newImageRect( "balloon.png", 112, 112 )
balloon2.x = display.contentCenterX-100
balloon2.y = display.contentCenterY
balloon2.alpha = 0.8
balloon3 = display.newImageRect( "balloon.png", 112, 112 )
balloon3.x = display.contentCenterX+100
balloon3.y = display.contentCenterY
balloon3.alpha = 0.8
createBalloon()
tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )
tapText:setFillColor( 0, 0, 0 )
physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon2, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon3, "dynamic", { radius=50, bounce=0.6 } )
balloon:addEventListener( "tap", pushBalloon )
balloon.gravityScale = 0
balloon:setLinearVelocity( 0, -10 )
balloon2:addEventListener( "tap", pushBalloon2 )
balloon2.gravityScale = 0
balloon2:setLinearVelocity( 0, -10 )
balloon3:addEventListener( "tap", pushBalloon3 )
balloon3.gravityScale = 0
balloon3:setLinearVelocity( 0, -10 )
end
-- show()
function scene:show( event )
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
physics.start()
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
physics.pause()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
You should declare newBalloon as local when you initialize it in createBalloon(). Also, mainGroup == nil when you pass it as the parent group to display.newImageRect(). For these reasons, newBalloon is probably nil when you pass it to physics.addBody() instead of being a DisplayObject, which is what that method requires as its first argument.
For safety, in createBalloon(), you could do this:
local newBalloon = display.newImageRect( ... )
if newBalloon then
physics.addBody( newBalloon, ... )
else
print("WARNING: newBalloon is nil in createBalloon")
end
By the way, if you want all the balloons to be in the same GroupObject, you could add this:
local balloonGroup -- forward declaration
...
local function createBalloon()
local newBalloon = display.newImageRect( balloonGroup, ... )
...
end
local scene:create()
...
balloonGroup = display.newGroup()
sceneGroup:insert( balloonGroup )
balloonGroup:toFront()
...
balloonGroup:insert( balloon )
balloonGroup:insert( balloon2 )
...
end
Related
I want the balloons in this scene, which are DisplayObjects, to disappear when the scene ends. I have added them to the local sceneGroup = scene.view by passing this as the first argument to display.newImageRect() and assumed this would be enough for them to be removed as they are listed in the destroy.scene bit at the bottom. However when it transitions to the next scene they are still there. Why are these DisplayObjects not being removed?
local composer = require( "composer" )
local scene = composer.newScene()
local Balloons
local positioninsheetOptions = 100
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*2,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*3,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*4,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*5,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*6,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*7,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*8,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*9,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*10,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*11,
width = 71,
height = 100
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
Balloons = {}
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local function gotoGame()
composer.gotoScene( "game" )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local balloonBlue1 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local balloonYellow1 = display.newImageRect(sceneGroup, objectSheet, 8, 71, 100)
local balloonRed1 = display.newImageRect(sceneGroup, objectSheet, 10, 71, 100)
local balloonBlue2 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local Balloons = display.newGroup()
Balloons:insert( balloonBlue1 )
Balloons:insert( balloonYellow1 )
Balloons:insert( balloonRed1 )
Balloons:insert( balloonBlue2 )
Balloons:addEventListener( "tap", gotoGame )
balloonBlue1.x = (display.contentWidth/8)
balloonBlue1.y = balloonBlue1.height
balloonYellow1.x = (display.contentWidth/8)*3
balloonYellow1.y = balloonBlue1.height
balloonRed1.x = (display.contentWidth/8)*5
balloonRed1.y = balloonBlue1.height
balloonBlue2.x = (display.contentWidth/8)*7
balloonBlue2.y = balloonBlue1.height
end
-- show()
function scene:show( event )
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
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
You need to add the Balloon group (of which the balloons are children) to your sceneGroup.
In scene:destroy, make sure you remove any listeners and cancel any transitions on the things you want destroyed. If you have, when the scene's view is destroyed, the Balloon group and all it's children will also be destroyed.
Can someone please explain what is going wrong here? It went wrong as soon as I added the AddEventListener
newBalloon:addEventListener( "tap", pushBalloon )
Complete Code:
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
-- Configure image sheet
local positioninsheetOptions = 144.1
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*2,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*3,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*4,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*5,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*6,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*7,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*8,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*9,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*10,
width = 112,
height = 142
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
local tapCount = 0
local platform
local tapText
local balloonsTable = {}
local leftBorder
local rightBorder
local backGroup
local mainGroup
local uiGroup
local platform
local platform2
local function createBalloon()
local randomBalloon = math.random( 10 )
local newBalloon = display.newImageRect( objectSheet, randomBalloon, 112, 142 )
if newBalloon then
table.insert( balloonsTable, newBalloon )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0 } )
newBalloon.myName = "bigBalloon"
newBalloon.alpha = 0.75
newBalloon.gravityScale = randomBalloon/-150
newBalloon:addEventListener( "tap", pushBalloon )
end
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 100
newBalloon.y = display.contentHeight+300
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 160
newBalloon.y = display.contentHeight+300
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 220
newBalloon.y = display.contentHeight+300
end
end
local function gameLoop()
-- Create new balloon
createBalloon()
-- Remove balloons which have drifted off screen
for i = #balloonsTable, 1, -1 do
local thisBalloon = balloonsTable[i]
if ( thisBalloon.x < -100 or
thisBalloon.x > display.contentWidth + 100 or
thisBalloon.y < -100 )
then
display.remove( thisBalloon )
table.remove( balloonsTable, i )
end
end
end
local function pushBalloon( event )
local tappedBalloon = event.target
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
if event.phase == "began" then
tappedBalloon.gravityScale = 10
end
end
local function pushBalloon2()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon2.gravityScale = 10
balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end
local function pushBalloon3()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon3.gravityScale = 10
balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
platform = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform.x = -5
platform.y = 0
physics.addBody( platform, "static", { friction=0.5, bounce=0.3 } )
platform2 = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform2.x = display.contentWidth+5
platform2.y = 0
physics.addBody( platform2, "static", { friction=0.5, bounce=0.3 } )
createBalloon()
end
-- show()
function scene:show( event )
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
gameLoopTimer = timer.performWithDelay( 1250, gameLoop, 0 )
-- Code here runs when the scene is entirely on screen
physics.start()
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
physics.pause()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
Thank you.
You refer to function (pushBalloon) do not exist yet when add event listener. So put definition of function above line you add event listener like that
local function pushBalloon( event )
local tappedBalloon = event.target
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
if event.phase == "began" then
tappedBalloon.gravityScale = 10
end
end
...
newBalloon:addEventListener( "tap", pushBalloon )
At the point in the file where you are adding the listener, pushBalloon() is not defined. You should add a forward declaration earlier in the file:
local scene = ...
local pushBalloon
...
-- createBalloon() and other existing code
...
pushBalloon = function ( event )
-- your function code
end
i have a water image that kills the user sprite on collision, taking 1 from the lives variable and re-spawns the sprite to the start. i have another image of a raft that constantly spawn new images that move across the screen. im trying to get it so the user can move onto the raft, that is over the water image, and not get re-spawned back to the start.
--sets the function for the death sequence.
function waterCollide(event)
frog.x = display.contentWidth/2
frog.y = 504
isOnRaft = 0
lives = lives - 1
showlives.text = "Lives: "..lives,230,-36,native.systemFont,25
lose()
end
--sets water
water = display.newRect(0,0,320,150)
water.x = display.contentWidth*0.5
water.y = 144
water.alpha = 0
physics.addBody(water,"static", {isSensor = true})
water:addEventListener("collision", function ()timer.performWithDelay(50,waterCollide)end)
--Set log position and movement
local mRandom = math.random
local raft = {"Raft1" ,"Raft2"}
local objectTag = 0
local object = {}
function spawnlogleft()
objectTag = objectTag + 1
local objIdx = mRandom(#raft)
local objName = raft[objIdx]
object[objectTag] = display.newImage(objName..".png")
object[objectTag].x = -96
object[objectTag].y = 216
object[objectTag].name = objectTag
transition.to(object[objectTag], {time = 10000, x = 416, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
physics.addBody( object[objectTag], "static",{isSensor = true})
end
spawnlogleft()
timer.performWithDelay(3500,spawnlogleft,0)
--user sprite details
frog = display.newSprite(frogSheet, sequenceData)
frog.x = display.contentWidth/2
frog.y = 504
physics.addBody( frog, "dynamic", physicsData:get("FrogSheetData"))
frog.isFixedRotation = true
the user sprite is an animation of three frames and a time of 300. any help would be appreciated. and i will provide more details if needed.
thank you
Corona has these neat things called CollisionFilters. They show you how to use them in /Physics/CollisionFilter.
You basically set bits to make certain objects un-collideable with other objects. Check this main.lua example out:
local physics = require("physics")
physics.start()
physics.setScale( 60 )
display.setStatusBar( display.HiddenStatusBar )
local bkg = display.newImage( "stripes.png" )
borderCollisionFilter = { categoryBits = 1, maskBits = 6 } -- collides with (4 & 2) only
borderBodyElement = { friction=0.4, bounce=0.8, filter=borderCollisionFilter }
local borderTop = display.newRect( 0, 0, 320, 1 )
borderTop:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderTop, "static", borderBodyElement )
local borderBottom = display.newRect( 0, 479, 320, 1 )
borderBottom:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderBottom, "static", borderBodyElement )
local borderLeft = display.newRect( 0, 1, 1, 480 )
borderLeft:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderLeft, "static", borderBodyElement )
local borderRight = display.newRect( 319, 1, 1, 480 )
borderRight:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderRight, "static", borderBodyElement )
local red = {}
local blue = {}
local redCollisionFilter = { categoryBits = 2, maskBits = 3 } -- collides with (2 & 1) only
local blueCollisionFilter = { categoryBits = 4, maskBits = 5 } -- collides with (4 & 1) only
local redBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=redCollisionFilter }
local blueBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=blueCollisionFilter }
for i = 1,4 do
red[i] = display.newImage( "red_balloon.png", (80*i)-60, 50 + math.random(20) )
physics.addBody( red[i], redBody )
red[i].isFixedRotation = true
blue[i] = display.newImage( "blue_balloon.png", (80*i)-60, 250 + math.random(20) )
physics.addBody( blue[i], blueBody )
blue[i].isFixedRotation = true
end
I want to change the bullet angle after collision, it should change the direction in which it is moving after collision, and must stop rotating after collision.... please give any suggestions... thanks
Sample code is written below
local physics = require("physics")
physics.start()
local obj
physics.setScale( 60 )
physics.setGravity( 0, 0 )
display.setStatusBar( display.HiddenStatusBar )
local obstacle = display.newCircle( 250, 250, 60 )
obstacle.x = display.contentWidth/2; obstacle.y = 200
physics.addBody( obstacle, { density=150, friction=0.2, bounce=0} )
obstacle.isBullet=true
obj = display.newRect(0, 0,20,40)
obj.x = display.contentWidth/2; obj.y = 780
obj.isBullet = true
obj.color = "white"
physics.addBody( obj, { density=1, friction=0.4, bounce=0.1} )
local target = display.newCircle( 250, 250, 60 )
target.x = obj.x; target.y = obj.y; target.alpha = 0
local function Shot( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( "enterFrame", startRotation )
local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if "moved" == phase then
if ( myLine ) then
myLine.parent:remove( myLine ) -- erase previous line, if any
end
myLine = display.newLine( t.x,t.y, event.x,event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
local stopRotation = function()
Runtime:removeEventListener( "enterFrame", startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )
if ( myLine ) then
myLine.parent:remove( myLine )
end
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )
end
end
return true
end
obj:addEventListener( "touch", Shot)
local physics = require "physics"
physics.start()
local rect1 = display.newRect(480,0,10,320)
physics.addBody(rect1, "static")
local rect2 = display.newRect(20,100,20,20)
local function shot()
physics.addBody(rect2, "dynamic")
rect2:applyForce( 1000, 0, rect2.x, rect2.y )
rect2.myName = "rect2"
end
rect2:addEventListener("touch", shot)
I'm bundling storyboard and i want user to move to next scene after filling fields and then press a button
I have a problem with the btn ,---------------------------------------------------------------------------------
-- scence2.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- BEGINNING OF YOUR IMPLEMENTATION
local widget = require( "widget" )
require("hijacks")
local sysFonts = native.getFontNames()
local tHeight -- forward reference
local roundedRect = display.newRoundedRect( 40, 120, 200, 40, 8 )
roundedRect:setFillColor( 0, 0, 0, 170 )
local t = display.newText( "Waiting for button event...", 0, 0, "AmericanTypewriter-Bold", 18 )
t.x, t.y = display.contentCenterX, 70
local function fieldHandler( textField )
return function( event )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
-- In some cases you may want to adjust the interface when the keyboard appears.
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( "editing" == event.phase ) then
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
print( textField().text )
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
end
-- Predefine local objects for use later
local nameField, phoneField
local fields = display.newGroup()
-- Note: currently this feature works in device builds or Xcode simulator builds only (also works on Corona Mac Simulator)
local isAndroid = "Android" == system.getInfo("platformName")
local inputFontSize = 18
local inputFontHeight = 30
tHeight = 30
if isAndroid then
-- Android text fields have more chrome. It's either make them bigger, or make the font smaller.
-- We'll do both
inputFontSize = 14
inputFontHeight = 42
tHeight = 40
end
nameField = native.newTextField( 40, 120, 200, tHeight )
nameField.font = native.newFont( native.systemFontBold, inputFontSize )
nameField:addEventListener( "userInput", fieldHandler( function() return nameField end ) )
phoneField = native.newTextField( 40, 160, 200, tHeight )
phoneField.font = native.newFont( native.systemFontBold, inputFontSize )
phoneField.inputType = "phone"
phoneField:addEventListener( "userInput", fieldHandler( function() return phoneField end ) )
-- Add fields to our new group
fields:insert(nameField)
fields:insert(phoneField)
-- * Add field labels *
local defaultLabel = display.newText( "الاسم", 250, 120, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
local defaultLabel = display.newText( "رقم الجوال", 250, 160, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
-- These are the functions triggered by the buttons
local button1Press = function( event )
t.text = "Button 1 pressed"
end
local button1Release = function( event )
t.text = "Button 1 released"
end
local buttonHandler = function( event )
t.text = "id = " .. event.target.id .. ", phase = " .. event.phase
end
-- This button has individual press and release functions
-- (The label font defaults to native.systemFontBold if no font is specified)
local button1 = widget.newButton{
default = "buttonRed.png",
over = "buttonRedOver.png",
onPress = button1Press,
onRelease = button1Release,
label = "موافق",
emboss = true
}
local button2 = widget.newButton{
default = "buttonBlue.png",
over = "buttonBlue.png",
onEvent = buttonHandler,
onPress = button1Press,
onRelease = button1Release,
label = "ok",
emboss = true;
labelColor = { default = { 51, 51, 51, 255 } },
fontSize = 32,
}
button1.x = 160; button1.y = 320
-- These are the functions triggered by the buttons
local button1Press = function( event )
nameField.text = "Button 1 pressed"
nameField.text = "hello"
storyboard.gotoScene( true, "scene3", "fade", 800 )
end
-- local button1Release = function( event )
-- nameField.text = "Button 1 released"
-- end
local funcction buttonHandler = function( event )
-- nameField.text = "hello"
-- storyboard.gotoScene( true, "scene3", "fade", 800 )
local background = display.newImage("buttonBlue.png", true) -- flag overrides large image downscaling
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
end
local image, text1, text2, text3
local function onSceneTouch( self, event )
if event.phase == "began" then
-- first argument means show native activity indicator while transitioning
--storyboard.gotoScene( true, "scene3", "fade", 800 )
return true
end
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
image = display.newImage( "bg2.jpg" )
screenGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( "Scene 2", 0, 0, native.systemFontBold, 24 )
text1:setTextColor( 255 )
text1:setReferencePoint( display.CenterReferencePoint )
text1.x, text1.y = display.contentWidth * 0.5, 270
screenGroup:insert( text1 )
text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 )
text2:setTextColor( 255 )
text2:setReferencePoint( display.CenterReferencePoint )
text2.x, text2.y = display.contentWidth * 0.5, display.contentHeight * 0.5
screenGroup:insert( text2 )
text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 )
text3:setTextColor( 255 ); text3.isVisible = false
text3:setReferencePoint( display.CenterReferencePoint )
text3.x, text3.y = display.contentWidth * 0.5, display.contentHeight - 100
screenGroup:insert( text3 )
print( "\n2: createScene event" )
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( "2: enterScene event" )
-- remove previous scene's view
storyboard.purgeScene( "scene1" )
-- Update Lua memory text display
local showMem = function()
image:addEventListener( "touch", image )
text3.isVisible = true
text2.text = text2.text .. collectgarbage("count")/1000 .. "MB"
text2.x = display.contentWidth * 0.5
end
local memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
-- Called when scene is about to move offscreen:
function scene:exitScene()
print( "2: exitScene event" )
-- remove touch listener for image
image:removeEventListener( "touch", image )
-- reset label text
text2.text = "MemUsage: "
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
print( "((destroying scene 2's view))" )
end
-- END OF YOUR IMPLEMENTATION
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
return scene it is not move to next scene..
the action works fine, it show testing message..
enter code here