Questions about OOP

Is it possible to make something thats within a function universal to all the things connected to it? Like for example I have a function that drags a very specific set of wings and health bar follows those specific set of wings. If I went ahead and added a new set of wings and a new health bar to the mix would that function work with that new set of wings and new health bar too? I know the point of OOP is to avoid having to re-write that function for the new set of wings and health bar, How can I do that!

Well off the top of my head I have some ideas but I am not sure how If I'm right since I've never tried anything like this.. I could put the wings in a table and do it that way. But where do I put the table? In the class itself?

I studied this blog by Jonathan Beebe and understand what its saying but can't seem to wrap my head on what should be in the class or in the main file as for the wings and healthrect

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

My mind is spinning so any help would really get the ball rolling for my game!

Here is an example code I was talking about..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
local healthRect = display.newRect(0,0,5,5)
healthRect:setFillColor(255,0,0)
        
local wings = display.newImageRect("wings.png", 50, 50)
wings.x = rand(100,550); wings.y = 150
wings.health = 2
 
local function moveMe(event)
        local phase = event.phase
        local x = event.x
        local y = event.y
        local target = event.target
        
        if "began" == phase then
                display.currentStage:setFocus(wings)
                target.x0 = x
                target.y0 = y
                target.isFocus = true
        elseif "moved" == phase and target.isFocus==true then
                target.x = target.x + (x-target.x0)
                target.y = target.y + (y-target.y0)
 
                healthRect:setReferencePoint(display.TopLeftReferencePoint)
                healthRect.x = target.x + (target.contentWidth/2)
                healthRect.y = target.y - target.contentHeight
 
                target.x0 = x
                target.y0 = y           
        elseif "ended" == phase then
                display.currentStage:setFocus(nil)
                wings.isFocus = false
                target.x0 = nil
                target.y0 = nil
        end
end
 
wings:addEventListener("touch",moveMe)

Also on a side note.. To understand OOP a little more I am currently reading this Objective-C book so far this is what I learned

My attributes for the set of wings are:

The wings are color coded.

The wings have a health bar attached to it.

--------------------

The method I want to attach to the set of wings:

Wings most be draggable.

To answer your first question, it is entirely possible to have a method work with two different objects, as long as they're specified with passed-in parameters

Something like:

--the two targets passed in as parameters
local function moveAround (targetWing, targetBar)

targetWing move

targetBar move

end

local function onTouch(event)

--determines which objects to pass in as the targets
if event.target == wing1 or event.target ==bar1 then
dummyWing = wing1 --your first wing object
dummyBar = bar1 --your first bar object
elseif event.target == wing2 or event.target ==bar2
dummyWing = wing2
dummyBar = bar2
elseif --account for the other wings
end

moveAround(dummyWing, dummyBar)

end

Thanks that gives me a good idea of what i need to do!!

views:1662 update:2011/10/29 17:13:10
corona forums © 2003-2011