Question about display.newRect()

Hello. I’m a beginner and i’m working on an assignment at school. I want to make a rectangle as the wall that fit the screen so the ball could bounce. i used display.newRect to create a rectangle but the sides are not connected to each other because of the wrong dimensions so there are holes on the sides where the ball would disappear if it touches those areas. How do i fix it? Thank you

The display is Iphone 5 640x1136

Here is my code: 

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 4.9)

local background = display.newImage(“bkg_bricks.png”, 130, 240);

local balloon = display.newImage(“red_balloon.png”)

balloon.x = display.contentWidth/2

physics.addBody(balloon, { bounce = 1, radius = 45, friction = 0.9} )

local leftWall = display.newRect(0, 0 , 0, display.contentHeight )

local rightWall = display.newRect( display.contentWidth, 0, 0, display.contentHeight )

local ceiling = display.newRect(0, 0, display.contentWidth, 1 )

physics.addBody(leftWall, “static”, { bounce = 0.1} )

physics.addBody(rightWall, “static”, { bounce = 0.1} )

physics.addBody(ceiling, “static”, { bounce = 0.1} )

local floor = display.newImage(“floor.png”, 30, 10);

floor.y = display.contentHeight+25

physics.addBody(floor, “static”, { bounce = 0.0, friction = 1.0})

display.setStatusBar(display.HiddenStatusBar);

function moveBalloon(event)

local balloon = event.target

balloon:applyLinearImpulse( 0, -0.2, event.x, event.y )

end

balloon:addEventListener(“touch”, moveBalloon)

So you want to make borders around the screen/just off the screen?

There are a couple of issues. First, 0, 0 is the top right corner of your content area. If you center a rectangle at 0, 0, then only half of it’s width will be on screen. You have to position it at the center of the screen on the X axis for your ceiling. The same with the side walls on the Y axis.

Next your rectangle should be wider than 1 pixel. Thin objects can be passed through with fast enough objects.

Rob

i want to made borders around the screen 

can you show me the code for this particular problem. i don’t really know how to do it

local leftWall = display.newRect( -5, display.contentCenterY, 10, display.contentHeight ) local rightWall = display.newRect( display.contentWidth + 5, display.contentCenterY, 10, display.contentHeight ) local ceiling = display.newRect( display.contentCenterX, -5, display.contentWidth, 10 )

Each wall will be 10 points wide and the height of the content area. Since you don’t want them on screen, we need to move them off screen a bit. The Center point of a 10 x contentHeight rectangle is 5, display.contentCenterY. So these values become the X Y to display.newRect(). On the left side -5 for the X will position the box so it’s right edge is at 0. On the right side, display.contentWidth + 5 will position it correct.

The 3rd and 4th parameters are the width and height of the rectangle. I just made sure they were 10 pixels wide.

Rob

So you want to make borders around the screen/just off the screen?

There are a couple of issues. First, 0, 0 is the top right corner of your content area. If you center a rectangle at 0, 0, then only half of it’s width will be on screen. You have to position it at the center of the screen on the X axis for your ceiling. The same with the side walls on the Y axis.

Next your rectangle should be wider than 1 pixel. Thin objects can be passed through with fast enough objects.

Rob

i want to made borders around the screen 

can you show me the code for this particular problem. i don’t really know how to do it

local leftWall = display.newRect( -5, display.contentCenterY, 10, display.contentHeight ) local rightWall = display.newRect( display.contentWidth + 5, display.contentCenterY, 10, display.contentHeight ) local ceiling = display.newRect( display.contentCenterX, -5, display.contentWidth, 10 )

Each wall will be 10 points wide and the height of the content area. Since you don’t want them on screen, we need to move them off screen a bit. The Center point of a 10 x contentHeight rectangle is 5, display.contentCenterY. So these values become the X Y to display.newRect(). On the left side -5 for the X will position the box so it’s right edge is at 0. On the right side, display.contentWidth + 5 will position it correct.

The 3rd and 4th parameters are the width and height of the rectangle. I just made sure they were 10 pixels wide.

Rob