I just tried this code :
-- requires local composer = require( "composer" ) local scene = composer.newScene() local physics = require("physics") physics.start() physics.setGravity(0,9.8) -- background function scene:create(event) local screenGroup = self.view brick = display.newImage("graphics/stones.png") brick.x = 260 brick.y = 210 brick:scale( 0.4, 0.5 ) screenGroup:insert(brick) display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) end function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end local timeLimit = 100 local currentTime = 0 local timerUpTimer local gradient = { type="gradient", color1={ 0, 2, 2 }, color2={ 0.1, 1.8, 0.3 }, direction="up" } currentTimeText = display.newText(currentTime, 160, 20, native.systemFontBold, 20) currentTimeText:setFillColor( gradient ) local function timerUp() currentTime = currentTime + 1 currentTimeText.text = currentTime if currentTime \>= timeLimit then display.remove(currentTimeText) timer.cancel(timerUpTimer) print("Good job beating the game !") end end timerUpTimer = timer.performWithDelay(1000, timerUp, 0) timerr = timer.performWithDelay(1000,timerDown,timeLimit) local bg1 = display.newImageRect("background2.jpg", xWidth, yHeight) bg1.x = xmid bg1.y = ymid local bg2 = display.newImageRect("background32.jpg", xWidth, yHeight) bg2.x = xmid bg2.y = ymid - display.contentHeight scroll = 2 local function bgScroll (event) bg1.y = bg1.y + scroll bg2.y = bg2.y + scroll if bg1.y == display.contentHeight \* 1.5 then bg1.y = display.contentHeight \* -.5 end if bg2.y == display.contentHeight \* 1.5 then bg2.y = display.contentHeight \* -.5 end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") Runtime:addEventListener("enterFrame", bgScroll) elseif ( phase == "did" ) then print("show showing objects") end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") elseif ( phase == "did" ) then print("hide moving objects") Runtime:removeEventListener("enterFrame", tmp) end end function scene:destroy(event) local sceneGroup = self.view end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene
and I’m getting this error :
game.lua:82: display.newImageRect() bad argument #2: width expected, but got nil
line 82 :
local bg1 = display.newImageRect("background2.jpg", xWidth, yHeight)