Anda di halaman 1dari 21

AN INTRODUCTION TO DEVELOPING ANDROID GAMES USING COCOS2D ENGINE.

By Dibia Victor | Africa Android Challenge

Presentation Outline
What are gaming engines ? Cocos2D Origins Cocos2D for Android Developing Games using Cocos2D Android Cocos2D Android Test Environment Setup (Windows) Setting up a Sample Game (Puzzlemania) Puzzlemania Components Conclusion

What are Gaming Engines ?


Gaming engines are software frameworks which provide/simplify repeated used features in games such as transitions, animations, sound, physics, flow control, text rendering, surface rendering etc. When we set out to create games, we want to focus more on the game logic, game experience and storyline rather than dwelling on how to animate the tile across the screen. Examples of gaming engines include Adventure Game Studio (C++), Agate lib (.NET), Allegro (C), Cocos2D (Python) etc

Cocos 2D Origins
Cocos2d is an open source framework for building 2D games, demos, and other graphical/interactive applications. Originally developed using python, some of its main features include : Flow control: Manage the flow control between different scenes in an easy way Sprites: Fast and easy sprites Actions: Just tell sprites what you want them to do. Composable actions like move, rotate, scale and much more Effects: Effects like waves, twirl, lens and much more Other features include Tiled Maps, Transitions, Menus ,Text Rendering , OpenGL based and aBuilt-in Python Interpreter

Cocos 2D Origins (Contd)


Due to its success and easy of use, several versions (ports) of Cocos2D on different platforms have emerged over the years. A very famous port Cocos2d-iphone written in objective C (targeting the iOS and MAC platforms) has been used in authoring a wide range of top ranking games sold on the Apple Appstore. Other ports include Cocos2D Android written in Java for the Android platform , and Cocos2D Javascript that uses the HTML5 canvas API. Each of these ports maintain the original Cocos2D design concepts and structure.

Cocos 2D For Android


Cocos 2D for android is a port of the Cocos2D-iphone essentially written in Java. Cocos2D for android project is still a budding open source community (November 2010) and aims to port latest cocos2d-iphone code, to the android platform. Main features include : Flow control, Sprites, Maps, Transitions, Menus, Text Rendering, BSD License, OpenGL Based: Hardware Acceleration The most current version of Cocos2D for android project is managed by ZhouWeikuan and found at [1]

Developing Games using Cocos2D Android

In order to start creating cool games using the Cocos2D gaming engine, you need to download the .jar library (cocos2d-android.jar) and compile your android project using it as a referenced library. The next section provides steps on setting up a test environment and developing a simple game.

Cocos2D Android Test Environment Setup (Windows)

1.) Download the cocos2d-android library. The cocos2D android project is managed here (http://code.google.com/p/cocos2d-android-1/) and the source code as well as cocos2d-android.jar can be downloaded at the following github account (https://github.com/ZhouWeikuan/cocos2d) via the downloads button. 2.) Download Eclipse IDE for Java Developers (http://www.eclipse.org/downloads/) Extract the downloaded zip file and run eclipse.exe to launch the IDE. No installations required. 3.) Install Android SDK for Windows. Download and extract the android sdk here (http://dl.google.com/android/android-sdk_r12-windows.zip) For easy access, the preferred extraction location is C:/android-sdkwindows.

Cocos2D Android Test Environment Setup (Windows)


4.) Install the Android Development Tools (ADT) Plugin for Eclipse. Android offers a custom plugin for the Eclipse IDE, called Android Development Tools (ADT), that is designed to provide a powerful, integrated environment in which to build Android applications. ADT plugin for Eclipse can be installed using the following steps. Start Eclipse, then select Help > Install New Software....Click Add, in the top-right corner. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location: https://dl-ssl.google.com/android/eclipse/Click OK and proceed to finish and restart Eclipse.

Cocos2D Android Test Environment Setup (Windows)


5.) Configure the ADT Plugin [2] The next step is to modify your ADT preferences in Eclipse to point to the Android SDK directory: Select Window > Preferences... to open the Preferences panel .Select Android from the left panel. You may see a dialog asking whether you want to send usage statistics to Google. If so, make your choice and click Proceed. For the SDK Location in the main panel, click Browse... and locate your downloaded SDK directory (C:/androidsdk-windows if you used the recommended path in step 2 above). Click Apply, then OK. Done!. Now, you should be able to create and manage android projects in the eclipse IDE.

