I have a main scene in the Corona SDK (main.lua) On this scene there is a button, when you click on which there should be a transition to another scene (home.lua). How do I do this? Preferably using composer
Modified version of example from documentation
local composer = require( "composer" )
local scene = composer.newScene()
...
local object = display.newImage( "ball.png" )
local function onObjectTouch( event )
if ( event.phase == "began" ) then
elseif ( event.phase == "ended" ) then
composer.gotoScene( "home" )
end
return true
end
object:addEventListener( "touch", onObjectTouch )
I'm making a side scroller and when i start my game i can touch the screen to keep my penguin in the air, but when i fail and collide with an ice block and press play after going to restart.lua i get the error attempt to call method 'applyForce'
heres my code
local function activatePengs(self,event)
self:applyForce(0, -45, self.x, self.y)
print("run")
end
local function touchScreen(event)
print("touch")
if event.phase == "began" then
peng.enterFrame = activatePengs
Runtime:addEventListener("enterFrame", peng)
end
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", peng)
end
end
local function onCollision(event)
if event.phase == "began" then
print "collide"
composer.gotoScene( "restart",{ time=800, effect="crossFade" } )
end
end
-- now comes four required functions for Composer:
function scene:create( event )
local sceneGroup = self.view
bg1 = display.newImageRect(sceneGroup, "bg.png", 800, 1000)
bg2 = display.newImage(sceneGroup, "ice2.png",140,210)
bg3 = display.newImage(sceneGroup, "ice2.png",540,210)
bg4 = display.newImage(sceneGroup, "ice2.png",940,210)
bg5 = display.newImage(sceneGroup, "ice2.png",1340,210)
bg6 = display.newImage(sceneGroup, "ice2.png",1740,210)
bg7 = display.newImage(sceneGroup, "ice1.png",140,420)
bg8 = display.newImage(sceneGroup, "ice1.png",540,420)
bg9 = display.newImage(sceneGroup, "ice1.png",940,420)
bg10 = display.newImage(sceneGroup, "ice1.png",1340,420)
bg11 = display.newImage(sceneGroup, "ice1.png",1740,420)
peng = display.newImage(sceneGroup, "peng.png", 80, 201)
physics.addBody(peng, "dynamic", {density=.18, bounce=0.1, friction=.5, radius=55})
...
end
Probably the error occurs because penguin is not a physics object when the function is called. I think remove enterFrame listener before go to next scene should solve your problem.
I added some improvements to your code (not tested) :
local applyForceToPenguin = false
local function enterFrame( self, event )
if applyForceToPenguin then
self:applyForce( 0, -45, self.x, self.y )
print("run")
end
end
local function touchScreen(event)
print("touch")
if event.phase == "began" then applyForceToPenguin = true end
if event.phase == "ended" then applyForceToPenguin = false end
end
local function onCollision(event)
if event.phase == "began" then
print "collide"
applyForceToPenguin = false
composer.gotoScene( "restart",{ time=800, effect="crossFade" } )
end
end
-- now comes four required functions for Composer:
function scene:create( event )
local sceneGroup = self.view
bg1 = display.newImageRect( sceneGroup, "bg.png", 800, 1000 )
bg2 = display.newImage( sceneGroup, "ice2.png", 140, 210 )
bg3 = display.newImage( sceneGroup, "ice2.png", 540, 210 )
bg4 = display.newImage( sceneGroup, "ice2.png", 940, 210 )
bg5 = display.newImage( sceneGroup, "ice2.png", 1340, 210 )
bg6 = display.newImage( sceneGroup, "ice2.png", 1740, 210 )
bg7 = display.newImage( sceneGroup, "ice1.png", 140, 420 )
bg8 = display.newImage( sceneGroup, "ice1.png", 540, 420 )
bg9 = display.newImage( sceneGroup, "ice1.png", 940, 420 )
bg10 = display.newImage( sceneGroup, "ice1.png", 1340, 420 )
bg11 = display.newImage( sceneGroup, "ice1.png", 1740, 420 )
peng = display.newImage( sceneGroup, "peng.png", 80, 201)
physics.addBody( peng, "dynamic", { density=.18, bounce=0.1, friction=.5, radius=55 } )
Runtime:addEventListener( "enterFrame", enterFrame )
...
end
function scene:destroy()
Runtime:removeEventListener( "enterFrame", enterFrame )
end
function scene:hide( 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
applyForceToPenguin = false
end
end
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
Have a nice day:)
I'm trying to create a textbox with this code,
local defaultBox
local function textListener( event )
if ( event.phase == "began" ) then
-- User begins editing "defaultBox"
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
-- Output resulting text from "defaultBox"
print( event.target.text )
elseif ( event.phase == "editing" ) then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
-- Create text box
defaultBox = native.newTextBox( 200, 200, 280, 140 )
defaultBox.text = "This is line 1.\nAnd this is line2"
defaultBox.isEditable = true
defaultBox:addEventListener( "userInput", textListener )
but it's not working and telling me this:
attempt to index global 'native' (a nil value)
I think the best answer here is to break down the error.
'attempt to index' means you're treating something like a table, and trying to access a value from it. Example:
t.x
t:x
t[x]
global native is the value you're trying to index. The 'global' part tells you it's not a local variable, and the 'native' part is the actual name of the variable.
Since 'native' is undefined (nil), check your spelling and if you need to require it
local native = require 'native'
I'm having a problem when I try to restart my game. I pause the physics, then when the user clicks the button, the physics are supposed to restart. When I put in physics.start(), though the game crashes. So, I was wondering if it's possible to pause one physical object and then change it's position on the screen. I'm kind of a newb so any answer will be helpful.
local physics = require( "physics")
physics.start( )
local crate1 = display.newRect( display.contentWidth/2,display.contentHeight/2, 40,40)
physics.addBody( crate1, { density=4.0, friction=0.3, bounce=.4} )
crate1.bodyType = "dynamic"
crate1.isBodyActive = true
crate1:setFillColor( 1,0,.3)
sky = display.newRect( display.contentWidth/2, .5, display.contentWidth, 0.000000000000000000001)
physics.addBody( sky, {density=4.0, friction=0.3, bounce=1.5} )
sky.bodyType = "static"
ground = display.newRect( display.contentWidth/2, display.contentHeight, display.contentWidth, .00000000001 )
physics.addBody( ground, {density=4.0, friction=0.3, bounce=1.5 } )
ground.bodyType = "static"
rightWall = display.newRect( display.contentWidth, display.contentHeight/2, 1, display.contentHeight )
physics.addBody( rightWall, {density=4.0, friction=0.3, bounce=1.5} )
rightWall.bodyType = "static"
leftWall = display.newRect(1, display.contentHeight/2, .0000000001, display.contentHeight )
physics.addBody( leftWall, {density=4.0, friction=0.3, bounce=1.5} )
leftWall.bodyType = "static"
physics.setGravity( 0, 3 )
local function handleCollisionOnDelay( event )
local obj = event.source.object
obj.isBodyActive = false
end
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
local dr = timer.performWithDelay( 10, handleCollisionOnDelay )
dr.object = self
crate1.x = display.contentWidth/2
crate1.y = display.contentHeight/2
end
end
crate1.collision = onLocalCollision
crate1:addEventListener( "collision", crate1 )
use isBodyActive property
http://docs.coronalabs.com/api/type/Body/isBodyActive.html
good luck
I have this big issue, and im so pissed off because of this. I have no idea anymore how to make a function that handles touching more than one object. To introduce what i have and what should it do.
I have 3 objects, two buttons: LEFT and RIGHT. and SHOOTING button. I need to be able to shoot while holding the rotating button. I have this (code below) and it does work but if i touch shooting button it stops rotating.
local isShooting = false
function resetShooting()
isShooting = false
end
function holdingLeft()
if isPressed == true then
if rotationOfship > 0 then
if touchedXTimes < 10 then
rotationOfship = rotationOfship-2
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 9 then
rotationOfship = rotationOfship-5
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 29 then
rotationOfship = rotationOfship-8
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
end
else
rotationOfship = 360
end
elseif isPressed == false then
timer.cancel(tmr_hold)
end
end
function holdingRight()
if isPressed == true then
if rotationOfship < 360 then
if touchedXTimes < 10 then
rotationOfship = rotationOfship+2
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 9 then
rotationOfship = rotationOfship+5
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 29 then
rotationOfship = rotationOfship+8
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
end
print(touchedXTimes)
else
rotationOfship = 0
end
elseif isPressed == false then
timer.cancel(tmr_hold)
end
end
function onTouch(event)
if event.target.name == "left" then
if event.phase == "began" then
isPressed = true
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
if tmr_hold ~= nil then timer.cancel(tmr_hold) end
tmr_hold = timer.performWithDelay( 8, holdingLeft, 0)
elseif event.target.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
isPressed = false
timer.cancel(tmr_hold)
touchedXTimes = 0
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
if event.target.name == "right" then
if event.phase == "began" then
isPressed = true
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
if tmr_hold ~= nil then timer.cancel(tmr_hold) end
tmr_hold = timer.performWithDelay( 8, holdingRight, 0)
elseif event.target.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
isPressed = false
timer.cancel(tmr_hold)
touchedXTimes = 0
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
if event.target.name == "laser" then
if event.phase == "began" and isShooting == false then
isShooting = true
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
bullet = display.newImageRect("images/laser.png",math.random(5,20),5/2)
bullet.x = halfW
bullet.y = halfH
bullet.name = "bullet"
bullet.rotation = rotationOfship-90
physics.addBody( bullet, "dynamic", { isSensor=true, radius=10} )
group:insert(bullet)
ship:toFront()
audio.play( laserSound, { channel=2, loops=0} )
local newX, newY = pointAtDistance(rotationOfship-90, 400)
bullet:setLinearVelocity( newX/2, newY/2 )
tmr_shoot = timer.performWithDelay( math.random(300,400), resetShooting, 1)
elseif event.target.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
isShooting = false
timer.cancel(tmr_shoot)
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
return true
end
dpad_left_circle:addEventListener( "touch", onTouch )
dpad_right_circle:addEventListener( "touch", onTouch )
laser_button_circle:addEventListener( "touch", onTouch )
I would recommend to split it to three methods instead of one:
dpad_left_circle:addEventListener( "touch", onTouchLeft )
dpad_right_circle:addEventListener( "touch", onTouchRight )
laser_button_circle:addEventListener( "touch", onTouchLaser )
Code will be much more clear and you'll be able to touch different buttons at the same time.