Flying Geese in CoronaSDK

NOTE: This has been taken from the CoronaSDK forums and the code is sole property of Philipp Lenssen from the thread on the forums found here.

First, let us look at what does this geese animation look like, (I apologise in advance for the very poor quality of video taken by my iPhone4 recording the iPad) While I had reviewed Philipps game "Free the Fobbles" ( review here ) I made a note that it would be interesting to know how the Geese animation was achieved, they fly in a quite natural manner than a scripted one.

It was indeed a surprise when Philipp complied and wrote a nice big article on how it can be done. For those that are members of CoronaSDK, you can read it on the forums, but for those that are wondering what is all of this about? Here's the same reproduced for your understanding.


The Video

The Code

function self:createBirds()
    local function handle(self)
        self.speedX = self.speedX + misc.randomFloat(-.5, .5)
        self.speedY = self.speedY + misc.randomFloat(-.5, .5)
    
        if self.subtype == 'swarmLeader' then
    
            if misc.getChance(1) or self.targetX == nil then
                local margin = 300
                self.targetX = math.random(app.minX - margin, app.maxX + margin)
                self.targetY = math.random(750, 850)
            end
    
        else
    
            if self.phase.name == 'followSwarmLeader' then
                if misc.getChance(1) then self.phase.name = 'default' end
                if self.targetSprite == nil then
                    self.targetSprite = appGetSpriteByType('bird', 'swarmLeader')
                end
            else
                if misc.getChance(1) then self.phase.name = 'followSwarmLeader' end
                self.targetSprite = nil
            end
    
        end
    end
 
    appRemoveSpritesByType('bird')
    local maxBirds = 8
    local swarmX = math.random(app.minX + 50, app.maxX - 50)
    local swarmY = math.random(750, 850)
    for i = 1, maxBirds do
        local x = swarmX + math.random(-100, 100)
        local y = swarmY + math.random(-70, 70)
        local self = spriteModule.spriteClass('rectangle', 'bird', nil, 'bird', false, x, y, 11, 14,
                1, 6)
        self.id = 'bird' .. i
        self.speedLimitX = 2
        self.speedLimitY = 1
        self.currentFrame = 1
        self.doDieOutsideField = false
        self.speedStep = .1
 
        if i == 1 then self.subtype = 'swarmLeader'
        else self.phase:set('followSwarmLeader')
        end
 
        self.frameNameNumber['right1'] = 1
        self.frameNameNumber['right2'] = 2
        self.frameNameNumber['right3'] = 3
        self.frameNameNumber['right4'] = 2
 
        self.frameNameNumber['left1'] = 4
        self.frameNameNumber['left2'] = 5
        self.frameNameNumber['left3'] = 6
        self.frameNameNumber['left4'] = 5
 
        self.frameName = self.frameNameNumber['left1']
 
        self.alpha = .18
        self.frameSpeed = .2
        appAddSprite(self, handle, moduleGroup)
    end
end

Comments

Popular Posts