Cocos2D Android Test Environment Setup (Windows)


6.) Setting up an Android Virtual Device (AVD) for testing. An AVD is an emulated version of a mobile device running the android operating system and is used to test android applications. In Eclipse, choose Window > Android SDK and AVD Manager.Select Virtual Devices in the left panel.Click New.The Create New AVD dialog appears.Type the name of the AVD, such as "my_avd".Choose a target. The target is the platform (that is, the version of the Android SDK, such as 2.1) you want to run on the emulator. It is recommended to use Android 2.1 and above for cocos2d development. Detailed information on testing android applications using an actual android device such as an android tablet pc or mobile phone can be found here [3]

Cocos2D Android Test Environment Setup (Windows)


7a.) Setting Up a Sample Cocos2D Project From Eclipse, select File > New > Project. If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". If you dont see the Android folder, follow step 6 above again to install the ADT Pluging (After you create one or more Android projects, an entry for "Android XML File" will also be available.)

Cocos2D Android Test Environment Setup (Windows)


7b.) Select "Android Project" and click Next. Fill in the project details and Click Finish. The Next Steps are to add the cocos2D android library (cocos2d-android.jar) to your project in order to access its functionalities. The cocos2dandroid.jar file can be found in the libs folder of the cocos2d source file downloaded in step 1 above.

Cocos2D Android Test Environment Setup (Windows)


7c.) Right Click the HelloCocos2D Project > New > Folder In the New Folder Dialog name the folder libs. Copy the cocos2d-android.jar file and paste it in the libs folder Right Click on the HelloCocos2D Project > Properties > Java Build PathClick Add JARsNavigate to the HelloCocos2D project > Libs Select cocos2d-android.jar.Complete! You can now use cocos2D functions in your android code.

Cocos2D Android Test Environment Setup (Windows)


8.) Importing a Cocos2D Android Project in Eclipse From the main menu bar, select File > Import.... The Import wizard opens. Select General > Existing Project into Workspace and click Next. Choose either Select root directory or Select archive file and click the associated Browse to locate the directory or file containing the projects. Under Projects select the project or projects which you would like to import. Click Finish to start the import.

Cocos2D Android Test Environment Setup (Windows)


9.) Running a Cocos2D Android Project in Eclipse Now that you have either set up a cocos2D project or imported an existing project the next simple step is to run your project. Right Click on project in package Explorer Run As > Android Application

Setting up a Sample Game (Puzzlemania)


Puzzlemania is an implementation of a simple number sliding block puzzle. A canvas of tiles labeled with numbers from 1 9 is presented to the user in random order. The users task is to arrange the numbers in there ascending order from left to right by sliding each tile. The users score is judged based on the amount of time used to solve the puzzle and the number of moves used to solve the puzzle. Project files for this sample can be downloaded from http://www.vidicorp.org/labs/puzzle.zip and imported as an android project using step 8 above. The project files are commented and explanatory.

Puzzlemania Screenshot

Puzzlemania Components

Puzzle.java Main logic that controls game play, timing, tile animation and difficulty selection Utility.java Generates puzzle number sequence such that the puzzle is ALWAYS solvable CCNodeExt.java A special node object used to model each tile.
A possible extension of puzzlemania would be to develop an image slider puzzle in contrast to the current number slider puzzle.

Conclusion
Cocos2D is a powerful game engine as evidenced by notable games on the iphone platform [4] . A great way to proceed in getting familiarized with the Cocos2D engine and game development is to attempt to develop mobile versions of simple games we played as children (Ludo, snakes and ladders and other local games).

References
1.

2.

3.

4.

Cocos2d for Android Github Project https://github.com/ZhouWeikuan/cocos2d Retrieved March 1, 2012 Installing the Android Developer Tools (ADT) Plugin http://developer.android.com/sdk/eclipseadt.html#installing. Retrieved March 2, 2012 Using Hardware Devices http://developer.android.com/guide/developing/dev ice.html. Retrieved March 3, 2012 Games Using the Cocos2D for iPhone http://code.google.com/p/cocos2diphone/wiki/GamesUsingCocos2d . Retrieved March 1, 2012

Anda mungkin juga menyukai