Showing posts with label Corona. Show all posts
Showing posts with label Corona. Show all posts

Saturday, June 9, 2012

pairs가 아닌 ipairs를 써야 하는 경우

다음같이 key 이름이 주어지지 않은 array 에 대해 pairs 를 하면 엉뚱한 garbage 값이 enumerate 되곤 한다.
(실제 elements 수보다 하나 더 많이 enumerate 되곤 함)
 sprites_aniLoopBnF_breakable = {   
      {["filename"]="enemyFlyGreen", ["w"]=42, ["h"]=58, ["numFrames"]=3, ["durationTime"]=2400},  
      {["filename"]="enemyFlyBlue", ["w"]=42, ["h"]=58, ["numFrames"]=3, ["durationTime"]=2400},  
      {["filename"]="df", ["w"]=100, ["h"]=106, ["numFrames"]=3, ["durationTime"]=2400}  
 }  
   
 function loadData(displayType)  
      for k, v in ipairs(sprites_aniLoopBnF_breakable) do  
           print(count .. ")", v.filename, v.w, v.h)  
           ...  
      end  
 end  

Friday, June 8, 2012

Corona SDK Memory Leak Prevention 101

Corona SDK Memory Leak Prevention 101
What is Memory Management?
Memory management in Corona is simply managing the following five things:
  1. Display objects
  2. Global variables
  3. Runtime listeners
  4. Timers
  5. Transitions
다음 코드를 main.lua 맨 밑에 넣으면 됨
 local monitorMem = function()  
   
   collectgarbage()  
   print( "MemUsage: " .. collectgarbage("count") )  
   
   local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000  
   print( "TexMem:  " .. textMem )  
 end  
   
 Runtime:addEventListener( "enterFrame", monitorMem )  
키워드 : garbage collection, collectgarbage

Corona의 event (OnTouch 등)

이벤트의 이해
Understanding Corona’s enterFrame Event (enterFrame Event에 대해 이해하기)
The Corona Event Model explained

이벤트의 종류
Events and Listeners
  • Runtime Events : "enterFrame", "orientation", "accelerometer"
  • Targeted Events : "timer"
  • Touch Events : event.name = "touch"/ event.phase = "began", "moved", "ended"
인용 : when an event occurs, the listener will be invoked and be supplied with a table representing the event. All events will have a property name that identifies the kind of event.
검색어 : addEventListener corona event name
키워드 : 터치

Thursday, May 24, 2012

Tip: Say Goodbye to Blurry Text for Good!

Tip: Say Goodbye to Blurry Text for Good!
Want a quick fix that will prevent text from being blurry on “retina” displays of any kind? Simply paste the following code at the top of your main.lua file (or put it in an external file and require-it in):

Wednesday, May 23, 2012

Corona에서 retina display 사용하는 법

config.lua 에 다음과 같이 넣는다.
 application = {  
      content = {  
           width = 320,  
           height = 480,   
           scale = "letterBox",  
           fps = 60,  
   
           imageSuffix = {  
       ["@1-5"] = 1.5, -- for Droid, Nexus One, etc.  
       ["@2x"] = 2,  -- for iPhone, iPod touch, iPad1, and iPad2  
       ["@3x"] = 3,  -- for various mid-size Android tablets  
       ["@4x"] = 4,  -- for iPad 3  
           }  
      }  
 }  
main.lua 에 다음과 같이 써준다.
 local red = display.newImageRect("red.png", 57, 63)  --> 57 x 63 은 이미지 사이즈
 red:setReferencePoint(display.TopLeftReferencePoint); --> pivot을 중앙이 아닌 좌상단으로(안해줘도 무관)
 red.x = 0  
 red.y = 0  
코드에는 image.png 를 로딩하는 걸로 했지만 시뮬레이터를 iPhone 4 로 돌리면 알아서 image@2x.png 가 로딩된다.
레퍼런스 :
Corona SDK: 03 Retina Images
Dynamic Image Resolution Made Easy
Content Scaling Made Easy
enabling retina display mode in corona sdk

