Friday, May 18, 2012

Corona의 Balloon Pop Sample 분석

init() 에서는 스프라이트를 만들고 타이머 이벤트를 등록한다.
 local function init()  
      ...  
      explosionSpriteSheet = sprite.newSpriteSheet("BigExplosion.png", 82, 117)  
      explosionSet = sprite.newSpriteSet(explosionSpriteSheet, 1, 18)  
      sprite.add(explosionSet, "default", 1, 18, 200, 1)  
   
      popSound = audio.loadStream("pop.wav")       
      Runtime:addEventListener("enterFrame", onTick) -- startBalloons()  
 end  

타이머 이벤트에서는 풍선을 생성시켜준다.
 function onTick()  
      if(balloonsPerLevel > 0) then  
           if(currentBalloonsShown < balloonsShownPerLevel) then  
                addBalloon()  
           end  
      end  
 end  

풍선에는 터치할 때 반응하기 위한 이벤트가 붙는다.
 function addBalloon()  
      currentBalloonsShown = currentBalloonsShown + 1  
      balloonsPerLevel = balloonsPerLevel - 1  
      local balloon = getNewBalloon()  
      balloon:addEventListener("touch", onTouch)  
      balloon.y = display.contentHeight + balloon.contentHeight  
      balloon.x = math.random(display.contentWidth)  
      local tween = transition.to(balloon, {time=5000, y=-100, onComplete=onBalloonEscaped})  
      currentBalloons[balloon] = {balloon=balloon, tween=tween}  
      return balloon  
 end  
   
 function onTouch(event)  
      local balloon = event.target  
      balloon.isVisible = false  
      local explosion = newExplosionSprite()  
      explosion.x = balloon.x  
      explosion.y = balloon.y  
      explosion:addEventListener("end", onBoomEnd)  
      explosion:play()  
      removeBalloon(balloon)  
      audio.play(popSound)  
 end  

풍선이 터치되었을 때 터지는 효과를 위한 스프라이트 생성
 function newExplosionSprite()  
      local explosion = sprite.newSprite(explosionSet)  
      explosion:prepare("default")  
      explosion.isHitTestable = false  
      return explosion  
 end  

오류
사실 샘플 코드에는 오류가 있는데 그것은 onBoomEnd()가 절대로 불리지 않는다는 점이다.
다음과 같이 고쳐주면 된다. (참고문헌 : spriteInstance:addEventListener())
 local function onBoomEnd(event)  
      if(event.phase == "end") then  
           print("onBoomEnd")  
           event.target:removeSelf()  
      end  
 end  
   
 function onTouch(event)  
      local balloon = event.target  
      balloon.isVisible = false  
      local explosion = newExplosionSprite()  
      explosion.x = balloon.x  
      explosion.y = balloon.y  
      print('explosion:addEventListener("~", onBoomEnd)')  
      explosion:addEventListener("sprite", onBoomEnd)  
      explosion:play()  
      removeBalloon(balloon)  
      audio.play(popSound)  
 end  
--------------------------------------------------------------------------
함수별

init()
sprite.newSpriteSheet()
sprite.newSpriteSet()
sprite.add()

startBalloons()
Runtime
Runtime:addEventListener()
The Corona Event Model explained

newExplosionSprite()
sprite.newSprite()
spriteInstance:prepare()

getNewBalloon()
display.newImage()

addBalloon()
object:addEventListener()

onTouch(event)
spriteInstance:addEventListener()

removeBalloon(balloon)
transition.cancel()
object:removeEventListener()

--------------------------------------------------------------------------
스프라이트 애니메이션
Advanced Animation Using Sprites

참고 : Corona의 event (중요!)

No comments:

Post a Comment