How to endlessly go up and place brick images randomly along the edges of the screen

I have this code :

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) bg1 = display.newImageRect("graphics/background2.jpg", xWidth, yHeight) bg1.x = 1 bg1.y = 3 screenGroup:insert(bg1) bg2 = display.newImageRect("graphics/background32.jpg", xWidth, yHeight) bg2.x = xmid bg2.y = ymid - display.actualContentHeight screenGroup:insert(bg2) display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) end scroll = 2 local function bgScroll (event) bg1.y = bg1.y + scroll bg2.y = bg2.y + scroll if bg1.y == display.actualContentHeight \* 1.5 then bg1.y = display.actualContentHeight \* -.5 end if bg2.y == display.actualContentHeight \* 1.5 then bg2.y = display.actualContentHeight \* -.5 end end

And now I’m getting this error :

attempt to perform arithmetic on global 'ymid' (a nil value)

When the scene starts , I could see the background but half of it is on the screen and the other half is off .

Again, ymid does not exist. you have to define it.

I did this and the same error is occurring :

-- requires local composer = require( "composer" ) local scene = composer.newScene() local physics = require("physics") physics.start() physics.setGravity(0,9.8) local bg1 local bg2 local xWidth = display.actualContentWidth local yHeight = display.actualContentHeight -- 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) bg1 = display.newImageRect("graphics/background2.jpg", xWidth, yHeight) bg1.x = 1 bg1.y = 3 screenGroup:insert(bg1) bg2 = display.newImageRect("graphics/background32.jpg", xWidth, yHeight) bg2.x = xmid bg2.y = ymid - display.actualContentHeight screenGroup:insert(bg2) display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) end

Did you define the variable ymid? Also, please tell us on what line the error is pointing to.

Seems to be a lot of code written or pasted from elsewhere, but not tested.

Start from scratch, think about the small steps you need to go through to reach the bigger goal. At the moment you’re just running around chasing your tail, trying to catch a lucky break - it might fix it, but you might not understand why.

Write a line towards each small step, test, write another line, test. If you don’t understand the flow and dependencies of your own program, it’s difficult to make sure the computer does.

Yeah, what @nick_sherman said.  It seems to me that you need to go right back to basics.  Follow the getting started tutorials.  Read them.  If you don’t understand a bit then re-read it.  Try the code it tells you and make sure that you understand what it is doing before moving on to the next bit.  If you get an error then read what it says and look at the line of code mentioned.  A lot of errors are down to scope (if you don’t understand scope or know what it is then there’s a very good post by Rob Miracle about it which I would advise reading and bookmarking).  If you really can’t figure out what the cause is then by all means post here, but don’t just tell us there’s an error and expect us to fix it for you as you’ll never learn anything that way.  Tell us the error, what you’ve tried doing to fix it, and what happened when you did.

When you do get bits working then try changing something to see what difference it makes and think about why it made that difference.

Looking back at your posting history it appears (to me at least) that you’re just blindly copying code from somewhere and expecting it to work and then immediately posting here when it doesn’t.  Don’t forget that most of the people here also have jobs and our time is valuable.

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)

The variable ‘xWidth’ has not been defined.  Fix that and you’ll probably get another similar error which you should then easily be able to solve yourself.

I have this :

bg1 = display.newImageRect("background2.jpg", xWidth, yHeight) bg1.x = 1000 bg1.y = yHeight 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

and i’m getting this error :

attempt to index local 'bg1' (a nil value)

Add local to the front of bg1.

I already did that at the top of the file for bg1 and bg2

I don’t see xWidth, yHeight, xmid, and ymid anywhere defined in your code. Are they globals?

I have this error :

display.newImageRect() bad argument #2: width expected, but got nil ERROR: Runtime error C:\Users\user\Documents\Corona Projects\app\game.lua:86: attempt to index local 'bg1' (a nil value)

line 86 :

bg1.x = 1000

I thought they were defined ? 

bg1 = display.newImageRect("background2.jpg", xWidth, yHeight) bg1.x = 1000 bg1.y = yHeight bg2 = display.newImageRect("background32.jpg", xWidth, yHeight) bg2.x = xmid bg2.y = ymid - display.contentHeight

It’s because xWidth and yHeight are not defined variables. Were you trying to type Width and Height instead?

No they were not, it’s just Width and Height, those are the variables I saw.

Also, to get a more accurate display of width and height, use display.actualContentWidth, display.actualContentHeight, it’s better that way.

Yes I thought I just had to do this :

bg1 = display.newImageRect("background2.jpg", xWidth, yHeight) bg1.x = 1000 bg1.y = yHeight

No, that is not the case.

so how do i define it ?