Create a Brick Breaker Game with the Corona SDK: Collision Detection
来源:互联网 发布:记账软件onlinedown 编辑:程序博客网 时间:2024/06/07 05:49
原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Collision Detection 这是系列教程的第二部分,完整内容点击下页的链接: 当球碰到砖块的时候,我们将使用与球板碰撞相同的方法来检查向哪边运行。代码如下: 当球碰到砖块的时候,砖块会消失 每碰到一个砖块将获得100分 当砖块全部被消掉的时候,玩家获胜 球的移动通过xSpeed和ySpeed变量来控制,当update函数执行的时候,球将会开始移动 下面的代码检查球与墙的碰撞,并且当发生碰撞的时候给球一个反的移动方向 调用Main()函数来开始程序 添加Default.png到工程目录,Corona编译器会自动将其添加为Loading Screen 注意不同平台需要的icon大小不一样 使用Corona模拟器打开项目所在目录即可进行测试 从Corona模拟器的File菜单中选择Build即可开始编译 原文地址:http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-collision-detection/ 原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Collision DetectionStep 35: Detect Brick Collisions
function
removeBrick(e)
-- Check the which side of the brick the ball hits, left, right
if
(e.other.name ==
'brick'
and (ball.x + ball.width * 0.5) < (e.other.x + e.other.width * 0.5)) then
xSpeed = -5
elseif(e.other.name ==
'brick'
and (ball.x + ball.width * 0.5) >= (e.other.x + e.other.width * 0.5)) then
xSpeed = 5
end
end
Step 37: Remove Brick
-- Bounce, Remove
if
(e.other.name ==
'brick'
) then
ySpeed = ySpeed * -1
e.other:removeSelf()
e.other = nil
bricks.numChildren = bricks.numChildren - 1
Step 38: Add Score
score = score + 1
scoreNum.text = score * SCORE_CONST
scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
scoreNum.x = 54
end
Step 39: Check Bricks
if
(bricks.numChildren < 0) then
alert(
' You Win!'
,
' Next Level ›'
)
gameEvent =
'win'
end
end
Step 40: Ball Movement
function
update(e)
-- Ball Movement
ball.x = ball.x + xSpeed
ball.y = ball.y + ySpeed
Step 41: Wall Collision
if
(ball.x < 0) then ball.x = ball.x + 3 xSpeed = -xSpeed end--Left
if
((ball.x + ball.width) > display.contentWidth) then ball.x = ball.x - 3 xSpeed = -xSpeed end--Right
if
(ball.y < 0) then ySpeed = -ySpeed end--Up
Step 42: Loss Condition
if
(ball.y + ball.height > paddle.y + paddle.height) then alert(
' You Lose'
,
' Play Again ›'
) gameEvent =
'lose'
end--down/lose
Step 43: Game Status Message
function
alert(t, m)
gameListeners(
'remove'
)
alertBg = display.newImage(
'alertBg.png'
)
box = display.newImage(
'alertBox.png'
, 90, 202)
transition.from(box, {time = 300, xScale = 0.5, yScale = 0.5, transition = easing.outExpo})
titleTF = display.newText(t, 0, 0,
'akashi'
, 19)
titleTF:setTextColor(254,203,50)
titleTF:setReferencePoint(display.CenterReferencePoint)
titleTF.x = display.contentCenterX
titleTF.y = display.contentCenterY - 15
msgTF = display.newText(m, 0, 0,
'akashi'
, 12)
msgTF:setTextColor(254,203,50)
msgTF:setReferencePoint(display.CenterReferencePoint)
msgTF.x = display.contentCenterX
msgTF.y = display.contentCenterY + 15
box:addEventListener(
'tap'
, restart)
alertScreen = display.newGroup()
alertScreen:insert(alertBg)
alertScreen:insert(box)
alertScreen:insert(titleTF)
alertScreen:insert(msgTF)
end
Step 44: Restart
function
restart(e)
if
(gameEvent ==
'win'
and table.maxn(levels) > currentLevel) then
currentLevel = currentLevel + 1
changeLevel(levels[currentLevel])--next level
levelNum.text = tostring(currentLevel)
elseif(gameEvent ==
'win'
and table.maxn(levels) <= currentLevel) then
box:removeEventListener(
'tap'
, restart)
alertScreen:removeSelf()
alertScreen = nil
alert(
' Game Over'
,
' Congratulations!'
)
gameEvent =
'finished'
elseif(gameEvent ==
'lose'
) then
changeLevel(levels[currentLevel])--same level
elseif(gameEvent ==
'finished'
) then
addMenuScreen()
transition.from(menuScreen, {time = 300, y = -menuScreen.height, transition = easing.outExpo})
box:removeEventListener(
'tap'
, restart)
alertScreen:removeSelf()
alertScreen = nil
currentLevel =
scoreText:removeSelf()
scoreText = nil
scoreNum:removeSelf()
scoreNum = nil
levelText:removeSelf()
levelText = nil
levelNum:removeSelf()
levelNum = nil
ball:removeSelf()
ball = nil
paddle:removeSelf()
paddle = nil
score = 0
end
end
Step 45: Change Level
function
changeLevel(level)
-- Clear Level Bricks
bricks:removeSelf()
bricks.numChildren = 0
bricks = display.newGroup()
-- Remove Alert
box:removeEventListener(
'tap'
, restart)
alertScreen:removeSelf()
alertScreen = nil
-- Reset Ball and Paddle position
ball.x = (display.contentWidth * 0.5) - (ball.width * 0.5)
ball.y = (paddle.y - paddle.height) - (ball.height * 0.5) -2
paddle.x = display.contentWidth * 0.5
-- Redraw Bricks
buildLevel(level)
-- Start
background:addEventListener(
'tap'
, startGame)
end
Step 46: Call Main Function
Main()
Step 47: Create the Loading Screen
Step 48: Icon
Step 49: Testing in the Simulator
Step 50: Build
- Create a Brick Breaker Game with the Corona SDK: Collision Detection
- Create a Brick Breaker Game with the Corona SDK: Game Controls
- Create a Brick Breaker Game with the Corona SDK: Application Setup
- collision detection
- 翻译:Panda3D Manual/V. Programming with Panda/T. Collision Detection
- CSMA/CD carrier sense multiple access with collision detection
- The disk is offline because it has a signature collision with another disk that is online
- 4: Create A Virtualenv( Challenge: Working with the Command Line)
- AoC Collision detection & Sounds
- AoC Collision detection & Sounds
- Advanced Collision Detection Techniques
- Scene Management --- Collision Detection
- Collision Detection Overview
- Collision Detection Tutorial
- Collision Detection - Parallel Computing
- Parallel Collision Detection
- ACM Collision detection
- Sicily 1907. Collision Detection
- 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 下的网络相关设置方法
- first blog