Random Image display based on predefined positions

Hi guys
I’m trying to get some blocks to appear in random positions using predefined numbers.

At the moment, I have

-- Blocks  
 blocks = {1,2,3,4}  
  
 local r = math.random (1, #blocks )  
  
 local block1 = display.newImageRect("images/block1.png", 50, 50)  
 block1:setReferencePoint(display.CenterReferencePoint)  
 physics.addBody = "kinematic"  
 block1.name = "block"  
 block1.x = \_W \* 2  
 block1.y = \_H / blocks[r]  
  
  
 local block2 = display.newImageRect("images/block1.png", 50, 50)  
 block2:setReferencePoint(display.CenterReferencePoint)  
 physics.addBody = "kinematic"  
 block2.name = "block"  
 block2.x = \_W \* 3   
 block2.y = \_H / blocks[r]  
  

What I’m trying to do is,
a) spawn a block
b) move it across the screen
c) when it reaches the end of the screen, remove it
d) spawn another block
e) repeat [import]uid: 40538 topic_id: 13369 reply_id: 313369[/import]

try this
[lua]local physics = require(“physics”)
physics.start()

local _W = display.contentWidth/2
local _H = display.contentHeight/2
local block ={}
local blocks
function newObject()
blocks = {1,2,3,4}
local rand = math.random (1, #blocks )
local img = “BOX”…rand…".png"
block[rand] = display.newImageRect(img, 50, 50)
block[rand]:setReferencePoint(display.CenterReferencePoint)
physics.addBody( block[rand],“kinematic”, { density=0.5, friction=0.3, bounce=0} )
block[rand].name = “block”
block[rand].x = _W
block[rand].y = _H / blocks[rand]
transition.to( block[rand], { time=1000, x=400, onComplete = function() block[rand]:removeSelf(); block[rand] = nil end } )
end

local dropobjects = timer.performWithDelay( 1500, newObject, 10 )[/lua] [import]uid: 71210 topic_id: 13369 reply_id: 49109[/import]

if you want to spawn next object only after the first object is destroyed then you can use the below code also
[lua]local physics = require(“physics”)
physics.start()

local _W = display.contentWidth/2
local _H = display.contentHeight/2
local block ={}
local blocks
function newObject()
blocks = {1,2,3,4}
local rand = math.random (1, #blocks )
local img = “BOX”…rand…".png"
block[rand] = display.newImageRect(img, 50, 50)
block[rand]:setReferencePoint(display.CenterReferencePoint)
physics.addBody( block[rand],“kinematic”, { density=0.5, friction=0.3, bounce=0} )
block[rand].name = “block”
block[rand].x = _W
block[rand].y = _H / blocks[rand]
transition.to( block[rand], { time=1000, x=400, onComplete = function() block[rand]:removeSelf(); block[rand] = nil ; newObject() end } )
end

newObject() [/lua] [import]uid: 71210 topic_id: 13369 reply_id: 49110[/import]

Thanks for that technowand - I’m almost there!

With

blocks = {1,2,3,4}  

Is it possible to use decimals, such as

blocks = {1,1.5,2, 2.5,3,3.5,4}  

I’ve tried, and it seems to be working, but the first block is always clear.
Second thing is how do I insert the blocks in my display group?
Thanks again [import]uid: 40538 topic_id: 13369 reply_id: 49199[/import]

to insert to group you can use
[lua]group:insert(block[rand]) [/lua]
and I din’t get what you meant by first block is always clear…
can you post your code ? [import]uid: 71210 topic_id: 13369 reply_id: 49202[/import]

I tried group:insert(block[rand]) but I get the following error:

“ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’”
It seems the first block is “clear” (as in, I can only see the physical properties in hybrid mode) if I do not insert it in the group.

If I do insert it I get the above error, but I can see all blocks. [import]uid: 40538 topic_id: 13369 reply_id: 49214[/import]

not sure whats happening. post me the code I will try to debug it. [import]uid: 71210 topic_id: 13369 reply_id: 49218[/import]

The code is exactly the same. The only difference is that I have changed the “img” object.

local \_W = display.contentWidth/2  
local \_H = display.contentHeight/2  
local block ={}  
local blocks  
function newObject()   
 blocks = {1,2,3,4}  
 local rand = math.random (1, #blocks )  
 local img = "block.png"  
 block[rand] = display.newImageRect(img, 50, 50)  
 block[rand]:setReferencePoint(display.CenterReferencePoint)  
 physics.addBody( block[rand],"kinematic", { density=0.5, friction=0.3, bounce=0} )  
 block[rand].name = "block"  
 block[rand].x = \_W  
 block[rand].y = \_H / blocks[rand]  
 transition.to( block[rand], { time=1000, x=400, onComplete = function() block[rand]:removeSelf(); block[rand] = nil ; newObject() end } )  
end  
   
newObject()   

I’ve tried, localGroup:insert(block[rand]) inside the function and outside it but it doesn’t seem to work.
I am also using Director Class. [import]uid: 40538 topic_id: 13369 reply_id: 49228[/import]

technowand, I tried this

local block ={}  
local blocks  
function newObject()   
 blocks = {1,2,3,4}  
 local rand = math.random (1, #blocks )  
  
 local block = display.newImageRect("block.png", 50, 50)  
 localGroup:insert(block)  
 block:setReferencePoint(display.CenterReferencePoint)  
 physics.addBody( block,"kinematic", { density=0.5, friction=0.3, bounce=0} )  
 block.name = "block"  
 block.x = \_W  
 block.y = \_H / rand  
 transition.to( block, { time=1000, x=400, onComplete = function() block:removeSelf(); block = nil ; newObject() end } )  
end  

which almost works!

but now on switching scenes I get the following error:

“Runtime error
/Users/myGame/myFile.lua:289: attempt to call method ‘removeSelf’ (a nil value)”

Could it be that I’m trying to call removeSelf when the transition hasn’t completed? [import]uid: 40538 topic_id: 13369 reply_id: 49304[/import]

@w.baig84 sorry for the late reply. was busy with some other things.

you cannot remove block like that, coz you are using the same cariable name block when spawning. you should use a table and should put each block as separate object. what is the error message that you are getting when u use localGroup:insert(block[rand])? and you should use it inside the function. [import]uid: 71210 topic_id: 13369 reply_id: 49313[/import]

technowand,

I have managed to fix it with the above and a transition.cancel - works perfectly!

But I do have some questions regarding what you said:

Although I am removing a variable with one name, I don’t see why I can’t create a new one with the same name - it is only the .y value which I want to change.
Also with localGroup:insert(block[rand]), rand is from the array, but yet we are also using it for the .y value - so does this mean that there can only ever be 4 blocks? because

blocks = {1,2,3,4}  
 local rand = math.random (1, #blocks )  

[import]uid: 40538 topic_id: 13369 reply_id: 49315[/import]

OOps I saw it wrong… Sorry… you are creating new object only after finishing transitioning the first one right ? So it should be ok. and for y you don’t have to take random from table.
math.random (1, #blocks ) will translate to math.random (1, 4 ) that means random number between 1 and 4. and it can be generated any number of times. [import]uid: 71210 topic_id: 13369 reply_id: 49318[/import]

Thought so - thanks for your help!! [import]uid: 40538 topic_id: 13369 reply_id: 49330[/import]