Random item spawns in angry birds type game?

I’ve been going through the forum for the past few days trying to figure out what I need to do to add an angry birds style slingshot into my game that generates a different launch object every time it’s used. The code I have is below.

I tried to add a math function to produce a new object every time but all I get is an error. This is becoming frustrating and very time consuming trying to figure this out so any and all help is greatly appreciated.

[code]
module(…, package.seeall)
math.randomseed( os.time() )

local images = {}

images[#images + 1] = “object1.png”
images[#images + 1] = “object2.png”
images[#images + 1] = “object3.png”
images[#images + 1] = “object4.png”
– Pass state reference
state = {};
– Bullet starts off in-active
ready = false;
– Pass audio references
shot = {};
band_stretch = {};

function bullet(event)

– Import easing plugin
local easingx = require(“easing”);

– Bullet properties
local bullet = {
name = “bullet”,
type = “bullet”,
density= 0.8,
friction= 0.2,
bounce= 0.5,
size = 15,
rotation = 0
}

local index = math.random( 1, #images )
local bullet = display.newImage( images[index] )

– Place bullet
bullet.x = 0; bullet.y = 0;
– Set up physical properties
physics.addBody(bullet, “dynamic”, {density=bullet.density, friction=bullet.friction, bounce=bullet.bounce, radius=bullet.size});

bullet.linearDamping = 0.3;
bullet.angularDamping = 0.8;
bullet.isBullet = true;
– Transition the bullet into position each time it’s spawned
transition.to(bullet, {time=600, y= _y, transition = easingx.easeOutElastic});

return bullet;

end
bullet:addPhysics(physics,‘dynamic’,{})

– Place bullet
bullet.x = -100
bullet.y = math.random(120,924)

physics.addBody(bullet,“dynamic”,{isSensor = true})
bullet.isSensor = true
transitions[#transitions + 1] = transition.to(bullet, {time = math.random(1400,1500), delay = 0, x = bullet.x + 968, onComplete=function() bullet :removeSelf() end})
spawnedGroup:insert(bullet)

return bullet

end [import]uid: 123618 topic_id: 22705 reply_id: 322705[/import]

where do you create “bullet” by calling newProjectile()?

Where do you create multiple bullets?

[import]uid: 19626 topic_id: 22705 reply_id: 90576[/import]

I corrected the code error of newProjectile. The problem partly is creating multiple bullets. I tried to use that image table at the top that list object1, object2 and so on to use as my bullets but it just errors out. [import]uid: 123618 topic_id: 22705 reply_id: 90581[/import]

Your “images” table is just a table of file names, not the display objects.

Your basic design pattern here goes something like this:

  
objects = {}  
  
function spawnBullet() -- I would avoid naming the function and other variables the same name...  
 local images = {"object1.png","object2.png","object3.png"}  
 local whichImage = images[math.random(3)]  
 local bullet = display.newImage(whichImage)  
 ...  
 ... -- rest of your setup code  
 ...  
 return bullet  
end  
  
for i = 1, maxObjects do  
 objects[i] = spawnBullet()  
 -- do stuff with this object  
end  

[import]uid: 19626 topic_id: 22705 reply_id: 90586[/import]