newImage to newImageRect?

So I am using a table to call obstacles into my game based on math.random. Because I don’t know the height or width of the obstacle I’ll be calling, since they are all different, I have to use newImage. Is there a way that once the image is calling in the code, I can get the height and width of that image and then use those values to call new imageRect? Here is the code:

local obs = {"obstacle5", "obstacle4", "obstacle6", "obstacle2", "obstacle1"} local random = obs[math.random(#obs)]; local physicsData = (require "level1Obst").physicsData(1.0) local function ranGen() if random == "obstacle6" then objObst3 = display.newImage(random..".png"); objObst3.Asteroids = random physics.addBody( objObst3, "kinematic", physicsData:get(random)) objObst3.x = 1000 objObst3.y = 200 objObst3.type = "op1" transition.to(objObst3,{time = 10000,x=(W-2000), onComplete = handleOnComplete, tag = "transTag"}) sceneGroup:insert(objObst3) local function obst6Rotate() transition.to(objObst3,{time = 2500,rotation = objObst3.rotation-360, onComplete = obst6Rotate, tag = "transTag"}) end obst6Rotate() else obj = display.newImage(random..".png"); obj.Asteroids = random physics.addBody( obj, "kinematic", physicsData:get(random)) obj.x = 1000 obj.y = 200 obj.type = "op1" transition.to(obj,{time = 10000,x=(W-2000), onComplete = handleOnComplete, tag = "transTag"}) sceneGroup:insert(obj) end end

Also, I am using these if statements to add a 360 transition to only one of the obstacles and keeping the others seperate. So based on this, is there a line of code I could use to call these obsatcles with newImageRect? Thanks for reading!

Not sure what you mean by “call obstacles” but if you want to load an image and get it’s height and width just use it’s .width and .height properties, then use those in the call to newImageRect.

if you’re already building a table of filenames, why not finish it by giving their dimensions as well?  fe, roughly:

obstacleList = { { -- #1 filename = "obstacle1.png", width = 64, height = 64 }, { -- #2 filename = "obstacle2.png", width = 128, height = 128 }, -- and so on } local index = random(#obstacleList) local obstacle = obstacleList[index] local image = display.newImageRect(obstacle.filename, obstacle.width, obstacle.height)

Not sure what you mean by “call obstacles” but if you want to load an image and get it’s height and width just use it’s .width and .height properties, then use those in the call to newImageRect.

if you’re already building a table of filenames, why not finish it by giving their dimensions as well?  fe, roughly:

obstacleList = { { -- #1 filename = "obstacle1.png", width = 64, height = 64 }, { -- #2 filename = "obstacle2.png", width = 128, height = 128 }, -- and so on } local index = random(#obstacleList) local obstacle = obstacleList[index] local image = display.newImageRect(obstacle.filename, obstacle.width, obstacle.height)