delme
replace
local p = display.newRect(x, y, width, height)
with
local p = display.newImageRect(“image.png”, width, height)
p.x = x
p.y = y
next line is from graphics 1.0 it is not valid any more unles you are using compatibility mode and can be removed as center is default in G2.0:
p:setReferencePoint(display.CenterReferencePoint)
remove this also as you don’t need color for an image:
p:setFillColor(0, 0, 0)
Wow that worked perfectly! Thank you!
I tried to apply the same method to the other shapes drawn in lua however I guess the syntax is incorrect because I cannot run without errors. In bold below is what I’m trying to have as a .png image. Do objects in this way work differently?
Thank you!
– objectType is “food”, “enemy”, “reward”, or “penalty”
spawn = function( objectType, xVelocity, yVelocity )–{{{
local object
local sizeXY = math.random( 10, 20 )
local startX
local startY
if 0 == xVelocity then
– Object moves along the y-axis
startX = math.random( sizeXY, display.contentWidth - sizeXY )
end
if xVelocity < 0 then
– Object moves to the left
startX = display.contentWidth
end
if xVelocity > 0 then
– Object moves to the right
startX = -sizeXY
end
if 0 == yVelocity then
– Object moves along the x-axis
startY = math.random( sizeXY, display.contentHeight - sizeXY )
end
if yVelocity < 0 then
– Object moves to the top
startY = display.contentHeight
end
if yVelocity > 0 then
– Object moves to the bottom
startY = -sizeXY
end
local collisionFilter = { categoryBits = 4, maskBits = 2 } – collides with player only
local body = { filter=collisionFilter, isSensor=true }
if “food” == objectType or “enemy” == objectType then
object = display.newRect ( startX, startY, sizeXY, sizeXY )
object.sizeXY = sizeXY
end
if “reward” == objectType or “penalty” == objectType then
object = display.newCircle ( startX, startY, 15 )
object.sizeXY = 30
end
local objectAlpha
if “food” == objectType or “reward” == objectType then
object:setFillColor ( 0, 0, 0, (gameIsOver and 20 or 255) )
else
object:setFillColor ( 0, 0, 255, (gameIsOver and 20 or 255) )
end
object.objectType = objectType
object.xVelocity = xVelocity
object.yVelocity = yVelocity
physics.addBody ( object, body )
object.isFixedRotation = true
table.insert ( objects, object )
end–}}}
The problem is here:
if “food” == objectType or “reward” == objectType then
object:setFillColor ( 0, 0, 0, (gameIsOver and 20 or 255) )
else
object:setFillColor ( 0, 0, 255, (gameIsOver and 20 or 255) )
end
You can not set color to an image. Remove that and you should be fine.
Okay, I made these changes however now my images are massively distorted when it runs. Everything is supposed to be the size of the brown orb, which is the player. Image2, and Image3 are the red and pink orbs:
local body = { filter=collisionFilter, isSensor=true }
if “food” == objectType or “enemy” == objectType then
object = display.newImageRect( “image2.png” , startX, startY, sizeXY, sizeXY )
object.sizeXY = sizeXY
end
if “reward” == objectType or “penalty” == objectType then
object = display.newImageRect( “image3.png” , startX, startY, 15 )
object.sizeXY = 30
end
object.objectType = objectType
object.xVelocity = xVelocity
object.yVelocity = yVelocity
physics.addBody ( object, body )
object.isFixedRotation = true
table.insert ( objects, object )
The parameters in newImageRect define how the image is shown. display.newImageRect(filename, x, y, width, height)
You have to set correct width and height for your images. You can use display.newImage and it will be shown in original height howe3ver I don’t recommend it. It is always better to use newImageRect in my opinion. So just check you set the width and height accordingly (calculate the aspect ration of your image). Note that with image3 you didn’t even provide height.
replace
local p = display.newRect(x, y, width, height)
with
local p = display.newImageRect(“image.png”, width, height)
p.x = x
p.y = y
next line is from graphics 1.0 it is not valid any more unles you are using compatibility mode and can be removed as center is default in G2.0:
p:setReferencePoint(display.CenterReferencePoint)
remove this also as you don’t need color for an image:
p:setFillColor(0, 0, 0)
Wow that worked perfectly! Thank you!
I tried to apply the same method to the other shapes drawn in lua however I guess the syntax is incorrect because I cannot run without errors. In bold below is what I’m trying to have as a .png image. Do objects in this way work differently?
Thank you!
– objectType is “food”, “enemy”, “reward”, or “penalty”
spawn = function( objectType, xVelocity, yVelocity )–{{{
local object
local sizeXY = math.random( 10, 20 )
local startX
local startY
if 0 == xVelocity then
– Object moves along the y-axis
startX = math.random( sizeXY, display.contentWidth - sizeXY )
end
if xVelocity < 0 then
– Object moves to the left
startX = display.contentWidth
end
if xVelocity > 0 then
– Object moves to the right
startX = -sizeXY
end
if 0 == yVelocity then
– Object moves along the x-axis
startY = math.random( sizeXY, display.contentHeight - sizeXY )
end
if yVelocity < 0 then
– Object moves to the top
startY = display.contentHeight
end
if yVelocity > 0 then
– Object moves to the bottom
startY = -sizeXY
end
local collisionFilter = { categoryBits = 4, maskBits = 2 } – collides with player only
local body = { filter=collisionFilter, isSensor=true }
if “food” == objectType or “enemy” == objectType then
object = display.newRect ( startX, startY, sizeXY, sizeXY )
object.sizeXY = sizeXY
end
if “reward” == objectType or “penalty” == objectType then
object = display.newCircle ( startX, startY, 15 )
object.sizeXY = 30
end
local objectAlpha
if “food” == objectType or “reward” == objectType then
object:setFillColor ( 0, 0, 0, (gameIsOver and 20 or 255) )
else
object:setFillColor ( 0, 0, 255, (gameIsOver and 20 or 255) )
end
object.objectType = objectType
object.xVelocity = xVelocity
object.yVelocity = yVelocity
physics.addBody ( object, body )
object.isFixedRotation = true
table.insert ( objects, object )
end–}}}
The problem is here:
if “food” == objectType or “reward” == objectType then
object:setFillColor ( 0, 0, 0, (gameIsOver and 20 or 255) )
else
object:setFillColor ( 0, 0, 255, (gameIsOver and 20 or 255) )
end
You can not set color to an image. Remove that and you should be fine.
Okay, I made these changes however now my images are massively distorted when it runs. Everything is supposed to be the size of the brown orb, which is the player. Image2, and Image3 are the red and pink orbs:
local body = { filter=collisionFilter, isSensor=true }
if “food” == objectType or “enemy” == objectType then
object = display.newImageRect( “image2.png” , startX, startY, sizeXY, sizeXY )
object.sizeXY = sizeXY
end
if “reward” == objectType or “penalty” == objectType then
object = display.newImageRect( “image3.png” , startX, startY, 15 )
object.sizeXY = 30
end
object.objectType = objectType
object.xVelocity = xVelocity
object.yVelocity = yVelocity
physics.addBody ( object, body )
object.isFixedRotation = true
table.insert ( objects, object )
The parameters in newImageRect define how the image is shown. display.newImageRect(filename, x, y, width, height)
You have to set correct width and height for your images. You can use display.newImage and it will be shown in original height howe3ver I don’t recommend it. It is always better to use newImageRect in my opinion. So just check you set the width and height accordingly (calculate the aspect ration of your image). Note that with image3 you didn’t even provide height.
Hey, I’m just now returning to this project. It was working in the way you suggested before, however, with no changes to the code, when I try to boot the main lua, I receive this error:
Did something change with the way Corona works? Everything was working before.
What version of Corona are you using?
What version were you using when it worked last?
What code is at line 106?
Hey there,
Thank for responding.
>What version of Corona are you using?
CoronaSDK-2013.1202
>What version were you using when it worked last?
CoronaSDK-2013.1202
> What code is at line 106?
p.x = x
You can see in post #2 the modifications that were suggested to me. And they were working at the time with the same version. Unsure what happened, the file hasn’t been changed since.
Any update on this? Did something change with how Corona works?
Well the error is telling me that the object p is nil. If p is coming from that display.newImageRect() perhaps the image isn’t there, invalid, or for some reason. Have you looked at your console log to see if there are any more messages besides the one that pops up?
Read this if you need to learn how to read the console log: http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/
Hey, I’m just now returning to this project. It was working in the way you suggested before, however, with no changes to the code, when I try to boot the main lua, I receive this error:
Did something change with the way Corona works? Everything was working before.
What version of Corona are you using?
What version were you using when it worked last?
What code is at line 106?