Corona SDK Game Development Instance (I): create a user interface

Source: Internet
Author: User
Tags corona sdk

Tutorial description:

  • Tool used: Corona SDK
  • Execution difficulty: normal
  • Operation Time: 30 minutes to 60 minutes
Step 1: Application Overview


With the help of Lua and Corona SDK APIs, we will use pre-prepared image materials to create an interesting little game.

Players need to use the gyroscope of the device to control the ball to avoid obstacles and finally reach the destination. You can modify game parameters to customize the content.

Step 2: Target Development Platform

First, we need to select the operating platform that the application works rely on. After confirming this, we can select the image display size that matches the device.

The specific parameters of the iOS system are as follows:

  • IPad: 1024x768 resolution, 132ppi

  • IPhone/iPodTouch: 320x133 resolution, 163 ppi

  • IPhone4: 960x640 resolution, 326 ppi

Because of the open features of the Android platform, we need devices with different parameters and resolutions. Here we select several popular products as the main reference objects:

  • Google NexusOne: 254 x800 resolution, ppi

  • Motorola DroidX: 854xlarge resolution, 228 ppi

  • HTC Evo: 217 x800 resolution, ppi

In this guide, we primarily design Images Based on iOS platforms, especially iPhone/iPod. However, the code used below applies theoretically to Corona SDK development on Android systems.

Step 3: User Interface

A concise and friendly user interface will help our application work smoothly open the market. In the compass application, the main elements of the user interface include the background pattern and pointer graphics.

All the graphic resources involved in this Guide are summarized in the compressed package. You can click the following link to obtain and use the resources.

Download link: https://mobiletuts.s3.amazonaws.com/Corona-SDK_Compass/source.zip

Step 4: Export Images

Based on the selected device platform, we need to export image resources in a proper PPI and size. This work is very simple and can be implemented by any mainstream image editing tool. You can simply process it according to your own habits. I personally use AdjustSize, which is an image preview application that comes with Mac OS X. After the export is complete, remember to give the file a clear and accurate name and save it in the project folder.

Step 5: Sound

To give gamers a more pleasant gaming experience, we need to set various sound effects for events. All the audio resources involved in this instance can be found on the Soungle.com website. Search for the keyword "bell" and "buzz.

Step 6: configure the application

First, create an external file config. lua to ensure that the application runs in full screen mode on the device. This file clearly shows the original resolution of the application, and provides a scaling solution to ensure that the application can be correctly displayed under the unique resolution of different devices.

 
 
  1. application = 
  2.     content = 
  3.     { 
  4.         width = 320, 
  5.         height = 480, 
  6.         scale = "letterbox" 
  7.     }, 
Step 7: Main. lua

Good. Preparations are ready. Now let's write the application!

Open your favorite Lua editor. Any text editing tool is competent, but not all of them support the Lua syntax highlighting function.) Are you ready to write your own applications! Remember to save the file in the project folder and name it Main. lua.

Step 8: code structure

We need to organize the code in the form of classes. If you are familiar with ActionScript or Java, you will surely find that the structure I recommend basically conforms to the structural characteristics of both.

 
 
  1. Necessary Classes 
  2. Variables and Constants 
  3. Declare Functions 
  4.     contructor (Main function) 
  5.     class methods (other functions) 
  6. call Main function  
Step 9: Hide the status bar
 
 
  1. display.setStatusBar(display.HiddenStatusBar) 

This Code hides the status bar. The status bar appears on any mobile system platform. It is usually located at the top of the screen and displays the time, signal strength, and other prompts.

Step 10: import the physical Engine

To restore the real collision response, we need to use the physical effect library in the application and use the following code to import the Library to the program:

 
 
  1. local physics = require('physics') 
  2. physics.start() 
  3. physics.setGravity(0, 0) 
Step 11: Game background pattern

Since it is a little work used by the trainer, we should use the picture above as the background pattern. The following code is used to introduce an image into an application.

 
 
  1. -- Graphics 
  2. -- [Background] 
  3. local bg = display.newImage('bg.png') 
