So one of the mechanics of my game requires the balls in the game to fly off the screen to the left or right when they hit the player controlled paddle. This is just for a nice effect so the balls aren’t just bouncing straight back up to the top of the screen. I figured I would use a Force or a LinearImpulse, but neither worked. I wouldn’t be surprised if its a stupid mistake I made, I’m just not experienced enough to realize it. Anyway, all I was hoping this code would do is to make the balls fly off the side of the screen when they hit the paddle. Thanks for your time and any help is appreciated!
display.setStatusBar(display.HiddenStatusBar)
-- Load and start physics
local physics = require("physics")
physics.start()
physics.setGravity(0, 5)
local gameLayer = display.newGroup()
local bulletsLayer = display.newGroup()
local enemiesLayer = display.newGroup()
local gameIsActive = true
local scoreText
local sounds
local score = 0
local toRemove = {}
local background
local paddle
local halfpaddleWidth
local textureCache = {}
textureCache[1] = display.newImage("paddle.png"); textureCache[1].isVisible = false;
textureCache[2] = display.newImage("regBall.png"); textureCache[2].isVisible = false;
local halfballWidth = textureCache[1].contentWidth \* .5
gameLayer:insert(bulletsLayer)
gameLayer:insert(enemiesLayer)
local function onCollision(self, event)
if self.name == "paddle" and event.other.name == "ball" and gameIsActive then
--table.insert(toRemove, event.other)
score = score + 10
scoreText.text = score
--I want this force to make the soccer balls fly off the screen at an angle so they
--don't bounce right back up
ball:applyForce( 500, 2000, ball.x, ball.y )
--gameIsActive = false
end
end
paddle = display.newImage("paddle.png")
paddle.x = display.contentCenterX
paddle.y = display.contentHeight - paddle.contentHeight
physics.addBody(paddle, "kinematic", {bounce = .75})
paddle.name = "paddle"
paddle.collision = onCollision
paddle:addEventListener("collision", paddle)
gameLayer:insert(paddle)
halfpaddleWidth = paddle.contentWidth \* .5
scoreText = display.newText(score, 0, 0, "HelveticaNeue", 35)
scoreText:setTextColor(255, 255, 255)
scoreText.x = 30
scoreText.y = 25
gameLayer:insert(scoreText)
--------------------------------------------------------------------------------
-- Game loop
--------------------------------------------------------------------------------
local timeLastBullet, timeLastball = 0, 0
local bulletInterval = 1000
local function gamePlay(event)
if gameIsActive then
-- Remove collided ball planes
for i = 1, #toRemove do
toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil
end
if event.time - timeLastball \>= math.random(1000, 2000) then
ballPic = "regBall.png"
local ball = display.newImage(ballPic)
ball.x = math.random(halfballWidth, display.contentWidth - halfballWidth)
ball.y = -ball.contentHeight
physics.addBody(ball, "dynamic", {bounce = .75})
ball.name = "ball"
enemiesLayer:insert(ball)
timeLastball = event.time
end
end
end
Runtime:addEventListener("enterFrame", gamePlay)
-- Controls
local function paddleMovement(event)
if not gameIsActive then return false end
if event.x \>= halfpaddleWidth and event.x \<= display.contentWidth - halfpaddleWidth then
paddle.x = event.x
end
end
paddle:addEventListener("touch", paddleMovement)
[import]uid: 7116 topic_id: 10810 reply_id: 310810[/import]