Create a Brick Breaker Game with the Corona SDK: Game Controls
来源:互联网 发布:记账软件onlinedown 编辑:程序博客网 时间:2024/06/06 20:39
原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Game Controls 这是系列教程的第二部分,完整内容点击下页的链接: 在第15步我们定义了保存关卡数据的多维数组,接下来我们将定义会用到的函数 接下来是主函数Main();该函数在程序启动的时候会被调用 接下来的代码片段把主界面图像添加到画布上,并且将其保存到menuScreen组 添加按钮响应函数 按钮响应函数的实现代码 下面几行代码用于隐藏主菜单界面 当玩家点击start按钮的时候,我们首先要做的是销毁菜单界面,然后再创建游戏界面 接下来我们将构建关卡,该函数在后面解释 我们在背景图上添加一个监听器,当玩家点击背景图后开始游戏 我们使用平衡仪来控制球板 我们在屏幕边缘放置不可见的边框来防止球板跑到屏幕外面去 关卡构建函数 该函数使用两层循环来检查关卡数据,在数据为1的地方放置砖块。注意在生成砖块的时候将其物理数据设置为static,这表示其不会主动做碰撞检测,在程序中只有唯一的一个dynamic对象,球,来做碰撞检测。 当球碰到球板的时候,ySpeed被设为负值,这样让其向上移动。我们同时也会检查球碰到了球板的哪一边,以决定它向哪边移动。 接下来我们将会处理砖块和墙的碰撞,以及分数的显示,测试,构建app等等。 原文地址:http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-game-controls/ 原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Game ControlsStep 16: Function Declarations
local addMenuScreen = {}
local tweenMS = {}
local hideAbout = {}
local rmvAbout = {}
local addGameScreen = {}
local buildLevel = {}
local movePaddle = {}
local gameListeners = {}
local startGame = {}
local update = {}
local bounce = {}
local removeBrick = {}
local alert = {}
local restart = {}
local changeLevel = {}
Step 17: Constructor Code
local function Main()
addMenuScreen()
end
Step 18: Add Menu Screen
function addMenuScreen()
menuScreen = display.newGroup()
mScreen = display.newImage('mScreen.png')
startB = display.newImage('startB.png')
startB.name = 'startB'
aboutB = display.newImage('aboutB.png')
aboutB.name = 'aboutB'
menuScreen:insert(mScreen)
startB.x = 160
startB.y = 260
menuScreen:insert(startB)
aboutB.x = 160
aboutB.y = 310
menuScreen:insert(aboutB)
Step 19: Button Listeners
startB:addEventListener('tap', tweenMS)
aboutB:addEventListener('tap', tweenMS)
end
Step 20: Call About Screen
function tweenMS:tap(e)
if(e.target.name == 'startB') then
-- Start Game
transition.to(menuScreen, {time = 300, y = -menuScreen.height, transition = easing.outExpo, onComplete = addGameScreen})
else
-- Call AboutScreen
aboutScreen = display.newImage('aboutScreen.png')
transition.from(aboutScreen, {time = 300, x = menuScreen.contentWidth, transition = easing.outExpo})
aboutScreen:addEventListener('tap', hideAbout)
Step 21: Hide Menu Buttons
startB.isVisible = false;
aboutB.isVisible = false;
end
end
Step 23: Remove About Screen
function hideAbout:tap(e)
transition.to(aboutScreen, {time = 300, x = aboutScreen.width*2, transition = easing.outExpo, onComplete = rmvAbout})
end
function rmvAbout()
aboutScreen:removeSelf()
-- Enable Menu Buttons
startB.isVisible = true;
aboutB.isVisible = true;
end
Step 24: Destroy Menu Screen
function addGameScreen()
-- Destroy Menu Screen
menuScreen:removeSelf()
menuScreen = nil
Step 25: Add Game Screen
-- Add Game Screen
paddle = display.newImage('paddle.png')
ball = display.newImage('ball.png')
paddle.x = 160
paddle.y = 460
ball.x = 160
ball.y = 446
paddle.name = 'paddle'
ball.name = 'ball'
Step 26: Call Build Level Function
buildLevel(levels[1])
Step 27: Scores & Levels Text
scoreText = display.newText('Score:', 5, 2, 'akashi', 14)
scoreText:setTextColor(254, 203, 50)
scoreNum = display.newText('0', 54, 2, 'akashi', 14)
scoreNum:setTextColor(254,203,50)
levelText = display.newText('Level:', 260, 2, 'akashi', 14)
levelText:setTextColor(254, 203, 50)
levelNum = display.newText('1', 307, 2, 'akashi', 14)
levelNum:setTextColor(254,203,50)
Step 28: Start Listener
background:addEventListener('tap', startGame)
end
Step 29: Move Paddle
function movePaddle:accelerometer(e)
-- Accelerometer Movement
paddle.x = display.contentCenterX + (display.contentCenterX * (e.xGravity*3))
Step 30: Paddle Border Collision
if((paddle.x - paddle.width * 0.5) < 0) then
paddle.x = paddle.width * 0.5
elseif((paddle.x + paddle.width * 0.5) > display.contentWidth) then
paddle.x = display.contentWidth - paddle.width * 0.5
end
end
Step 31: Build Level Function
function buildLevel(level)
-- Level length, height
local len = table.maxn(level)
bricks:toFront()
for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local brick = display.newImage('brick.png')
brick.name = 'brick'
brick.x = BRICK_W * j - OFFSET
brick.y = BRICK_H * i
physics.addBody(brick, {density = 1, friction = 0, bounce = 0})
brick.bodyType = 'static'
bricks.insert(bricks, brick)
end
end
end
end
Step 32: Game Listeners
function gameListeners(action)
if(action == 'add') then
Runtime:addEventListener('accelerometer', movePaddle)
--Runtime:addEventListener('enterFrame', update)
paddle:addEventListener('collision', bounce)
--ball:addEventListener('collision', removeBrick)
else
Runtime:removeEventListener('accelerometer', movePaddle)
--Runtime:removeEventListener('enterFrame', update)
paddle:removeEventListener('collision', bounce)
--ball:removeEventListener('collision', removeBrick)
end
end
Step 33: Start Game
function startGame:tap(e)
background:removeEventListener('tap', startGame)
gameListeners('add')
-- Physics
physics.addBody(paddle, {density = 1, friction = 0, bounce = 0})
physics.addBody(ball, {density = 1, friction = 0, bounce = 0})
paddle.bodyType = 'static'
end
Step 34: Paddle-Ball Collisions
function bounce(e)
ySpeed = -5
-- Paddle Collision, check the which side of the paddle the ball hits, left, right
if((ball.x + ball.width * 0.5) < paddle.x) then
xSpeed = -5
elseif((ball.x + ball.width * 0.5) >= paddle.x) then
xSpeed = 5
end
end
-- Run the Code
Main()
Next in the Series
- Create a Brick Breaker Game with the Corona SDK: Game Controls
- Create a Brick Breaker Game with the Corona SDK: Application Setup
- Create a Brick Breaker Game with the Corona SDK: Collision Detection
- UVa 11587 - Brick Game
- UVa 11875 - Brick Game
- A. Game With Sticks
- A. Game With Sticks
- POJ2996Help Me with the Game
- Help Me with the Game
- poj2996Help Me with the Game
- Help Me with the Game
- POJ2996Help Me with the Game
- Help Me with the Game
- POJ2996Help Me with the Game
- Creating a Game with CocosBuilder
- Creating a Game with CocosBuilder
- Creating a Game with CocosBuilder
- 3019. A Game with Marbles
- 编程不仅仅是decode
- USACO Fiday the thirteenth
- USACO Fiday the thirteenth
- zoj 1109
- Create a Brick Breaker Game with the Corona SDK: Application Setup
- Create a Brick Breaker Game with the Corona SDK: Game Controls
- Create a Brick Breaker Game with the Corona SDK: Collision Detection
- clamAV(clamscan)源代码调试注意事项
- dota入门
- Qt编程核心技术
- 循环队列的实现
- wpf的学习记录1
- 程序学习第九天
- fedora 14 下的网络相关设置方法