2015년 6월 26일 금요일

corona sdk 에서 tiled 사용하기 12 – Collectible Items


https://github.com/anthonymoralez/lime-tutorials/tree/master/tutorial-12

NOTE: This tutorial is copied from the original at OutlawGameTools.com

12 – Collectible Items (획득 가능한 아이템)

Difficulty: Beginner
Duration: 15 minutes
Description:
This tutorial is an extension to the previous tutorial so if you haven’t done that one yet please do so now. In this tutorial we will see how easy it is to give our player some basic animations. This is not the only way this can be achieved, this is just the way I am showing here, if you have a better way please email me.

Step 1: Getting your map

We will be using the exact same map we created in the last tutorial, so go ahead and make of a copy of that project now as we need the code as well.
이전 맵 사용하면 됩니다.
The Map
You can download the map here.

Step 2: Creating the item properties (아이템 프로퍼티 생성)

To get pickups to work we will be using custom properties, please remember that you can choose to go about tasks such as this any way you wish and this solution is just one of many.
The following image shows the item properties that you will need on your collectible tile. For this tutorial I have added these properties to a block tile from the standard tileset however you may want to create an animated tile such as a coin or ring for your game.
아래 프로퍼티를 보고 item을 사용할 tile에 해당 프로퍼티 작업을 합니다.
Item Properties
As you can see I have the now basic physics properties to make it a static body however I have also added an “isSensor” property, this is built in Corona/Box2D property that essentially turns your body into a ghost allowing objects to pass through it yet still register collisions. Your tile doesn’t require this property however without it your player will bounce off it, which may be a desired effect if you are going for a Mario style coin brick, however for this tutorial we will keep things simple.
The only other properties needed are our custom ones that define our pickup item, however please remember these tutorials are only showing one possible solution and you may wish to define your pickups (and other items) in a completely different way and could also include animations etc.
isSensor프로퍼티를 추가합니다. 해당 프로퍼티는 corona box2d에서 충돌 감지만 할 뿐 실제로 밀려나거나 하는 동작은 하지 않습니다.

Step 3: Adding the code(코드 넣기)

The code to get this working is very simple, all you need to do is add another check in your onCollision event that was set up for the player to control jumping.
아래코드를 충돌 핸들러에 추가합니다. 
if event.other.IsPickup then
    -- Deal with your collectible here
end
The following shows a very simple way of dealing with your score pickup.
if event.other.IsPickup then
  local item = event.other
  local onTransitionEnd = function(s) 
    return function(evt)
      evt:removeSelf()
    end
  end
  -- fade out the item
  transition.to(item, {time = 500, alpha = 0, onComplete=onTransitionEnd("item")})
  if item.pickupType == "score" then
    local text = display.newText( item.scoreValue .. " Points!", 0, 0, "Helvetica", 50 )
    text:setFillColor(0, 0, 0, 255)
    text.x = display.contentCenterX
    text.y = text.height / 2
    transition.to(text, {time = 1000, alpha = 0, onComplete=onTransitionEnd("text")})
  end
end
All fairly simple stuff, all it does is fade out the item, making sure to remove it from the world when the transition is complete, and then display some text based on the score value which also gets removed at the end. In a real game you would also want to add this score value to a player score variable stored somewhere.
먹은 아이템은 페이드 아웃한 뒤 마지막에 삭제합니다. 

Step 4: Run your game

If you run your game now you will be able to collect some items. Naturally no score actually gets saved but that can come in a later tutorial.
Complete
Resources:
Completed Project: git clone https://github.com/anthonymoralez/lime-tutorials

댓글 없음:

댓글 쓰기