Step 12: Title View

As shown in the title view, it is the first interactive interface we face after entering the game. The content is set and saved according to the following variables.

 
 
  1. -- [Title View] 
  2. local titleBg 
  3. local playBtn 
  4. local creditsBtn 
  5. local titleView 
Step 13: production personnel View

The developer name and game copyright ownership information are shown in the following variables.

 
 
  1. -- [CreditsView] 
  2. local creditsView 
Step 14: Game View

The game view involves many elements, including players, obstacles, and destinations. Use the Code listed below to create a basic game interface.

 
 
  1. -- [Game View] 
  2. -- [Player] 
  3. local player 
  4. -- [Bars Table] 
  5. local bars = {} 
  6. -- [Holes Table] 
  7. local holes = {} 
  8. -- [Goal] 
  9. local goal 
Step 15: Sound

The following code saves the sounds used in the game.

 
 
  1. local bell = audio.loadSound('bell.caf') 
  2. local buzz = audio.loadSound('buzz.caf') 
Step 16: Code Review

The following lists all the Code outlines mentioned in this tutorial. You can check the works from a macro perspective to confirm that all elements are included in the final program:

 
 
  1. -- Teeter like Game 
  2. -- Developed by Carlos Yanez  
  3.  
  4. -- Hide Status Bar 
  5.  
  6. display.setStatusBar(display.HiddenStatusBar)  
  7.  
  8. -- Physics 
  9.  
  10. local physics = require('physics') 
  11. physics.start() 
  12. physics.setGravity(0, 0) 
  13.  
  14. -- Graphics 
  15.  
  16. -- [Background]  
  17.  
  18. local bg = display.newImage('bg.png') 
  19.  
  20. -- [Title View]  
  21.  
  22. local titleBg 
  23. local playBtn 
  24. local creditsBtn 
  25. local titleView 
  26.  
  27. -- [Credits] 
  28.  
  29. local creditsView  
  30.  
  31. -- [Player] 
  32.  
  33. local player 
  34.  
  35. -- [Bars Table] 
  36.  
  37. local bars = {} 
  38.  
  39. -- [Holes Table] 
  40.  
  41. local holes = {}  
  42.  
  43. -- [Goal] 
  44.  
  45. local goal 
  46.  
  47. -- Sounds 
  48.  
  49. local bell = audio.loadSound('bell.caf') 
  50. local buzz = audio.loadSound('buzz.caf') 
Step 17: function declaration

Declare the basic status of all functions at the beginning of the application.

 
 
  1. local Main = {} 
  2. local startButtonListeners = {} 
  3. local showCredits = {} 
  4. local hideCredits = {} 
  5. local showGameView = {} 
  6. local gameListeners = {} 
  7. local movePlayer = {} 
  8. local onCollision = {} 
  9. local alert = {} 
  10. local dragPaddle = {} 
Step 18: Game Constructor

Next, we will create an initialization Mechanism for the running logic. The details are as follows:

 
 
  1. function Main() 
  2.     -- code... 
  3. end 
Step 19: add the title View

Now we place the title view in the main interface, and call the function used to listen to the button "Touch" action.

 
 
  1. function Main() 
  2.     titleBg = display.newImage('titleBg.png') 
  3.     playBtn = display.newImage('playBtn.png', display.contentCenterX - 35.5, display.contentCenterY + 10) 
  4.     creditsBtn = display.newImage('creditsBtn.png', display.contentCenterX - 50.5, display.contentCenterY + 65) 
  5.     titleView = display.newGroup(titleBg, playBtn, creditsBtn) 
  6.       
  7.     startButtonListeners('add') 
  8. end 
Step 20: Start button listening

This function is used to add the required listener for the title View button.

 
 
  1. function startButtonListeners(action) 
  2.     if(action == 'add') then 
  3.         playBtn:addEventListener('tap', showGameView) 
  4.         creditsBtn:addEventListener('tap', showCredits) 
  5.     else 
  6.         playBtn:removeEventListener('tap', showGameView) 
  7.         creditsBtn:removeEventListener('tap', showCredits) 
  8.     end 
  9. end 
