Dahai teaches you how to build the environment and encapsulate the scenario in the first season of 5cocoslua, 2015cocoslua_01

Source: Internet
Author: User

Dahai teaches you how to build the environment and encapsulate the scenario in the first season of 5cocoslua, 2015cocoslua_01

Course address: http://ke.qq.com/cgi-bin/courseDetail? Course_id = 78017

Cocos2d-x live course exchange group: 461039382 (publish videos and materials for each course)

Network Disk Data download: http://pan.baidu.com/s/1dDvZvAp (password to enter the group to have)

Cocos is a cross-platform mobile game engine that uses graphic processing and interactive events on different platforms

C ++ implements a unified Api. For details, see:


In the middle of the blue part of the Cocos2d-x is through C ++ call ios, Android and other platform graphics library OpenGL, at the same time the platform of User Interaction Events passed to the Cocos2d-x director class, in this way, developers can use C ++ Api on the basis of Cocos2d-x to develop cross-platform mobile games and applications.

Of course, developers can select three languages

1. Use C ++

A) Rigorous syntax and efficient code

B) convenient customization of function calls for the underlying system platform (C ++ can call OC and C ++ can call Java)

C) Isn't it a good idea to tell others that you will know C ++!

2. Use Lua

A) simple code and weak language

B) Flexible code structure

C) efficient development

D) can be dynamically updated (this is the most difficult to update without reinstalling the App)

3. Use JavaScript

A) It can be used by web developers.

B) Publish the game to the H5 browser.

Okay! Starting from Lua, we should first get started and then be proficient. It is a good choice to get started with Lua. Starting from C ++, it is difficult for developers.

The first lesson should solve the following problems:

1. How to Build a Development Environment

A) install jdk1.7 first.

B) install Python2.7 again (the later version cannot be installed)

C) install CocosCode IDE (1.2 and 2.0 are not prompted. Let's talk about it later)

D) install Cocos2d-x3.2 source code (later will talk about cocos2d-x3.6 and Quick, if it is 3.6 MVC is estimated that the user will be circled, do not know how the engine team think, I think this MVC affects the user's unified understanding of CocosApi. It should be used as an optional package and should not be used as the default template)

E) Configure CocosCodeIDE

For example, you can download related resources from a Network Disk:

2. core concepts of Cocos

A) Director (controls display and switching of all interfaces, including event distribution and garbage processing)

B) Scene (a game interface, each interface can have many layers)

C) layer Node (each drawing element in a scenario, such as text, image, menu, and animation, is a layer)

D) Action (encapsulation of Node transformation)

For example:


Playing a game is to define multiple scenes and then implement screen switching through Director.

3. How to understand the file structure of the first project

A) The Res folder stores all the resource clips, sounds, animations, and level files in the game.

B) Src is the source code folder that saves the lua source code for game development.

Main. lua is an entry file. It defines a scenario and runs it. The Code is as follows:

 

Require"Cocos2d" -- reference cocos lua function library

-- Cclog

LocalCclog =Function(...)

Print(String. Format (...))

End

-- For CCLuaEngine traceback

Function_ G1_trackback __(Msg)

Cclog ("----------------------------------------")

Cclog ("lua error :"..Tostring(Msg) .. "\ n ")

Cclog (Debug. Traceback ())

Cclog ("----------------------------------------")

ReturnMsg

End

LocalfunctionMain () -- entry function

Collectgarbage("Collect ")

-- Avoidmemory leak

Collectgarbage("Setpause", 100)

Collectgarbage("Setstepmul", 5000)

-- Add a file search path

Cc. FileUtils: getInstance (): addSearchPath ("src ")

Cc. FileUtils: getInstance (): addSearchPath ("res ")

-- Set screen adaptationCc. Director: getInstance (): getOpenGLView (): setDesignResolutionSize (960,640, 0)

-- Createscene 1. Create a scenario

LocalMS =Require("MyScene ")

LocalMs_scene = MS: create ()

-- 2: display this scenario

IfCc. Director: getInstance (): getRunningScene ()Then

Cc. Director: getInstance (): replaceScene (ms_scene)

Else

Cc. Director: getInstance (): runWithScene (ms_scene)

End

End

LocalStatus, msg =Xpcall(Main,_ G1_trackback __)

IfnotStatusThen

Error(Msg)

End

 

4. define your own scenarios

The following is the encapsulation of the scenario class and the background and hero are displayed on the screen. The running result is as follows:

 

The Code is as follows:

1. -- Define the first scenario

2. -- Define a class MyScene to inherit from cc. Scene

3.LocalMyScene =Class("MyScene ",Function()

4.ReturnCc. Scene: create ()

5.End)

6. -- create a function

7.FunctionMyScene: create ()

8.Print("MyScene: create ")

9.LocalMS = MyScene. new ()

10. ms: addChild (ms: init ())

11.ReturnMS

12.End

13. -- Constructor

14.FunctionMyScene: ctor ()

15.Print("MyScene: ctor ")

16. -- add member attributes

17. self. winsize =Cc. Director: getInstance (): getWinSize ()

18.End

19. -- initialize the Function

20.FunctionMyScene: init ()

21.Print("MyScene: init ")

22.LocalLayer =Cc. Layer: create ()

23. -- add the background image to the scene

24.LocalSp_bk =Cc. Sprite: create ("bbg_burning_land.jpg ")

25. layer: addChild (sp_bk)

26. sp_bk: setPosition (self. winsize. width/2, self. winsize. height/2)

27. -- add the character layer to the scenario

28.LocalSp =Cc. Sprite: create ("kick07.png ")

29. layer: addChild (sp)

30. sp: setAnchorPoint (0, 1)

31. sp: setPosition (self. winsize. width/2, self. winsize. height/2)

32. sp: setRotation (40)

33. sp: setLocalZOrder (3) -- set the display sequence of the current Layer

34. -- Let the characters run the action

35. -- sp: runAction (cc. RotateBy: create (5,720 ))

36. -- add a text layer

37.LocalTxt01 =Cc. Label: createWithSystemFont ("9 Second Class", "", 40)

38. layer: addChild (txt01)

39. txt01: setPosition (self. winsize. width/2, self. winsize. height/2 + 70)

40. -- add the character Layer

41.LocalHero01 =Cc. Sprite: create ("hero01.png ")

42. hero01: setAnchorPoint (0.5, 0.5)

43. hero01: setPosition (480,320)

44. hero01: setLocalZOrder (4)

45. layer: addChild (hero01)

46. hero01: setScale (0.3) -- scale to 30% of the original size

47.ReturnLayer

48.End

49.

50.ReturnMyScene

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.