코드 예시

공부·2022년 7월 23일
0
physics.start()
physics.setDrawMode( "hybrid" )
   
local background = display.newImageRect("image/ocean.png", display.contentWidth, display.contentHeight)
background.x, background.y = display.contentWidth/2, display.contentHeight/2

local ground = {}
ground[1] = display.newImageRect("image/ground.png", 192, 96)
ground[1].x, ground[1].y = 544, 670
ground[2] = display.newImageRect("image/ground.png", ground[1].width, ground[1].height)
ground[2].x, ground[2].y = 736, 670
ground[3] = display.newImageRect("image/ground.png", ground[1].width, ground[1].height)
ground[3].x, ground[3].y = 960, 470
ground[4] = display.newImageRect("image/ground.png", ground[1].width, ground[1].height)
ground[4].x, ground[4].y = 724, 232
ground[5] = display.newImageRect("image/ground.png", ground[1].width, ground[1].height)
ground[5].x, ground[5].y = 436, 334
ground[6] = display.newImageRect("image/ground.png", ground[1].width, ground[1].height)
ground[6].x, ground[6].y = 100, 270

local wall = {}
wall[1] = display.newRect(0, background.y, 30, background.height)
wall[2] = display.newRect(background.width, background.y, 30, background.height)
wall[3] = display.newRect(background.x, 0, background.width, 30)
wall[4] = display.newRect(background.x, background.height, background.width, 30)

sceneGroup:insert(background)

for i = 1, #ground do 
	physics.addBody(ground[i], "static")
	sceneGroup:insert(ground[i])
end

for i = 1, #wall do
	physics.addBody(wall[i], "static")
	sceneGroup:insert(wall[i])
end

local chest = display.newImageRect("image/chest.png", 64, 48)
local chest_outline = graphics.newOutline(1, "image/chest.png")
chest.x, chest.y = 760, 600
chest.name = "chest"

physics.addBody(chest, "static", {outline=chest_outline})
sceneGroup:insert(chest)

local arrow = {}
arrow[1] = display.newImageRect("image/arrow_left.png", 38, 64)
arrow[1].x, arrow[1].y = 900, 625
arrow[1].name = "left"
arrow[2] = display.newImageRect("image/arrow_center.png", 84, 84)
arrow[2].x, arrow[2].y = arrow[1].x+86, 625
arrow[2].name = "center"
arrow[3] = display.newImageRect("image/arrow_right.png", 38, 64)
arrow[3].x, arrow[3].y = arrow[2].x+86, 625
arrow[3].name = "right"

arrow[4] = "right"    -- 토끼의 방향 정보
   
local player = display.newImageRect("image/rabbit.png", 52, 50)

local player_outline_none = graphics.newOutline(1, "image/rabbit.png")
local player_outline_flip = graphics.newOutline(1, "image/rabbit_flip.png")
player.x, player.y = background.x, background.y+200
player.name = "player"

physics.addBody(player, {friction=1, outline=player_outline_none})
player.isFixedRotation = true 
sceneGroup:insert(player)


function arrowTab( event )
	x = player.x
	y = player.y

	if (event.target.name == "center") then
		if (arrow[4] == "left") then
			transition.to(player, {time=100, x=(x-100), y=(y-200)})
        else
            transition.to(player, {time=100, x=(x+100), y=(y-200)})
        end
    else
    	if (event.target.name == arrow[4]) then
            if (event.target.name == "left") then
               transition.to(player, {time=100, x=(x-50)})
            else
               transition.to(player, {time=100, x=(x+50)})
            end
         else
            arrow[4] = event.target.name
            player:scale(-1, 1)
            physics.removeBody(player)
            
            if (event.target.name == "left") then
               physics.addBody(player, {friction=1, outline=player_outline_flip})
               transition.to(player, {time=100, x=(x-50)})
            else
               physics.addBody(player, {friction=1, outline=player_outline_none})
               transition.to(player, {time=100, x=(x+50)})
            end
            player.isFixedRotation = true
		end
	end
end

for i = 1, 3 do
	arrow[i]:addEventListener("tap", arrowTab)
	sceneGroup:insert(arrow[i])
end


local potion = display.newImageRect("image/potion_red.png", 60, 60)
local potion_outline = graphics.newOutline(1, "image/potion_red.png")
potion.x, potion.y = 500, 590
potion.name = "potion"

physics.addBody(potion, "kinematic", {outline=potion_outline, isSensor=true})    -- isSensor를 설정하면 플레이어와 부딪혀도 튕겨나가지 않는다!
potion.isFixedRotation = true
sceneGroup:insert(potion)

local score = 0
local flag = false
function rabbit( self, event )
	if(event.phase == "ended" and flag == false) then
		if(event.other.name == "chest" or event.other.name == "potion") then
			flag = true
            if(event.other.name == "chest") then
               score = score + 100
            else 
               score = score - 100
            end
            timer.performWithDelay( 500, function()
                   physics.removeBody( event.other )
                   flag = false
            end )

            print(score)
		end
	end
end

player.collision = rabbit
player:addEventListener("collision")
profile
리액트

0개의 댓글

관련 채용 정보