I want to have my screen scroll up endlessly while the user jumps from brick to brick on the side of the screen and in the middle . I have an image of the brick and the user will use a ball to jump from brick to brick . The screen is upright . Any help please ?
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 ?
At the top of your file, write,
local xWidth = display.actualContentWidth local yHeight = display.actualContentHeight
I’m still getting the same error .
top of the file :
local bg1 local bg2 local xWidth = display.actualContentWidth local yHeight = display.actualContentHeight
rest of the code :
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.actualContentHeight 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
Try putting 1000, and 300, or other numbers, where it says xWidth and yHeight, on line 85.
That didn’t change anything
There appears to be some scope issues here. Shouldn’t you be creating your background and so forth in the proper Composer scene blocks, not outside of them?
Brent