newImage issues

so either I have the display.newImage declaration incorrect, i.e. it can’t find the image. Or I’ve done something else. The image is in the same directory as the main which the snippet below is from.

code snippet:

30    local cScale = 3  

31   local coin = display.newImage(“Sprite_Star_Green_9.png”, 100, 100)
32            coin:translate(500, 800)

33             –or trying this, which doesn’t work either
34            –coin.y = 500
35            –coin.y = 800
36            –transition.to(coin, {x = coin.x, y = coin.y, xScale = cScale, yScale = cScale})

37            transition.to(coin, {xScale = cScale, yScale = cScale})
38            transition.blink(coin,{time = 1000})

error:

main.lua:32: attempt to index local ‘coin’ (a nil value)
message
stack traceback:
    ?: in function <?:205>
    [C]: ?
    \main.lua:32: in main chunk

Can someone please show me the error of my ways?

  1. Error 1. Please use code formatting.

formatyourcode.jpg

  1. Your code looks ok, so… either that file is not in the root folder, or it is not spelled that way, or your case is wrong:

“Sprite_Star_Green_9.png”

Is it a JPG and not a PNG?

However, you don’t need to do this:

local coin = display.newImage("Sprite\_Star\_Green\_9.png", 100 , 100) coin:translate(500, 800)

Just do this:

local coin = display.newImage("Sprite\_Star\_Green\_9.png", 500, 800)

ok I’ll run through the code again following your instructions, and many thanks for showing me the way to format the code that your lives will be easier :smiley:

Just to clarify…  display.newImage() and display.newImageRect() are different.

display.newImage( filename, X, Y ) – note: x, y is positioning

display.newImageRect( filename, WIDTH, HEIGHT ) – of the image, draws the center at 0, 0 by default. You have to explicitly set the X, Y after the affect. You can use translate or just set the .x and .y, though translate seems to be a little more efficient.

This trips up some people until you get used to things.

  1. Error 1. Please use code formatting.

formatyourcode.jpg

  1. Your code looks ok, so… either that file is not in the root folder, or it is not spelled that way, or your case is wrong:

“Sprite_Star_Green_9.png”

Is it a JPG and not a PNG?

However, you don’t need to do this:

local coin = display.newImage("Sprite\_Star\_Green\_9.png", 100 , 100) coin:translate(500, 800)

Just do this:

local coin = display.newImage("Sprite\_Star\_Green\_9.png", 500, 800)

ok I’ll run through the code again following your instructions, and many thanks for showing me the way to format the code that your lives will be easier :smiley:

Just to clarify…  display.newImage() and display.newImageRect() are different.

display.newImage( filename, X, Y ) – note: x, y is positioning

display.newImageRect( filename, WIDTH, HEIGHT ) – of the image, draws the center at 0, 0 by default. You have to explicitly set the X, Y after the affect. You can use translate or just set the .x and .y, though translate seems to be a little more efficient.

This trips up some people until you get used to things.