Step 21: display the developer list

When you click the corresponding button, the application will display a list of developers. In this case, you need to add an additional listener so that when the user clicks again, the program will suspend the list and return to the main interface.

 
 
  1. function showCredits:tap(e) 
  2.     playBtn.isVisible = false 
  3.     creditsBtn.isVisible = false 
  4.     creditsView = display.newImage('credits.png', 0, display.contentHeight+40) 
  5.     transition.to(creditsView, {time = 300, y = display.contentHeight-20, onComplete = function() creditsView:addEventListener('tap', hideCredits) end}) 
  6. end 
Step 22: Hide the developer list

When a user clicks the screen during the display of the developer list, the display will be interrupted in the form of animation and return to the main interface.

 
 
  1. function hideCredits:tap(e) 
  2.     playBtn.isVisible = true 
  3.     creditsBtn.isVisible = true 
  4.     transition.to(creditsView, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end}) 
  5. end 
Step 23: display the game view

When you click the "Start game" Play) button, the title view will be removed in the form of animation and display the game view.

 
 
  1. function showGameView:tap(e) 
  2.     transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end}) 
Step 24: Destination

Here we want to set a destination for the ball. In addition, we also need to set a name for it so that the scheduled event can be triggered smoothly when the ball touches the destination.

 
 
  1. -- Goal 
  2.   
  3. goal = display.newImage('goal.png') 
  4. goal.x = 439 
  5. goal.y = 31 
  6. goal.name = 'g' 
Step 25: Wall

We need to set the wall in the game interface to ensure that the ball is always active in the predefined game venue.

 
 
  1. -- Walls 
  2.       
  3.     local left = display.newLine(-1, 0, -1, display.contentHeight) 
  4.     local right = display.newLine(display.contentWidth+1, 0, display.contentWidth+1, display.contentHeight) 
  5.     local top = display.newLine(0, -3, display.contentWidth, -3) 
  6.     local bottom = display.newLine(0, display.contentHeight, display.contentWidth, display.contentHeight) 
Step 26: obstacle

These Bar obstacles are the key to improving the fun of the game. You can use the following code to implement these settings in the game.

 
 
  1. -- Bars 
  2.  
  3. local b1 = display.newImage('bar.png', 92, 67) 
  4. local b2 = display.newImage('bar.png', 192, -2) 
  5. local b3 = display.newImage('bar.png', 287, 67) 
  6. local b4 = display.newImage('bar.png', 387, -2) 
Step 27: traps

These traps are the "enemies" we designed for the ball. Once the ball touches them, the game ends.

 
 
  1. -- Holes  
  2.  
  3. local h1 = display.newImage('hole.png', 62, 76) 
  4. local h2 = display.newImage('hole.png', 124, 284) 
  5. local h3 = display.newImage('hole.png', 223, 224) 
  6. local h4 = display.newImage('hole.png', 356, 114) 
  7. local h5 = display.newImage('hole.png', 380, 256) 
  8. -- Name holes for collision detection 
  9. h1.name = 'h' 
  10. h2.name = 'h' 
  11. h3.name = 'h' 
  12. h4.name = 'h' 
  13. h5.name = 'h' 
Step 28: small ball players)

Next we will add the main character-ball in the game. With the help of the device gyroscope, the ball will naturally scroll with the player's operations.

 
 
  1. -- Player  
  2.  
  3. player = display.newImage('player.png') 
  4. player.x = 49 
  5. player.y = 288 
  6. player:setReferencePoint(display.CenterReferencePoint) 
Next Forecast

In the first part of this series of tutorials, we have discussed how to design user interfaces and basic settings for games. I hope you will continue to pay attention to the second part. At that time, we will learn how to process the logic, buttons, operations, and other details of the application. Let's see you next time!

Http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-teeter-like-game-setup-interface-creation/

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.