1. Game Description
A "Memory" game. Who is your turn to fight against the computer, who is turning two cards, if the two cards are the same, they will be removed, scored 2 points, you can continue to flip, if the two cards are different, change to another person. Until the end, the score is high.
The game design idea can be viewed in this article "cocos2d game state machine"
2. Lua simple object-oriented
We know that Lua is a scripting language rather than a real object-oriented language. However, object orientation seems to make the code structure more reasonable. JavaScript and Lua can simulate forward objects. There are many methods to implement the Lua object-oriented method. Of course we will not describe it here. Let's simply look at the method of inheriting Sprite. Copy the official copy.
Matchcard = Class ("match", function (spriteframename, value) matchcard. value = value // -- property return CC. sprite: createwithspriteframename ("matchcardbg.png") End) matchcard. test = true // -- Property // -- a method function matchcard: containstouchlocation (x, y) local position = cc. P (SELF: getposition () Local S = self: getcontentsize () Local touchrect = cc. rect (-s. width/2 + position. x,-s. height/2 + position. y, S. width, S. height) local B = cc. rectcontainspoint (touchrect, CC. P (x, y) return bend // -- New an object. Of course, this is different from C ++'s local card = matchcard. new () // -- call method card: containstouchlocation ()
3. Create a Lua project using Cocos code ide
I created this game with Cocos code IDE, and it was quite smooth. For more information about the API, see LUA-tests. The entire process is the same. You have never played quick cocos2d-x.
4. How to Use cocos2d Lua
The use of Cocos2d-x C ++ programming is the most comfortable, although the C ++ syntax is difficult to write, but not easy to encounter a very strange bug, that is, the Compilation speed is too slow, it is difficult to cooperate with planning, such as JS and Lua. You have defined some writing methods, and planning can directly write JS and Lua, and C ++ won't work. There is Cocos2d-x C ++ can use the latest version, porting Android and iOS is no problem. There is also Cocos2d-x C ++ feel can write higher quality code. And vs2012 is very useful.
Lua and JS are all scripting languages used to develop games much faster. Compilation is not required. After getting used to the object-oriented style of C ++, writing Lua and JS object-oriented looks strange. Debugging is sometimes more difficult than C ++. Cocos2d JS is still well written using webstorm. There is still a gap between Cocos code IDE and vs2012 and webstorm. In some aspects, Cocos is integrated into the game framework, so some aspects are relatively easy to use.
5. Download the dynamic graph and project source code
I use less Lua, and I feel that there are too many global variables. Project:
Http://www.waitingfy.com /? Attachment_id = 1291
EXE can be directly run in/runtime/Win32/matchgame.exe. XP is not supported, so win7 should be able.
Http://www.waitingfy.com/archives/1289
Cocos2d Lua getting started example: A memory game