Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Spawning the same object base off Random selection
Started by veracode Oct 22 2019 12:17 PM

12 replies to this topic

Best Answer sporkfin , 23 October 2019 - 10:38 AM

If I understand you correctly, you want to spawn a random image and then make a twin in a different location?  If that is the case, you should only do the random call once and then pass that information to each function

 

 
local randomNum = math.random(18)
objectMatch(randomNum)
objectMatch2(randomNum)

 

and then the functions would look like

 

local function objectMatch(randomNum)
     newObject = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     -- add positioning info here
end
 

local function objectMatch2(randomNum)
     newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     -- add positioning info here
end

 

Now you have the same image - just place them where you want them.  Eventually you might want to make on function that can be used for both placements by passing more parameters.

 

Something like:

local function placeObject(randomNum, xPos, yPos)
     newObject = display.newImageRect("Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     newObject.x = xPos ; newObject.y = yPos
     return newObject
end
 
local randomNum = math.random(18)

local leftObject = placeObject(randomNum, 100, 100)
local rightObject = placeObject(randomNum, 300, 100)

 

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

i am attempting to spawn an object that matches the randomly spawned object but am not sure how to do so.

 

i have a folder called Objects , within that folder i have 18 variations of the same object named object1,object2,object3...etc. 

 

i want to be able to spawn the first object in the middle of the screen then spawn the object that matches it on the right or left side of the screen.  

 

here is the code i have on this,

local function objectMatch() -- works on left side of screen
  if newObject == "Objects/Object2.png" then
    display.newImageRect( mainGroup, "Objects/Object2.png" , 69.2 , 60.8, display.contentCenterX, display.contentCenterY)
  end
      --newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8)
      --newObject2.x = 50
      --newObject2.y = math.random (60 , 600)
end
local function objectMatch2() -- works on right side of screen
     newObject3 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8)
     newObject3.x = 310
     newObject3.y = math.random (60 , 600)
end


local function createObjects()
  newObject = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8)
  table.insert( ObjectsTable, newObject )
  physics.addBody( newObject, "dynamic", {radius=0 , bounce = 0.5} ) -- changing radius adjusts hit box, hitbox does not mean touchbox
  newObject.myName = "Object"



objectMatch()
objectMatch2()

local whereFrom
      --from top
      newObject.x = math.random(150, 210) -- keeps Objects within middle of screen
      newObject.y = -60
end

The code currently will just spawn random objects and not match the objects already spawned, this is because of the code that is in  objectMatch2()

local function objectMatch2() -- works on right side of screen
     newObject3 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8)
     newObject3.x = 310
     newObject3.y = math.random (60 , 600)
end

i attempted to get this to work with the code in objectMatch1() but this did not work either. 

local function objectMatch() -- works on left side of screen
  if newObject == "Objects/Object2.png" then
    display.newImageRect( mainGroup, "Objects/Object2.png" , 69.2 , 60.8, display.contentCenterX, display.contentCenterY)
  end
      --newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8)
      --newObject2.x = 50
      --newObject2.y = math.random (60 , 600)
end

any help on how to solve this would be much appreciated! 



[TOPIC: post.html]
#2

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

moved this to its own topic

Edited by veracode, 23 October 2019 - 09:03 AM.


