I was reading about sensors and thanks to Peach I got a better grip of how to use them.
Peach talks about the sensors at the link below:
Link here http://techority.com/2011/08/31/lets-talk-about-sensors/
So now I am working on a specific aspect of my game, and I need a little help.
The ball is draggable, when it goes touches the sensor the box fades out, when the ball leaves the sensor area it fades back in, concept is simple - the code below works, but now I want to clean it up and make it more efficient (cuz I think it could be better
)
Right now, the only way I could get this to work was to put 2 sensors and 2 functions to handle the began and ended phases.
What I want to accomplish:
-
I would like a way to consolidate this down so I have 1 sensor, and 1 function that handles the began and ended in the same line.
-
I would also like to make the fadeSensor function local (when I tried to make it local, it gives black screen in simulator).
I’ve been reading the lua reference manual 5.1, and lua programming but I am not “getting” the whole if, when, or , if else, etc. I can get things to work when I make a separate function for everything and call it - but I would like to learn how to make it “cleaner” or more efficient!
Thanks!
Here is the code (from Peach, and I changed a bunch of names around and added additional stuff )
ng
[code]
display.setStatusBar(display.HiddenStatusBar)
–activate multitouch
system.activate( “multitouch” )
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
physics.setPositionIterations(32)
local _W = display.contentWidth
local _H = display.contentHeight
–game stuff
local game = display.newGroup()
game.x = 0
game.y = 0
–the main group
local main = display.newGroup()
main.x = 0
main.y = 0
–powerups, or pu (don’t worry doesn’t stink 
local pu = display.newGroup()
pu.x = 0
pu.y = 0
local ball = display.newCircle( 40, 40, 30 )
ball.name = “ball”
physics.addBody(ball, “dynamic”)
main:insert (ball)
local function moveball (event)
ball.x = event.x
ball.y = event.y
end
ball:addEventListener(“touch”, moveball)
–Box - this will disapear when the fadeSensor is tripped
–It will reappear when fadeSensor is no longer being touched
local box = display.newRect(383,63,150,150)
game:insert (box)
physics.addBody(box ,“static”)
local fadeSensor = display.newCircle( _W/2, _H/2, 40 )
pu:insert (fadeSensor)
physics.addBody(fadeSensor, {isSensor = true})
fadeSensor.alpha = 0.2
local fadeSensor2 = display.newCircle( fadeSensor.x, fadeSensor.y, 40 )
pu:insert (fadeSensor2)
physics.addBody(fadeSensor2, {isSensor = true})
fadeSensor2.alpha = 0.2
function fadeSensor:collision(event)
if event.other.name == “ball” and event.phase == “began” then
transition.to( game, { time=500, alpha=.25 } )
print (“began:box is now faded alpha .25%”)
end
end
fadeSensor:addEventListener(“collision”, fadeSensor)
function fadeSensor2:collision(event)
if event.other.name == “ball” and event.phase == “ended” then
transition.to( game, { time=500, alpha= 1 } )
print (“ended:box is now alpha 1”)
end
end
fadeSensor:addEventListener(“collision”, fadeSensor2)
[/code] [import]uid: 61600 topic_id: 16242 reply_id: 316242[/import]