local \_W = display.contentWidth local \_H = display.contentHeight local physics = require("physics" ) physics.setScale( 60 ) physics.start( ) local score = 1000 local scGraphic local scoreGraphic = function() if scGraphic then scGraphic.isVisible = false end local options = { text = score, x = \_W-50, y = 10, width = 200, height = 32, fontSize = 16, align = "center" } scGraphic = display.newText(options) scGraphic:setFillColor( 1 ) scGraphic.isVisible = true options = nil text = nil end local circle = {} local cir = 1 local floor = display.newRect( \_W\*0.5, \_H-10, \_W, 20 ) floor:setFillColor( 1,0,0 ) physics.addBody( floor, "static", {density=1, friction=1, bounce=0.3 } ) floor.value = 1 local leftWall = display.newRect( 10, \_H\*0.5, 20, \_H ) leftWall:setFillColor( 1,0,0 ) physics.addBody( leftWall, "static", {density=1, friction=1, bounce=0.3 } ) leftWall.value = 2 local rightWall = display.newRect( \_W-10, \_H\*0.5, 20, \_H ) rightWall:setFillColor( 1,0,0 ) physics.addBody( rightWall, "static", {density=1, friction=1, bounce=0.3 } ) rightWall.value = 3 local onCollision = function(self,event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then score = score-100 display.remove( circle[hit] ) circle[hit] = nil else score = score+100 display.remove( circle[hit] ) circle[hit] = nil end end scoreGraphic() end local dragCircle = function( event ) local target = event.target local phase = event.phase if ( event.phase == "began" ) then local parent = target.parent display.getCurrentStage():setFocus( target ) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif ( target.isFocus ) then if ( phase == "moved" ) then target.x = event.x - target.x0 target.y = event.y - target.y0 elseif ( phase == "ended" or phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) target.isFocus = false transition.to( event.target, {time=500, x=event.target.xOrig, y=event.target.yOrig} ) end end return true end local spawnCircles = function() local s = math.random(40,\_W-40) circle[cir] = display.newCircle( s, -100, 20 ) circle[cir]:setFillColor( 0,1,1 ) physics.addBody( circle[cir], "dynamic", {density=.01, friction=0.5, bounce=0.1 } ) circle[cir].gravityScale = 0.1 circle[cir].collision = onCollision circle[cir]:addEventListener( "collision", circle[cir] ) circle[cir]:addEventListener( "touch", dragCircle ) circle[cir].value = cir cir = cir + 1 if cir == 5 then cir = 1 end end floor.collision = onCollision leftWall.collision = onCollision rightWall.collision = onCollision myTimer = timer.performWithDelay( 1000, spawnCircles, -1 ) scoreGraphic()
Ok so that is my code… when i just put it in the main.lua it works perfect… but once i try to put it in a game.lua with the scenes and stuff it just gives me an error in the command center… Heres the code in game.lua
local composer = require( "composer" ) local scene = composer.newScene() local \_W = display.contentWidth local \_H = display.contentHeight local physics = require("physics" ) physics.setScale( 60 ) physics.start( ) function scene:create( event ) local sceneGroup = self.view local score = 1000 local scGraphic local scoreGraphic = function() if scGraphic then scGraphic.isVisible = false end local options = { text = score, x = \_W-50, y = 10, width = 200, height = 32, fontSize = 16, align = "center" } scGraphic = display.newText(options) scGraphic:setFillColor( 1 ) scGraphic.isVisible = true options = nil text = nil end local circle = {} local cir = 1 local floor = display.newRect( \_W\*0.5, \_H-10, \_W, 20 ) floor:setFillColor( 1,0,0 ) physics.addBody( floor, "static", {density=1, friction=1, bounce=0.3 } ) floor.value = 1 local leftWall = display.newRect( 10, \_H\*0.5, 20, \_H ) leftWall:setFillColor( 1,0,0 ) physics.addBody( leftWall, "static", {density=1, friction=1, bounce=0.3 } ) leftWall.value = 2 local rightWall = display.newRect( \_W-10, \_H\*0.5, 20, \_H ) rightWall:setFillColor( 1,0,0 ) physics.addBody( rightWall, "static", {density=1, friction=1, bounce=0.3 } ) rightWall.value = 3 local onCollision = function(self,event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then score = score-100 display.remove( circle[hit] ) circle[hit] = nil else score = score+100 display.remove( circle[hit] ) circle[hit] = nil end end scoreGraphic() end local dragCircle = function( event ) local target = event.target local phase = event.phase if ( event.phase == "began" ) then local parent = target.parent display.getCurrentStage():setFocus( target ) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif ( target.isFocus ) then if ( phase == "moved" ) then target.x = event.x - target.x0 target.y = event.y - target.y0 elseif ( phase == "ended" or phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) target.isFocus = false transition.to( event.target, {time=500, x=event.target.xOrig, y=event.target.yOrig} ) end end return true end local spawnCircles = function() local s = math.random(40,\_W-40) circle[cir] = display.newCircle( s, -100, 10 ) circle[cir]:setFillColor( 0,1,1 ) physics.addBody( circle[cir], "dynamic", {density=.01, friction=0.5, bounce=0.1 } ) circle[cir].gravityScale = 0.1 circle[cir].collision = onCollision circle[cir]:addEventListener( "collision", circle[cir] ) circle[cir]:addEventListener( "touch", dragCircle ) circle[cir].value = cir cir = cir + 1 if cir == 5 then cir = 1 end end floor.collision = onCollision leftWall.collision = onCollision rightWall.collision = onCollision myTimer = timer.performWithDelay( 1000, spawnCircles, -1 ) scoreGraphic() end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene
ya… so when its in game.lua it wont work but when its in main.lua it works perfect… can you help me out? thanks