[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 619 posts
  • Corona SDK

  Best Answer

If I understand you correctly, you want to spawn a random image and then make a twin in a different location?  If that is the case, you should only do the random call once and then pass that information to each function

 

 
local randomNum = math.random(18)
objectMatch(randomNum)
objectMatch2(randomNum)

 

and then the functions would look like

 

local function objectMatch(randomNum)
     newObject = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     -- add positioning info here
end
 

local function objectMatch2(randomNum)
     newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     -- add positioning info here
end

 

Now you have the same image - just place them where you want them.  Eventually you might want to make on function that can be used for both placements by passing more parameters.

 

Something like:

local function placeObject(randomNum, xPos, yPos)
     newObject = display.newImageRect("Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     newObject.x = xPos ; newObject.y = yPos
     return newObject
end
 
local randomNum = math.random(18)

local leftObject = placeObject(randomNum, 100, 100)
local rightObject = placeObject(randomNum, 300, 100)

 



[TOPIC: post.html]
#4

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

If I understand you correctly, you want to spawn a random image and then make a twin in a different location?  If that is the case, you should only do the random call once and then pass that information to each function

 

 
local randomNum = math.random(18)
objectMatch(randomNum)
objectMatch2(randomNum)

 

and then the functions would look like

 

local function objectMatch(randomNum)
     newObject = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     -- add positioning info here
end
 

local function objectMatch2(randomNum)
     newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     -- add positioning info here
end

 

Now you have the same image - just place them where you want them.  Eventually you might want to make on function that can be used for both placements by passing more parameters.

 

Something like:

local function placeObject(randomNum, xPos, yPos)
     newObject = display.newImageRect("Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     newObject.x = xPos ; newObject.y = yPos
     return newObject
end
 
local randomNum = math.random(18)

local leftObject = placeObject(randomNum, 100, 100)
local rightObject = placeObject(randomNum, 300, 100)

 

 

yes, you are correct.

 

this makes sense, i am going to attempt to apply it and will update. 
 

the other way i was going with this was creating individual functions for each object then randomizing the function that plays.

super happy that i might not have to do that XD 

 

 

Thank You



[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

yes, you are correct.

 

this makes sense, i am going to attempt to apply it and will update. 
 

the other way i was going with this was creating individual functions for each object then randomizing the function that plays.

super happy that i might not have to do that XD 

 

 

Thank You

 

A key principle in programming, but one that takes a while to master, is D.R.Y - don't repeat yourself.

 

When you find two or more pieces of code doing almost exactly the same thing, perhaps with a different value for positions, or colours, or text, there will be a way to make just one function that takes in the required parameters and does it all. It also makes it easier to change things if you only have to amend one function instead of multiple.

 

For example if you had ten functions for creating animals - createHorse, createDog etc. and then decided each animal should have a .name property, there's ten places to add it instead of one called createAnimal().


  • sporkfin likes this

[TOPIC: post.html]
#6

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

so i have added this into my code and it works as far as creating 3 sets of the image with every spawn, 

 

so if object1.png was the randomly selected object it spawns it in the middle of the screen and on the left and right.  but  

 

it is not selecting a new image within the objects folder, it just continues to spawn object1.png over and over.

 

which is very odd because if i place math.random(18) directly into the code it randomly selects an object form the folder each time the loop runs.

 

 

 

-- i attempted to add the concatenation .. ".png" to the variable randomNum thinking maybe this would resolve the issue or if not at least create an error or different result but the results were the same, the same,  "object(#).png" spawning over and over but within the correct coord placement 


Edited by veracode, 24 October 2019 - 09:39 AM.


[TOPIC: post.html]
#7

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

Are you changing the random number each time you call the loop? randomNum will not change from the value it is first given unless it is assigned a new random number.



[TOPIC: post.html]
#8

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

i see now,

somewhere within here i have to say acquire a new random number to associate with the variable randomNum for every loop.

 

What i did was declare it at the top and then just run the code ... obviously not going to work. 



[TOPIC: post.html]
#9

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

nope still no luck i think im missing something, at first i thought it was where i was declaring

 

 local randomNum = math.random(18)

 

in the wrong place but even after moving things around and not getting any errors i still have the same result, somehow i need to get the random number to change for every loop



[TOPIC: post.html]
#10

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

Difficult to help further as we don't know how your code has changed since the start...



[TOPIC: post.html]
#11

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

right sorry, here is all of it from where im at now i have noly adjusted what was suggested by sporkfin but am still confused where to palce it so the loop works properly 

 

 

 

there are some parts that need adjusting to work such as the increaseFallRate() but i have it commented out until i can get other necessary parts moving correctly. 

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local physics = require("physics")
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( "hybrid" )


math.randomseed(os.time())

local lives = 3
local score = 0
local died = false

local ObjectsTable = {}

local newObject
local newObject2
local gameLoopTimer
local livesText
local ScoreText
local fallRate = 100
local delayRate = 1000

local randomNum = math.random(18)

local backGroup = display.newGroup()
local mainGroup = display.newGroup()
local uiGroup = display.newGroup()

local backGround = display.newImageRect(backGroup, "Images/BackGround1.png", display.contentWidth, display.contentHeight)
backGround.x = display.contentCenterX
backGround.y = display.contentCenterY

livesText = display.newText( uiGroup, "Lives: ".. lives, display.contentCenterX - 120,15, native.systemFont, 20 )
ScoreText = display.newText(uiGroup, "Score: ".. score,display.contentCenterX + 120, 15, native.systemFont, 20)

local function updateText()
  livesText.text = "Lives: " .. lives
  ScoreText.text = "Score: " .. score
end
--------------------------------------------------------------------------------
local function ObjectMatch(randomNum)
      newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
      newObject2.x = display.contentCenterX - 140
      newObject2.y = math.random (60 , 900)
end
local function ObjectMatch2(randomNum)
     newObject3 = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
     newObject3.x = display.contentCenterX + 140
     newObject3.y = math.random (60 , 900)
end
--------------------------------------------------------------------------------
local function createObjects()
  newObject = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8)
  table.insert( ObjectsTable, newObject )
  physics.addBody( newObject, "dynamic", {radius=0 , bounce = 0.5} ) 
  newObject.myName = "Object"



  ObjectMatch(randomNum)
  ObjectMatch2(randomNum)
--------------------------------------------------------------------------------
  local function dragObject ( event )
    newObject = event.target
    local phase = event.phase

    if ( "began" == phase ) then
      display.currentStage:setFocus( newObject, event.id )
      newObject.isFocus = true
      newObject.touchOffsetX = event.x - newObject.x
      newObject.touchOffsetY = event.y - newObject.y

    elseif ( newObject.isFocus ) then

      if ( "moved" == phase) then
      newObject.x = event.x - newObject.touchOffsetX
      newObject.y = event.y - newObject.touchOffsetY

    elseif ( "ended" == phase or "cancelled" == phase ) then
      display.currentStage:setFocus(newObject, nil )
      newObject.isFocus = false
      end
    end
    return true
  end

  newObject:addEventListener( "touch", dragObject)
--------------------------------------------------------------------------------


  local whereFrom
      --from top
      newObject.x = math.random(150, 210) -- keeps Objects within middle of screen
      newObject.y = -60
      newObject:setLinearVelocity( 0, fallRate) -- param 2 changes falling speed
end

--local function increaseFallRate()
  --if (score > 3) then
  --  whereFrom = newObject:setLinearVelocity(10,3000)
--  end
--end

local function gameLoop()
  createObjects()

  for i = #ObjectsTable, 1, -1 do
    local thisObject = ObjectsTable[i]

    if ( thisObject.x < -100 or thisObject.x > display.contentWidth + 100 or thisObject.y < -100 or thisObject.y > display.contentHeight + 100)
    then
      display.remove( thisObject )
      table.remove( ObjectsTable, i )
    end
  end
end

gameLoopTimer = timer.performWithDelay( delayRate, gameLoop, 0 ) 



[TOPIC: post.html]
#12

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

Insert randomNum = math.random(18) right at the start of your createObjects function.



[TOPIC: post.html]
#13

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

that worked....so simple....  maybe I've been staring at the code too long , this was painfully obvious. 

 

Thank you again everyone!  

 

next part is adding a collision listener, but if i cant figure it out i believe that is a story for another topic.




[topic_controls]
[/topic_controls]