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]