Spawning Objects [RESOLVED]

Hey
So I’ve been scouring the forms and the internet trying to figure out how to spawn objects and apply physics to it. The code I have so far works but it doesn’t apply the physics and I don’t how to make a bunch appear on the screen.
If you have any questions about the code let me know.

Thanks for you help in advance!

Danny

[code]
local apple = display.newImage ( “Red Apple.png” )
physics.addBody(apple, {density=3.0})
apple.name = “apple”

local function spawnApple()
local apple = display.newImage ( “Red Apple.png” )
physics.addBody( apple, { density = 3.0 } )
apple.x = math.random(40, 260)
apple.y = math.random(60, 210)
end [import]uid: 59140 topic_id: 12870 reply_id: 312870[/import]

You problem is you have to declare what the physics variable is and start physics in your game. Put this at the top of your code.

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

Regards,
Jordan Schuetz
Ninja Pig Studios
[import]uid: 29181 topic_id: 12870 reply_id: 47248[/import]

Hey jordan sorry about that, I didn’t post enough of my code because I didn’t want to show all of it. I already have the physics and the collisions working but the collisions only seem to work on the first apple and then the second one spawns but doesn’t react to collisions. I don’t know why it is doing that. [import]uid: 59140 topic_id: 12870 reply_id: 47253[/import]

Try added physics to spawnApple and see if that works. If not, make this out side of the function.

[lua]physics.addBody( apple, { density = 3.0 } )[/lua]

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 12870 reply_id: 47259[/import]

Maybe try something like this:

[lua]local appleTable = {}
local n = 1

local function spawnApple()
local appleTable[n] = display.newImage ( “Red Apple.png” )
physics.addBody( appleTable[n], { density = 3.0 } )
appleTable[n].x = math.random(40, 260)
appleTable[n].y = math.random(60, 210)
n = n + 1
end[/lua]
[import]uid: 27965 topic_id: 12870 reply_id: 47308[/import]

Oh and I thought this might be of help…

This is the action that happens when the apple hits the bottom of the screen

[code]

local function onLocalCollision( self, event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
score = score + 1
scoreText.text = score
self:removeSelf()
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )

self:removeSelf()
end
end

apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple ) [import]uid: 59140 topic_id: 12870 reply_id: 47796[/import]

Sorry for the delay between responses.

Caleber, I’m afraid I don’t totally understand your code, I’m still a little bit new to lua, could you explain a little bit more of your code?

Also, how can I wait 3 seconds to apply the physics to game so that when the user opens the level it doesn’t start right away.

[import]uid: 59140 topic_id: 12870 reply_id: 47764[/import]

Please help, I’m so frustrated!! [import]uid: 59140 topic_id: 12870 reply_id: 47814[/import]

Seems like you are registering the collision event only once for the first apple.
Try putting
[lua]apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple ) [/lua]
inside spawnApple()function.

so ur code will look something like this

[lua]local apple = display.newImage ( “Red Apple.png” )
physics.addBody(apple, {density=3.0})
apple.name = “apple”

local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
score = score + 1
scoreText.text = score
self:removeSelf()
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
self:removeSelf()
end
end

apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )
local function spawnApple()
local apple = display.newImage ( “Red Apple.png” )
physics.addBody( apple, { density = 3.0 } )
apple.x = math.random(40, 260)
apple.y = math.random(60, 210)
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )
end[/lua] [import]uid: 71210 topic_id: 12870 reply_id: 47817[/import]

Danny,

technowand is indeed correct about your problem. You have to add an eventListener to each one of the objects you want to react to collisions. However the code will not work as-is, because the spawnApple function is being created after the function it is being called from. You could move the spawnApple function above the onLocalCollision function but I’m lazy so now is a great time to learn about a wonderful thing known as “forward referencing” (a.k.a. forward declaration). I won’t get in to the details since Jon Beebe already has a wonderful article about this very subject here (http://jonbeebe.net/post/3789692325/forward-referencing-will-save-you) but it allows you to declare a variable to be used as a function name before declaring the function itself. So I have slightly modified technowand’s code to show how to do this.
[lua]local apple = display.newImage ( “Red Apple.png” )
physics.addBody(apple, {density=3.0})
apple.name = “apple”

local spawnApple – forward reference for the spawnApple function below

local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
score = score + 1
scoreText.text = score
self:removeSelf()
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
self:removeSelf()
end
end

apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )

spawnApple = function() – this is where the spawnApple “variable” gets declared as the spawnApple function
local apple = display.newImage ( “Red Apple.png” )
physics.addBody( apple, { density = 3.0 } )
apple.x = math.random(40, 260)
apple.y = math.random(60, 210)
apple.name = “apple”
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )
end[/lua] [import]uid: 27965 topic_id: 12870 reply_id: 47827[/import]

Thanks for the fix calebr2048… I din’t notice that function call inside collision event. :slight_smile:
[import]uid: 71210 topic_id: 12870 reply_id: 47828[/import]

You’re welcome technowand! I love the community efforts on these forums! [import]uid: 27965 topic_id: 12870 reply_id: 47830[/import]

