Can someone help me Create this as an OOP module please.

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) :slight_smile:

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]

the first issue is there are several ways to implement classes

i do it this way, but I’m not sure it’s ideal
http://developer.anscamobile.com/forum/2010/11/09/modules-vs-classes

some of the other examples in the Code Exchange like the Scene Manager and the Game Template, and moreover the included Corona samples do it differently

I’m going to look into that latter method tonight and possibly amend my code to suit

basically if you look at movieclip.lua for instance it’s all wrapped in one big function (functions within functions) that returns the movieclip

[lua]module(…, package.seeall)

function newAnim (imageTable) – “global” public function, but only to the specific module

– local => private variables
local g = display.newGroup()

– public methods, note the :
– i believe the : is a shortcut to writing function g.stop(self)
function g:stop()
Runtime:removeEventListener( “enterFrame”, self )
end

return g – return the movieclip

end[/lua]
[lua]local movieclip = require(“movieclip”)
local myAnim = movieclip.newAnim({ “img1.png”, “img2.png”, “img3.png”, “img4.png” })
myAnim.stop() – note the . rather than : here[/lua]

i think therefore you want to do this at the end of your your shoot.new function in shoot.lua

[lua]return({slug: slug, casing: casing})[/lua]

and then call something like

[lua]local shot = shoot.bullet(…)
local slug = shot.slug
local casing = shot.casing[/lua]

This is new to me too, so don’t expect it’ll definitely work!

I’ll give it a go later.

regards
j

[import]uid: 6645 topic_id: 3507 reply_id: 10587[/import]

I’m thinking that this must be pretty impossible if none of the guru’s out there have any comment.

Even a really really simple tutorial on creating an object that can be instanced into an indexed array (in Corona speak) would be very very useful…

It’d surely help a design minded person like myself understand more :slight_smile:

Cheers

Chris [import]uid: 9457 topic_id: 3507 reply_id: 10758[/import]

I suppose the code I’ve put up to begin with is where I really have the issue.

I’m not sure what I should or could put into the class.

Using “self” etc for methods, Which function how to use functions within methods and how to output to main, the info that needs to come out. etc etc…

I’m a little perplexed as you can imagine :slight_smile: [import]uid: 9457 topic_id: 3507 reply_id: 10759[/import]