Hello guys, I’m trying to make my first game in Corona SDK, so please be patient. This is my code for a class “bird”. I create some birds using bird.newBird(). Before going to the next level I need to remove the Runtime Event Listener, otherwise director class will not work properly. How can I do that?
This is my newBird function, but how you can see it’s made out of local functions and variables, so I can’t access them externally ( like bird.removeListener() or bird[1].removeListener() for example ).
thanks for the help!
[code]
function newBird( _y, _phase, type )
– load correct sprites and animations
– depending on bird type
local _deadImage
local deadImage
if(type == 1) then
_deadImage = “bird1dead.png”
spriteSheet = sprite.newSpriteSheetFromData( “bird1.png”, data1 )
animationSpriteSet = sprite.newSpriteSet(spriteSheet, 1, 2)
sprite.add( animationSpriteSet, “flying”, 1, 2, 350, 0 )
end
if(type == 2) then
_deadImage = “bird2dead.png”
spriteSheet = sprite.newSpriteSheetFromData( “bird2.png”, data2 )
animationSpriteSet = sprite.newSpriteSet(spriteSheet, 1, 2)
sprite.add( animationSpriteSet, “flying”, 1, 2, 250, 0 )
end
if(type == 3) then
_deadImage = “bird3dead.png”
spriteSheet = sprite.newSpriteSheetFromData( “bird3.png”, data3 )
animationSpriteSet = sprite.newSpriteSet(spriteSheet, 1, 2)
sprite.add( animationSpriteSet, “flying”, 1, 2, 300, 0 )
end
local flyingSpriteSet = sprite.newSprite( animationSpriteSet );
flyingSpriteSet:prepare(“flying”)
local _bird
local birdSpeed = 200
local startingX = -250
local jumpImpulseX = 350
local jumpImpulseY = -500
local borderXright = 300
local isAlive = true
local randRightXMin = 1100
local randRightXMax = 1800
local function restartMovement()
if not isAlive then
– this means the bird was previously hit. So reset the Y position to the original one
_bird.y = _y
– and change the sprite back to flying
end
_bird.x = startingX
_bird:setLinearVelocity(birdSpeed, 0)
end
– respawn when out of display
local function checkBirdOutOfWindow( event )
if squirrel.isLaunched() then
– if the squirrel has been launched
if _bird.x < squirrel.getSquirrelInstance().x - borderXright then
startingX = squirrel.getSquirrelInstance().x + math.random(randRightXMin, randRightXMax)
deadImage.isVisible = false
flyingSpriteSet.isVisible = true
restartMovement()
end
else
– squirrel has not been launched yet
if _bird.x - squirrel.getSquirrelInstance().x > 1300 then
_bird.x = squirrel.getSquirrelInstance().x - 300
deadImage.isVisible = false
flyingSpriteSet.isVisible = true
_bird:setLinearVelocity(birdSpeed, 0)
end
end
end
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
–event.other:applyLinearImpulse(jumpImpulseX, jumpImpulseY, event.other.x, event.other.y)
local velX, velY = event.other:getLinearVelocity()
– make the squirrel jump
event.other:setLinearVelocity( velX+jumpImpulseX, jumpImpulseY )
– kill the bird
isAlive = false
_bird:setLinearVelocity(0, birdSpeed*2)
– set the correct image for the bird
deadImage.isVisible = true
flyingSpriteSet.isVisible = false
end
end
_bird = display.newGroup()
local function test()
print(“test”)
end
– create the bird
local function createBird( event )
– Set up physical properties
physics.addBody( _bird, “kinematic”, physicsData:get(“bird1flying”) )
_bird:setLinearVelocity(birdSpeed,0)
_bird.linearDamping = 0.0;
_bird.angularDamping = 0.0;
_bird.isBullet = true;
_bird.collision = onLocalCollision
_bird:addEventListener( “collision”, _bird )
layer:insert(_bird)
Runtime:addEventListener(“enterFrame”, checkBirdOutOfWindow)
end
– bird image
deadImage = display.newImage( _deadImage )
deadImage:scale(0.3, 0.3)
deadImage.isVisible = false
flyingSpriteSet.isVisible = true
_bird:insert( flyingSpriteSet )
_bird:insert( deadImage )
– resets position of the image inside the group
flyingSpriteSet.x = 0
flyingSpriteSet.y = 0
deadImage.x = 0
deadImage.y = 0
_bird.x = startingX
_bird.y = _y
_bird:scale(2,2)
_bird.name = “bird”
– create the bird with the right phase
flyingSpriteSet:play()
timer.performWithDelay(_phase, createBird)
return _bird
end
[/code] [import]uid: 76779 topic_id: 13681 reply_id: 313681[/import]
[import]uid: 52491 topic_id: 13681 reply_id: 50401[/import]