Thank you, thank you, thank you!! Both of you have been amazing, this will help my game immensely. I just have two small questions…

  1. How can I set up a variable system to limit how many times it spawns the apple.
  2. I have been using a font that is working on the simulator but when I put it on my iPod it disappears.

[code]
outOf = display.newText( “”, 253, 20, “Stencil”, 16 )
outOf:setTextColor(255, 25, 25)

outOf.text = “Out Of:”

scoreApple2 = display.newText( “”, 205, 19, “Stencil”, 16 )
scoreApple2:setTextColor(255, 255, 255)
scoreApple2.text = “5”

scoreText = display.newText(score, 0, 0, “Stencil”, 16)
scoreText:setTextColor(255, 255, 255)
scoreText.x = 301
scoreText.y = 29

[import]uid: 59140 topic_id: 12870 reply_id: 47903[/import]

Hi
to keep track of how many apples are spawned, create a local variable outside spawnApple function and increment it inside the function. you can check the limit inside the function and return true to stop executing further statements.
will look something like this

[lua]local var1
local function spawnApple()
if var1 > limit then return true end
var1 = var1 + 1
end[/lua]

for custom font try using OTF font.
u can convert TTF to OTF using the below site
http://www.freefontconverter.com/
[import]uid: 71210 topic_id: 12870 reply_id: 47925[/import]

  • EDIT -
    technowand beat me to it again! ;-p

As for your first question all you have to do is create a variable and increment it inside the spawnApple function then check to see if it is at the amount you want. I have modified the code below.
[lua]local apple = display.newImage ( “Red Apple.png” )
physics.addBody(apple, {density=3.0})
apple.name = “apple”

local spawnApple – forward reference for the spawnApple function below

local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
score = score + 1
scoreText.text = score
self:removeSelf()
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
self:removeSelf()
end
end

apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )

local n = 0 – This will be our counter variable
local maxApples = 5 – You can use any number you want here

– You can use this function for whatever you want I have just used it to print to the terminal as an example
local noMoreApples = function()
print(“Out of apples!”)
end

spawnApple = function() – this is where the spawnApple “variable” gets declared as the spawnApple function
– This is where we check to see if the counter has reached the maximum number of apples allowed
if n >= maxApples then
noMoreApples()
end
local apple = display.newImage ( “Red Apple.png” )
physics.addBody( apple, { density = 3.0 } )
apple.x = math.random(40, 260)
apple.y = math.random(60, 210)
apple.name = “apple”
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )
n = n + 1 – This is where the counter variable gets incremented
end[/lua]
As for your second question, do you have the font file in the same folder as your main.lua file? Remember that if you are using a font not natively support by the device you have to include it yourself. [import]uid: 27965 topic_id: 12870 reply_id: 47927[/import]

@calebr2048 better luck next time… :slight_smile:

I saw his question yesterday night so replied it when i saw it still unanswered. usually I reply posts to which nobody replies. I know how frustrating it is to ask a question and not to get any answers.don’t want it to happen in this forum. :slight_smile:

[import]uid: 71210 topic_id: 12870 reply_id: 47929[/import]

And I couldn’t be more grateful for the response :smiley: I know that once my knowledge gets better I can help more and more people! I love the explanations that you guys include with your post because I like to learn how it works (and I like text that works :wink:

I was wondering how do I stop the apples from falling. What is the command?

Also, I didn’t download a font, I just typed in the font I liked. Do I need to download the font and what should it look like in .buildSettings? [import]uid: 59140 topic_id: 12870 reply_id: 47930[/import]

Asking questions is one of the best ways to learn.

Do you want the apples to start to fall and then stop or do you not want them to fall at all or do you want to prevent the apples from falling off the screen?

Using custom fonts is explained in the corona doc here:
http://developer.anscamobile.com/content/display-objects#Text [import]uid: 27965 topic_id: 12870 reply_id: 47932[/import]

you should download the font and put it in to your resource folder
then in build.settings put the following code
[lua]settings =
{ iphone =
{
plist =
{ UIAppFonts =
{
“fontname.otf”
},

},
},

} [/lua] [import]uid: 71210 topic_id: 12870 reply_id: 47933[/import]

Caleber you got to it first!! haha.

Basically I want x amount of apples to fall and after that number has been reached then the apples stop falling and then have it come up with a result screen. I’ve already prevented them from falling off the screen and remove itself we it does.

The thing with the fonts is I do it exactly like they say to but I keep getting this error when I try to build it for my iPod…

Syntax error: /Users/danny/Desktop/Today’s Tutorials/acc_ball/build.settings:12: ‘}’ expected (to close ‘{’ at line 9) near ‘UIAppFonts’

and this is my code. I’ve also tried “UIAppFonts = { “gunplay.otf” }” with the brackets above the UIStatusBarHidden

[code]
settings = {
orientation =
{
default = “portrait”,
},
iphone =
{
plist=
{
UIStatusBarHidden=true,
UIApplicationExitsOnSuspend = true
UIAppFonts = {
“gunplay.otf”
}
},
},
}
[import]uid: 59140 topic_id: 12870 reply_id: 47934[/import]