Can someone help me Create this as an OOP module please and also help me with the “main” code.
This is for creating a gun that I’d can change up for multiple types at differing times.
This is what I’ve got so far and I’m not able to get rid of the bullets or swap them out etc etc.
I’m using the director.lua framework right now.
The following is crankArm with gun setup… It’s an image of cranks arm that has a gun attached. This would get swapped out for other arms with different guns attached. Also to be able to do cleanup ie. get rid of bullets afte x number of seconds and also I’m trying to work out how to hit test them with the enemies and add to a scoring system that I haven’t created yet. I know I’m not asking for much (yeh right) 
This is what I have in my scene.lua.
[lua]—=======================================================
—========== Include Files for Game =====================
—=======================================================
local shoot = require(“shoot”)
–=======Physics Setup =========
physics.start()
physics.setScale( 20 )
–======================================
function new()
– Cranks Arm
–local crankArm = display.newImageRect(“images/arms.png”,90,44)
local crankPistol = crankParts:grabSprite(“arms_02”,true)
–local crankMachinegun = crankParts:grabSprite(“crank_arms_machinegun_01”,true)
—=======================================================
—========== Display Groups for Game ====================
—=======================================================
local playerGroup = display.newGroup()
local bulletGroup = display.newGroup()
—=======================================================
—================= CREATE MAIN SCENE ===================
—=======================================================
– Scene setup stuff here already
—=======================================================
—=============== Create Characters =====================
—=======================================================
– other character setup stuff here already
playerGroup:insert(crankPistol)
–set pivot point for arm
local crankArm = crankPistol;
crankArm:setReferencePoint( display.TopCenterReferencePoint )
crankArm.xReference = crankArm.xReference - crankArm.width *.7
crankArm.yReference = crankArm.yReference + crankArm.height *.6
local kStartAngle = 0
– Crank Arm Rotation Controller
local function onTouch(touch)
local phase = touch.phase
if “began” == phase then
local fingerX = touch.x
local fingerY = touch.y
local crankArmInitX = crankArm.x
local crankArmInitY = crankArm.y
local dx = fingerX - crankArmInitX
local dy = fingerY - crankArmInitY
local angle = math.atan2( dy, dx )
–print ("angle is " … angle)
local angleStart = 0
local pi = math.pi
local delta = (angle - angleStart)
delta = delta*180/pi
–print("delta " … delta)
crankArm.rotation = delta
– elseif “ended” == phase or “cancelled” == phase then
– crankArm.rotation = kStartAngle
end
return delta
end
Runtime:addEventListener( “touch”, onTouch )
– The gun firing event
local function fireGun(event)
local delta = crankArm.rotation
local fingerX = event.x
local fingerY = event.y
local crankArmInitX = crankArm.x
local crankArmInitY = crankArm.y
local dx = fingerX - crankArmInitX
local dy = fingerY - crankArmInitY
local armLength = crankArm.width
local armHeight = crankArm.height
local power = 100
local shot = shoot.bullet(crankArm.x,crankArm.y,dx,dy,armLength,armHeight,power)
–shot.x = crankArm.xReference + crankArm.width
–shot.y = (crankArm.y * 1.13) - crankArm.height
shot.rotation = delta
bulletGroup:insert(shot)
–print("delta " … delta)
local ShotSound = media.newEventSound(“audio/pistolShot.caf”)
media.playEventSound(ShotSound, onComplete)
end
localGroup:addEventListener(“tap”, fireGun)
—=============================================================================
—================== Animate Elements Runtime:addEventListener( “enterFrame”, …) ===================
—=============================================================================
– A per-frame event to move the elements
local tPrevious = system.getTimer()
– Move Scenery Function
local function moveScenery(event)
– Animate Bounce for anything attached to Crank
local spriteWidth = crank.width
local spriteHeight = crank.height
– Bounce in x
crankArm.x = (crank.x * .95) - ( spriteWidth * .15)
print = ("xRef " … crank.xReference)
– Bounce in Y
crankArm.y = (crank.y * .96) - ( spriteHeight * .5)
print = ("yRef " … crank.yReference)
end
– Start everything moving
Runtime:addEventListener( “enterFrame”, moveScenery );[/lua]
and this is my shoot.lua that I would dearly love to have as a instanciatable object:
[lua]—=======================================================
—==================== shoot.lua ========================
—=======================================================
module(…, package.seeall)
function bullet(gunX,gunY,dx,dy,armLength,armHeight,power)
if power == nil then
power = 100
else if power ~= nil then
end
end
– Spawn bullet. I’m thinking that it would be better to pull these from a sprite sheet
local slug = display.newImage(“images/slug.png”)
– I’m trying to offset the bullet so that it appears to come out of the barrel wherever the arm/gun points.
– Doesn’t work too well at the moment
slug.x= gunX +20
slug.y= gunY - (armHeight * .4)
physics.addBody( slug, { density= 10.0, friction= 0.3, bounce=0.00 } )
slug.isBullet = true
slug.myName = “slug”
slug:applyForce( power*dx, power*dy, slug.x, slug.y )
print ("slug power: " … power)
– Spawn ejected casing. I’m thinking that it would be better to pull these from a sprite sheet
local casing = display.newImage(“images/casing.png”)
casing.myName = “casing”
– I’m trying to offset the shells so that it appears to come out of the barrel wherever the arm/gun points.
– Doesn’t work too well at the moment
casing.x= gunX +18
casing.y= gunY - (armHeight * .4)
physics.addBody( casing, { density= 1.0, friction= 0.3, bounce=0.00 } )
casing:applyForce( -10, 0, casing.x+math.random(3), casing.y+math.random(3) )
– terminal
print ("you shot a " … slug.myName)
return slug, casing
end[/lua]
I hope I’m not asking too much but I’m sure that if anyone can help me here as I’m a GS person originally. It would help alot of the GS none programmers get their own heads around the OOP too(I know it’s not true OO in Corona).
Thanks again in advance
Cheers
Chris [import]uid: 9457 topic_id: 3507 reply_id: 303507[/import]