Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By : Michelle M Fernandez
Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By: Michelle M Fernandez

Overview of this book

Table of Contents (19 chapters)
Corona SDK Mobile Game Development Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – making the ball bounce against the paddle


We will check which side of the paddle the ball has hit to choose the side where it will move next. It's important to have the motion to follow through any directional hits as it would in a realistic environment. With every paddle collision, we want to make sure that the ball goes in the up direction. For this, follow these steps:

  1. Create a new function called bounce() for the ball after the movePaddle() function:

    function bounce()
  2. Add in a value of -3 for velocity in the y direction. This will make the ball move in an upward motion:

      vy = -3
  3. Check when a collision is made with the paddle and ball objects and close the function:

      if((ball.x + ball.width * 0.5) < paddle.x) then
        vx = -vx
      elseif((ball.x + ball.width * 0.5) >= paddle.x) then
        vx = vx
      end
    end

What just happened?

When the ball collides with the paddle, the motion follows through, depending on what side of the paddle is touched by the ball. In the first part...