Sprite Sheet의 경우
Sprite Sheet의 경우 위의 방식이 안되는데 다음과 같이 해결하는 방법이 있다.
Dynamic Retina SpriteSheets? Here's how!
I have to say I'm extremely pleased with the update if just because I can now finally add retina spritesheets to my game and thus having complete support for highres displays! Since I'm using even spaced SpriteSheets it was easy to do. If you're using custom sizes it should still be possible, but you're on your own.

Corona SDK: Creating a Scrolling Background

Corona SDK: Creating a Scrolling Background
Daniel Williams on Jul 7th 2011 with 4 comments
The Corona SDK makes it very easy to create dynamic effects with very few lines of code. Using the Corona SDK, we will create a scrolling 2D background with graphics that we create in Photoshop.

Monday, May 21, 2012

Corona Debugger

Using the Corona Debugger
I do not write bug-free Lua code (I know, shocking). Much of the time, judicious use of print() statements will be enough to help me figure out just where things have gone off the rails. But every so often, that’s not enough...

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 (중요!)

Saturday, April 28, 2012

Friday, April 27, 2012

LevelEditor for Corona SKD

Corona 용 Level Editor 의 종류
  • Corona Level Editor
  • LevelHelper
  • Lime
  • SpriteDeck
  • Lime
  • Gumbo
  • Corona SVG Level Builder
질문 : There are many Level Editor for Corona SKD, which one you recommend ?
답 : 현재는 LevelHelper가 잘 쓰이는 듯
공식 Corona Level Editor 가 release 되면 그걸 쓰는게 좋음
링크 : what Level editor you recommend

Corona SVG Level Builder

Plug-in: Drag-and-Drop Corona SDK Level Editor
Corona SVG Level Builder lets you create physics-based games, platform levels, and maps for your Corona games by simply drawing them in Inkscape, a free and open source vector-drawing program.

Corona SVG Level Builder
Corona SVG Level Builder is a set of libraries to be used in a Corona SDK game that allows you to create Corona SDK physics based games, levels and maps by just drawing in the free and open source vector drawing Inkscape.

Sprite Sheet 데이터를 로드하기 - 두가지 방법

1. Uniformly-sized frames
The following code creates a new sprite sheet from image containing uniformly-sized frames, but does not add anything to the display list.
 spriteSheet = sprite.newSpriteSheet("image.png", frameWidth, frameHeight)  

2. Zwoptex로 뽑은 frames
This second form takes as its second parameter a table that describes the frame sizes and positions in the source sprite sheet image.
 spriteSheet = sprite.newSpriteSheetFromData( "image.png", spriteData )  
관련링크 : Corona로 Sprite Sheet Animation 만들기

Sprite Sheet 데이터를 로드하는 코드

myTest.lua 에서 Sprite Sheet 데이터를 로드하는 코드
 local mySpriteSheet = sprite.newSpriteSheetFromData("myTest.png", require("myTest").getSpriteSheetData() )  

