3
votes

I'm learning corona sdk to make a simple game. the first that I have to do is display background image.

I made a new project in corona sdk and wrote the following code

local background = display.newImage("background.png")

this code give a small pic in upper left corner of the screen like the one shown below. I tried so many thing as said in other platform and youtube but could not find the answer.

picture that I get after the code above screenshot

3

3 Answers

1
votes

Try this:

local image = display.newImageRect( "images/background.jpg",
               display.contentWidth, display.contentHeight) 
image.x = display.contentCenterX
image.y = display.contentCenterY
0
votes

By default, a display image is positioned based on its center point at Point(0,0) on the grid. Point (0,0) starts at the top left so it makes sense that the center of your background is positioned at the top left of the screen.

There are two ways to fix your problem:

  1. set displayObject.anchorX = 0, displayObject.anchorY = 0 so that the top left of the display image is positioned at the top left of the screen.
  2. Do not set the anchor points. Instead set the displayObject.x and displayObject.y to the center of the screen.
0
votes

Try using this,

local image = display.newImageRect( "image.png", 100, 100 )
image.x = display.contentCenterX
image.y = display.contentCenterY

try using newImageRect instead of newImage.

https://docs.coronalabs.com/api/library/display/newImageRect.html#examples