Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Increase object Velocity+Spawn Rate based on timer or score.
Started by veracode Oct 15 2019 01:06 PM

4 replies to this topic

Best Answer veracode , 22 October 2019 - 12:06 PM

I have resolved this by creating a function that states 

local function bonusLife()
  if score == score + 5 then
    lives = lives + 1
    livesText.text = "Lives: ".. lives
  end
end

using this same method i was able to increase the fallRate variable i declared earlier. there may be a cleaner way to do this but for now i just create 2 more functions to check the score again to see if it increased another 5 to continue increasing the fallRate.

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

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

I copied this object spawning code from the documentation to play with the numbers and see how it works so i could apply it to the game i am currently working on.

 

this almost mimics what i am building currently but i need to increase either the velocity or spawn rate based on a timer or score, i cant seem to figure out how to do so. 

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


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

local spawnTimer
local spawnedObjects = {}

-- Seed the random number generator
math.randomseed( os.time() )


local spawnParams = {
    xMin = 110,
    xMax = 250,
    yMin = -50,
    yMax = -50,
    spawnTime = 3000,
    spawnOnTimer = 12,
    spawnInitial = 0
}


-- Spawn an item
local function spawnItem( bounds )

    -- Create an item
    local item = display.newCircle( 0, 0, 20 )
    physics.addBody( item, "dynamic", {radius=20 , bounce = 0.5} )
    item:setLinearVelocity( 0,100 )

    -- Position item randomly within set bounds
    item.x = math.random( bounds.xMin, bounds.xMax )
    item.y = math.random( bounds.yMin, bounds.yMax )

    -- Add item to "spawnedObjects" table for tracking purposes
    spawnedObjects[#spawnedObjects+1] = item
end


local function spawnController( action, params )

    -- Cancel timer on "start" or "stop", if it exists
    if ( spawnTimer and ( action == "start" or action == "stop" ) ) then
        timer.cancel( spawnTimer )
    end

    -- Start spawning
    if ( action == "start" ) then

        -- Gather/set spawning bounds
        local spawnBounds = {}
        spawnBounds.xMin = spawnParams.xMin or 0
        spawnBounds.xMax = spawnParams.xMax or display.contentWidth
        spawnBounds.yMin = spawnParams.yMin or 0
        spawnBounds.yMax = spawnParams.yMax or display.contentHeight

        -- Gather/set other spawning params
        local spawnTime = spawnParams.spawnTime or 1000
        local spawnOnTimer = spawnParams.spawnOnTimer or 50
        local spawnInitial = spawnParams.spawnInitial or 0

        -- If "spawnInitial" is greater than 0, spawn that many item(s) instantly
        if ( spawnInitial > 0 ) then
            for n = 1,spawnInitial do
                spawnItem( spawnBounds )
            end
        end

        -- Start repeating timer to spawn items
        if ( spawnOnTimer > 0 ) then
            spawnTimer = timer.performWithDelay( spawnTime,
                function() spawnItem( spawnBounds ); end,
            spawnOnTimer )
        end

    -- Pause spawning
    elseif ( action == "pause" ) then
        timer.pause( spawnTimer )

    -- Resume spawning
    elseif ( action == "resume" ) then
        timer.resume( spawnTimer )
    end
end

spawnController( "start", spawnParams )



[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 619 posts
  • Corona SDK

If you look at the timer "spawnTimer" you will see that it is using the variable  "spawnTime" which in turn is defined by "spawnParams.spawnTime".  So you can adjust the time in the table "spawnParams"

local spawnParams = {
    xMin = 110,
    xMax = 250,
    yMin = -50,
    yMax = -50,
    spawnTime = 3000, -- this is the variable that controls the time for the timer
    spawnOnTimer = 12,
    spawnInitial = 0
 
}
-- When you are ready to adjust the timer - cancel the timer, change the spawn time and then restart the timer using spawnController
 
timer.cancel( spawnTimer ) -- cancel timer / stop spawning
 
spawnParams.spawnTime = 2500 -- change the spawn time to whatever you want
 
spawnController( "start", spawnParams ) -- restart the spawning



[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 619 posts
  • Corona SDK

*note - spawnTimer is predeclared outside of the spawnController function so when I say look at "spawnTimer" I mean when it is assigned as a timer in the spawnController function.



[TOPIC: post.html]
#4

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

i was able to figure out how to make it work but now, i guess im attempting or want to try and do it a bit different 

 

 

here is the code from my project 

 

in the function createObjects i set the velocity of all of the objects  that are spawning to fallRate which is equal to 30

 

i used the fallRate variable in place of the second parameter for setLinearVelocity in attmept to modify the variable with a function based off the score changing.... say for every 10 score the velocity should increase by 10 

i put 2000 in its place just to attempt to get the process working. 

 

i attempted to get this to work by creating 

local function increaseFallRate()
  if (score == 3) then
    fallRate = 2000
  end
end

but this did not work im not sure where to go from here, i thought maybe i could change the way i spawn the objects but then it became more complicated for what im trying to accomplish.

 

is there a way to do this with what i have or do i have to rewrite the way my spawning works?  

-----------------------------------------------------------------------------------------
--
-- 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 gameLoopTimer
local livesText
local ScoreText
local fallRate = 30
local delayRate = 3000


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, 55,30, native.systemFont, 20 )
ScoreText = display.newText(uiGroup, "Score: ".. score,56, 55, native.systemFont, 20)

local function updateText()
  livesText.text = "Lives: " .. lives
  ScoreText.text = "Score: " .. score
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"

--------------------------------------------------------------------------------
  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(100, 240) -- keeps Objects within middle of screen
      newObject.y = -60
      newObject:setLinearVelocity( 0, fallRate) -- param 2 changes falling speed
end

local function decreaseDelay()
  if (score == 3) then
    fallRate = 2000
  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 ) -- param one sets delay between new Object spawn


Edited by veracode, 17 October 2019 - 11:48 AM.


[TOPIC: post.html]
#5

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

  Best Answer

I have resolved this by creating a function that states 

local function bonusLife()
  if score == score + 5 then
    lives = lives + 1
    livesText.text = "Lives: ".. lives
  end
end

using this same method i was able to increase the fallRate variable i declared earlier. there may be a cleaner way to do this but for now i just create 2 more functions to check the score again to see if it increased another 5 to continue increasing the fallRate.




[topic_controls]
[/topic_controls]