myTest.lua 의 내용 (Zwoptex 에서 생성)
 module(...)  
 -- This file is for use with CoronaSDK™ and was generated by Zwoptex (http://zwoptexapp.com/)  
 --  
 -- For more details, see http://developer.anscamobile.com/reference/sprite-sheets  
 function getSpriteSheetData()  
      local sheet = {  
           frames = {  
                {  
                     name = "myTest-1.png",  
                     spriteColorRect = { x = 20, y = 38, width = 101, height = 157 },   
                     textureRect = { x = 165, y = 159, width = 101, height = 157 },   
                     spriteSourceSize = { width = 181, height = 209 },   
                     spriteTrimmed = true,  
                     textureRotated = false  
                },  
                {  
                     name = "myTest-2.png",  
                     spriteColorRect = { x = 16, y = 52, width = 79, height = 157 },   
                     textureRect = { x = 0, y = 187, width = 79, height = 157 },   
                     spriteSourceSize = { width = 181, height = 209 },   
                     spriteTrimmed = true,  
                     textureRotated = false  
                },  
                {  
                     name = "myTest-3.png",  
                     spriteColorRect = { x = 5, y = 52, width = 107, height = 157 },   
                     textureRect = { x = 165, y = 0, width = 107, height = 157 },   
                     spriteSourceSize = { width = 181, height = 209 },   
                     spriteTrimmed = true,  
                     textureRotated = false  
                },  
                {  
                     name = "myTest-4.png",  
                     spriteColorRect = { x = 18, y = 9, width = 163, height = 185 },   
                     textureRect = { x = 0, y = 0, width = 163, height = 185 },   
                     spriteSourceSize = { width = 181, height = 209 },   
                     spriteTrimmed = true,  
                     textureRotated = false  
                },  
           }  
      }  
      return sheet  
 end  
관련링크 : Corona로 Sprite Sheet Animation 만들기

Thursday, April 26, 2012

Corona로 Sprite Sheet Animation 만들기

1. 애니메이션 제작
  1. Photoshop 에서 레이어들로 애니메이션을 만든다.
  2. pivot(anchor)이 되는 위치는 일치하도록 만듬 (중요)
  3. Layer들, 즉 각 프레임을 png 파일들로 export 함
    (전체 네 프레임이고 파일명은 myTest-1.png ... myTest-4.png 라고 하자)
2. Sheet animation 용 data 제작
  1. Zwoptex 에 그들 png 파일들을 끌어넣음
  2. 각각을 배치함. 툴바의 Layout 버튼 누르면 자동으로 되고
    수동으로 배치할 수도 있음 (이때 빨간색 bounding box가 서로 겹쳐서는 안됨)
  3. Document의 width / height 를 적절히 조절해줌
  4. 툴바의 Publish 버튼 누름 (이때 Corona의 lua 포맷 선택)
  5. myTest_default.png 와 myTest_default.lua 라는 두개의 파일이 생성됨
    각각을 myTest.png 와 myTest.lua 라는 이름으로 변경
3. Corona
  1. New Project 함 (template = Blank, 방향은 landscape로)
    프로젝트명 MyAniTest (아무거나 무관)
  2. Zwoptex에서 나온 두 파일을 프로젝트 폴더에 넣음
  3. main.lua 를 다음과 같이 작성
 require "sprite"  
   
 display.setStatusBar(display.HiddenStatusBar)  
   
 local mySpriteSheet = sprite.newSpriteSheetFromData("myTest.png", require("myTest").getSpriteSheetData() )  
 local mySpriteSet = sprite.newSpriteSet(mySpriteSheet, 1, 4)  
 sprite.add(mySpriteSet,"myTest", 1, 4, 1000, 0)  
   
 local spriteInstance = sprite.newSprite(mySpriteSet)  
 spriteInstance:setReferencePoint(display.BottomRightReferencePoint)  
 spriteInstance.x = 400  
 spriteInstance.y = 280  
   
 spriteInstance:prepare("myTest")  
 spriteInstance:play()  
   
레퍼런스 :
Corona Sample에 들어 있는 HorseAnimation
Advanced Animation Using Sprites

Wednesday, April 25, 2012

block comment in lua

--[[
]]

Corona로 Hello World 만들기

보통, 튜토리얼에는 폴더 만들어서 main.lua 파일만 넣은 후 시뮬레이터로 돌리면 된다고 하는데
그렇게 하면 글자가 보이지 않는다. 폴더를 직접 만드는 게 아니라 Corona에서 New Project 로 시작해야 함
 local myText = display.newText( "Hello, World!", 0, 0, native.systemFont, 40 )  
 myText.x = display.contentWidth / 2  
 myText.y = display.contentHeight * 0.48  
 myText:setTextColor( 255,110,110 )  
키워드 : does not work, problem, 문제, 안되는 이유

Corona SDK

공식
Corona SDK Stable Release : January 2012
http://en.wikipedia.org/wiki/Corona_(software_development_kit)

기초
Docs - Learning Corona
Lua (programming language)
Advanced Animation Using Sprites (Sprite Sheets)

튜토리얼
How To Make a Game Like Doodle Jump with Corona Tutorial Part 1
Corona SDK: Build an Endless Runner Game From Scratch!

샘플
샘플 파일들... templates (http://developer.anscamobile.com/resources/)
Community - Code Exchange (shared code repository, 완성된 게임 샘플들 있음)