Spawn anywhere accept certain positions.

*** Title supposed to be ’ Except certain positions’ and not ’ Accept’ *** sorry for that mistake

I’m wondering if its possible to have a table of coordinates and inside a spawn function that creates a display object, make it exclude those positions inside the table when positioning. 

For example, I have another object that is spawning at these coordinates only…

local yPosition = {100, 160, 220 }

And using them like this when creating an object.

local rnd = math.floor(math.random() \* 3) +1 if(rnd == 0) then gem2 = display.newImageRect("images/obstacle1.png", 90,212) gem2.x = display.contentWidth + 100 gem2.y = yPosition[math.floor(math.random() \* 4)+1]

Is there a way to use something similar to that, but only exclude the positions in yPosition and spawn everywhere else? 

Hope this made sense. 

Cheers.

In your case rnd  will never be 0?  If you want to generate a random number between 1 and 3 use math.random(1,3).

Two ways of approaching your problem.  1) Have an array of allowable spawn points - good if the count of spawn points is low. 2) Check if a spawn point is not being used and then continue.

For for the comment adrianm, I think you might have misunderstood my question which is actually my fault. sorry. 

The snippet from the code including the rnd part was just a demonstration of how I’m using yPosition variable. That function has more rnd values inside the function and works fine. 

I guess the main question is: How can I make a new spawn exclude the values inside yPosition. 

Sorry for the confusion.

Ah OK… was just pointing out what appeared a logic bug (this is the trouble with partial code).

If you want to exclude a range of values do something like this

local yPositions = {100, 160, 220, etc} local yPos = 0 local canPlace, foundSpawnPoint = true, false while not foundSpawnPoint do yPos = math.random(1,240) for i = 1, #yPositions do if yPos == yPositions[i] then canPlace = false break end end if canPlace then foundSpawnPoint = true end end

Note: this is not the most optimal way but I’ve coded it like this so it is easy for you to understand.

Thats perfect. Exactly what I was after and learned something new. Thanks for the help adrianm

In your case rnd  will never be 0?  If you want to generate a random number between 1 and 3 use math.random(1,3).

Two ways of approaching your problem.  1) Have an array of allowable spawn points - good if the count of spawn points is low. 2) Check if a spawn point is not being used and then continue.

For for the comment adrianm, I think you might have misunderstood my question which is actually my fault. sorry. 

The snippet from the code including the rnd part was just a demonstration of how I’m using yPosition variable. That function has more rnd values inside the function and works fine. 

I guess the main question is: How can I make a new spawn exclude the values inside yPosition. 

Sorry for the confusion.

Ah OK… was just pointing out what appeared a logic bug (this is the trouble with partial code).

If you want to exclude a range of values do something like this

local yPositions = {100, 160, 220, etc} local yPos = 0 local canPlace, foundSpawnPoint = true, false while not foundSpawnPoint do yPos = math.random(1,240) for i = 1, #yPositions do if yPos == yPositions[i] then canPlace = false break end end if canPlace then foundSpawnPoint = true end end

Note: this is not the most optimal way but I’ve coded it like this so it is easy for you to understand.

Thats perfect. Exactly what I was after and learned something new. Thanks for the help adrianm