Spawn when score = 10

how can i make the codes below spawn when the score equals 10?

[code]
local function spawnTen()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
ten = display.newImage( “ten.png” )
ten.x = randomPos
ten.y = -100
ten.myName = “ten”
physics.addBody(ten , “dynamic”, {isSensor = true, radius = 15})
localGroup:insert( ten ) --PEACH: Adds new orbs to localGroup for use with Director
end

end
tenTimer = timer.performWithDelay( 1500, spawnTen, 0 )

----- My score codes are below

local function checkScore()
if score == 10 then
–What goes here?
end
end
Timer = timer.performWithDelay( 4000, checkScore, 4000 ) [import]uid: 132369 topic_id: 27344 reply_id: 327344[/import]

Hi

If i assume you have a variable with the score stored then
You just need to add a call to the spawn function in your checkscorefunction. Add this: spawnTen() after the if statement in the checkscore function. I hope it works. [import]uid: 122802 topic_id: 27344 reply_id: 111072[/import]

Thank you for replying. But as soon as i start the game the “ten” starts to spawn. I only want it to spawn when the score reaches 10. [import]uid: 132369 topic_id: 27344 reply_id: 111073[/import]

Hi

How do you keep track of the score? With a local variable or via text displayed on the screen etc?
If you fx have forward declared a variable that equals ten, the spawn function will start. [import]uid: 122802 topic_id: 27344 reply_id: 111075[/import]

with a text.
[lua]–Display a simple score
scoreText = display.newText(score, 400, 10, native.systemFont, 20)
scoreText:setTextColor(0,0,0)

–Function to update score
local function updateScore()
scoreText.text = score
end

[import]uid: 132369 topic_id: 27344 reply_id: 111076[/import]

Hi

Is the score variable equals to zero? If I am correct, you just need to compare the text with 10. :slight_smile: I hope that helps :slight_smile: [import]uid: 122802 topic_id: 27344 reply_id: 111077[/import]

Im sorry. What I’m trying to do here is stop the “ten” from spawning in the beginning, and i only want it to start spawning only if the score equals 10. Please right a few lines of codes explaining what you mean cause i do not get it. Thank you. [import]uid: 132369 topic_id: 27344 reply_id: 111079[/import]

Try to move your

tenTimer = timer.performWithDelay( 1500, spawnTen, 0 )

to where you have

--What goes here?

Then change

Timer = timer.performWithDelay( 4000, checkScore, 4000 )

to

Runtime:addEventListener("enterFrame", checkScore)

If you want your code to spawn only one object when the score of 10 is reached, then you should also change the 0 in your tenTimer-delay to 1. And you could probably also lower the 1500 so the object spawns more immediately. You can also call your function directly from where you have

--What goes here?

by replacing this line with

spawnTen()

This is what i have, but its still not working. It’s not spawning at all.
[lua]local function spawnTen()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
ten = display.newImage( “ten.png” )
ten.x = randomPos
ten.y = -100
ten.myName = “ten”
physics.addBody(ten , “dynamic”, {isSensor = true, radius = 15})
localGroup:insert( ten )
end

end

local function checkScore()
if score == 2 then
tenTimer = timer.performWithDelay( 1500, spawnTen, 0 )
end
end
Runtime:addEventListener(“enterFrame”, checkScore)
[import]uid: 132369 topic_id: 27344 reply_id: 111082[/import]

That should work.
Are you sure your spawnTrue variable is set to true and that your score variable is increased to 2 somewhere in your code. And check if it’s not more than 2. You might want to try with

if score \>= 2 then

By the way, placing the object at y=-100 might result in the object being placed outside the visible screen. And if you haven’t implemented director yet, you should remove

localGroup:insert( ten ) for the time being.
[import]uid: 129287 topic_id: 27344 reply_id: 111083[/import]

Hi !

If i was you i would use something like this :

(Notice, this code is not tested)

[lua]canSpawn = true

score = 10 --Here’s your score

local function spawnTen (event)
if score == 10 and canSpawn == true then
local ten = display.newImage(“image.png”)
ten.x = 100
ten.y = 100
–Put more info about “ten” here
canSpawn = false – So “ten” only spawn once
end
end

Runtime:addEventListener(“enterFrame”, spawnTen)

– [[enterFrame means that “spawnTen” will be triggered as many times as you FPS. Proably 30 times per second.]]–
[import]uid: 119384 topic_id: 27344 reply_id: 111103[/import]

This tutorial should help (with spawning) http://www.coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/ [import]uid: 84637 topic_id: 27344 reply_id: 111837[/import]