2015년 7월 3일 금요일

corona sdk 로 슈팅 게임 만들기(적 이동)


* 해야할 것 *

배경스크롤
주인공 이동
무기발사
적 이동
스테이지
점수 처리
시작처리
종료처리
적유닛 생성
이미지 사용할 것:http://m484games.ucoz.com/index/shoot_39_em_up_gfx/0-38
참고 소스:http://www.tandgapps.co.uk/resources/tutorial-space-shooter-in-240-lines/




적 이동
지난번에는 배경 스크롤에 대해서 알아보았고, 이번에 할 내용은 적 이동입니다. 하늘에서 적들이 무수히 내려옵니다. 시작하기전에 그룹에대해서 알아 보겠습니다.
그룹 설명:https://docs.coronalabs.com/guide/graphics/group.html
아래와 같이 그룹을 만들고 그룹 단위로 삭제를 할 수 있습니다.
그룹을 사용하는 이유는 관리적인 측면입니다. enemy객체가 동적으로 생성되는데 일괄적으로 관리하지 않으면 memory leak이 발생하기 때문에 필요에 의해 사용하게 되었습니다. 그리고 group으로 관리를 하니 기존 background scroll또한 그룹으로 관리해야 합니다. 안그러면 이미지 layer에 대한 처리를 할 수가 없습니다. 여러개의 그룹을 생성할때 먼저 생성한 그룹이 뒤쪽으로 가고 나중 생성그룹이 앞쪽에 display되게 됩니다. 참고해서 작업하면 됩니다.
local farBackground = display.newGroup()  
local nearBackground = display.newGroup()  --this will overlay 'farBackground'  
local foreground = display.newGroup()  --and this will overlay 'nearBackground'
실제 소스에서 아래와 같이 enemyGroup을 뒤쪽에 생성했습니다.
local bgGroup = display.newGroup()
local enemyGroup = display.newGroup()

전체적으로 설명이 쉽게 함수로 변경하였습니다.
eventLoop안에 3개의 함수 콜이 있습니다.
  checkNSpawnEnemy() 적들이 생성할 조건이 되면 생성 시키는 함수입니다.
  moveStarField() 앞에서 설명한 배경 스크롤 입니다.
  moveEnemy() 적들을 이동시킵니다.

checkNSpawnEnemy() 이번에 추가된 함수입니다.
spawnInt 값이 하나씩 증가되면서 최대 수치가 되면 적을 새로 하나 생성합니다.
이때 적의 생성은 spawnEnemy() 함수에서 합니다.
background와 다르게 physics.addBody를 사용하는 이유는 충돌 검사를 하기 위해서 사용합니다.

아래는 전체 소스 입니다.


-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
display.setStatusBar( display.HiddenStatusBar ) --Hide status bar from the beginning
local physics = require ("physics") --Require physics
physics.start(); physics.setGravity( 0, 0 ) --Start physics

-----------------------------------------------
--*** Set up our variables and group ***
-----------------------------------------------
local bgGroup = display.newGroup()
local enemyGroup = display.newGroup()

local _W = display.contentWidth
local _H = display.contentHeight

local stars1, stars2 --Background moving stars
local spawnInt = 0 --Gameloop spawn control
local spawnIntMax = 30 --Gameloop max spawn
local spawned = 0 --Keep track of enemies
local spawnedMax = 10 --Max allowed per level
local enemySpeed = 7 --How fast the enemies are
local wave = 0

local function loadBGImg()
  stars1 = display.newImageRect("star1_480_800.png", 480, 800)
  stars1.x = _W*0.5; stars1.y = _H*0.5
  bgGroup:insert(stars1)
  stars2 = display.newImageRect("star2_480_800.png", 480, 800)
  stars2.x = _W*0.5; stars2.y = _H*0.5-800
  bgGroup:insert(stars2)
end

local function setup()
  loadBGImg()
end

local function moveStarField()
  --Move the starfields.
  stars1:translate(0,2)
  stars2:translate(0,2) 
  if stars1.y >= (_H*0.5)+800 then
    stars1.y = (_H*0.5)-800
  end
  if stars2.y >= (_H*0.5)+800 then
    stars2.y = (_H*0.5)-800
  end
end

local function spawnEnemy()
  local imageInt = math.random(1,3)
  local enemy = display.newImageRect("ship_"..imageInt.."_20_20.png",20,20)

  enemy.x = math.random( 20, 480-20 ); enemy.y = -30; enemy.rotation = 90
  enemy.name = "enemy"; physics.addBody( enemy, { isSensor = true } )
  enemyGroup:insert( enemy )

  if spawned == spawnedMax then 
    wave = wave + 1 --Increase the wave.
    if wave <= 18 then --Limit max speed/spawn
      enemySpeed = enemySpeed + 1 
      spawnIntMax = math.round(spawnIntMax * 0.9)
    end
    spawned = 0 --Reset so that the next wave starts from 0
  end
  spawnInt = 0
end

local function checkNSpawnEnemy()
  --Increase the int until it spawns an enemy..
  spawnInt = spawnInt + 1
  --change spawnIntMax if you want enemies to spawn
  --faster or slower.
  if spawnInt == spawnIntMax then 
    spawnEnemy()
    spawned = spawned + 1
  end
end

local function moveEnemy()
  --Move the enemies down each frame!
  local i
  for i = enemyGroup.numChildren,1,-1 do
    local enemy = enemyGroup[i]
    if enemy ~= nil and enemy.y ~= nil then
      enemy:translate( 0, enemySpeed)
    end
  end 
end

local function gameLoop(event)
  checkNSpawnEnemy()
  moveStarField()
  moveEnemy()
end

setup()
Runtime:addEventListener ("enterFrame", gameLoop)


아래는 실행화면 입니다.



댓글 없음:

댓글 쓰기