Score Counter and Collison Help

I’ve been struggling getting my first shot at a game down and need some help. I’ve tried absolutely everything I could to get my score count to work. (cScore) I want it to count +1 every time the ball collides with the paddle. Also my paddle and ball collision right now isn’t what I want. I’ve added physics to both objects but the ball still just goes right through the paddle… whats wrong with my code?? Thanks a lot.

[lua]-----------------------------------------------------------------------------------------

– main.lua


W = display.contentWidth
H = display.contentHeight
display.setStatusBar(display.HiddenStatusBar);

require(“physics”)
physics.start()
physics.setGravity(0,0)

–physics.setDrawMode(“hybrid”)

– Define Walls
local leftWall = display.newRect (0, 0, 1, H)
local rightWall = display.newRect (W - 1, 0, W, H)
local topWall = display.newRect (0, 0, W, 1)
local bottomWall = display.newRect (0, H - 1, W, H)

– Make Walls Physical
physics.addBody (leftWall, “static”, {bounce = .1})
physics.addBody (rightWall, “static”, {bounce = .1})
physics.addBody (topWall, “static”, {bounce = .1})
physics.addBody (bottomWall, “static”, {bounce = .1})

–Background Image
backImage = display.newImage (“background_image.png”);
backImage:toBack();

– Player Score
–function scores()
cScore = 0
cScore = display.newText(cScore, 50, 0, nil, 14);
dScore = display.newText(“Score:”, 10, 3, nil, 12)
dScore:setTextColor(255,255,255)
– Drag Handler
local function onTouch(event)
local t = event.target
local phase = event.phase

if “began” == phase then
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
–t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif “moved” == phase then
–t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil)
t.isFocus = false

end
return true
end

– Create Paddle
local paddleP1 = display.newRect(15, 125, 10, 75);
physics.addBody(paddleP1, “static”, {bounce = .5, friction = 1, density = 5})
paddleP1:addEventListener(“touch”, onTouch)

– Create Ball
–local aBall = display.newCircle(175, 190, 7.5)
–physics.addBody (aBall, {bounce = 1, friction = -1, density = 1})

local xdirection,ydirection = 3,3 – Ball Speed
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
ball = display.newCircle( xpos, ypos, 8 ); – Ball size
ball:setFillColor(255,255,255,255);
physics.addBody (ball, {bounce = 1, friction = -1, density = 1})

local function animate(event)
xpos = xpos + ( 2.8 * xdirection );
ypos = ypos + ( 2.2 * ydirection );

if (xpos > display.contentWidth - 20 or xpos < 20) then
xdirection = xdirection * -1;
end
if (ypos > display.contentHeight - 20 or ypos < 20) then
ydirection = ydirection * -1;
end

ball:translate( xpos - ball.x, ypos - ball.y)
end

Runtime:addEventListener( “enterFrame”, animate )

ball.class = “ball”
paddleP1.class = “paddle”

function paddleP1:collision(e)
if (e.phase == “began”) then
print(e.target.class,“collided with”,e.other.class)
end
return true
end

paddleP1:addEventListener(“collision”)
[import]uid: 20272 topic_id: 34736 reply_id: 334736[/import]

Hi, try this.
[lua]function paddleP1:collision(e)
if (e.phase == “began”) then
print(e.target.class,“collided with”,e.other.class)
– add 1 to your score on collision
cScore.text = cScore.text + 1
– change direction as the ball hits the paddle
xdirection = xdirection * -1
end
return true
end[/lua]

burhan [import]uid: 74883 topic_id: 34736 reply_id: 138020[/import]

Works perfectly thanks! I just cScore = cScore + 1 … didn’t have .text on there. I want something similar for the leftWall variable also so that when collided with it does another action. I did something like this with no luck… What am I missing to why this wont work? Appreciate all the help guys… I’m starting to pick up on it.

EDIT: Oh ya… I also tried making leftWall a Global Variable and still no luck…

[lua]ball.class = “ball”
paddleP1.class = “paddle”

function paddleP1:collision(e)
if (e.phase == “began”) then
xdirection = xdirection * -1
cScore.text = cScore.text + 1
–print(e.target.class,“collided with”,e.other.class)
end
return true
end

paddleP1:addEventListener(“collision”)
ball.class = “ball”
leftWall.class = “wall”

function leftWall:collision(e)
if (e.phase == “began”) then
xdirection = xdirection * -1
cScore.text = cScore.text - 1
–print(e.target.class,“collided with”,e.other.class)
end
return true
end

leftWall:addEventListener(“collision”) [import]uid: 20272 topic_id: 34736 reply_id: 138086[/import]

I switched some things and orientation and got it figured out now doing that very thing but with a new Rect…

[lua]-----------------------------------------------------------------------------------------

– main.lua


W = display.contentWidth
H = display.contentHeight
display.setStatusBar(display.HiddenStatusBar);

require(“physics”)
physics.start()
physics.setGravity(0,0)

–physics.setDrawMode(“hybrid”)

– Define Walls
leftWall = display.newRect (0, 0, 1, H)
rightWall = display.newRect (W - 1, 0, 1, H)
topWall = display.newRect (0, 0, W, 1)
bottomWall = display.newRect (0, H - 1, W, H)
negWall = display.newRect (0, 450, W, H)

– Make Walls Physical
physics.addBody (leftWall, “static”, {bounce = .1})
physics.addBody (rightWall, “static”, {bounce = .1})
physics.addBody (topWall, “static”, {bounce = .1})
physics.addBody (bottomWall, “static”, {bounce = .1})
physics.addBody (negWall, “static”, {bounce = .1})

–Background Image
backImage = display.newImage (“background.png”);
backImage:toBack();

– Player Score
–function scores()
cScore = 0
cScore = display.newText(cScore, 50, 0, nil, 14);
dScore = display.newText(“Score:”, 10, 3, nil, 12)
dScore:setTextColor(255,255,255)
– Drag Handler
local function onTouch(event)
local t = event.target
local phase = event.phase

if “began” == phase then
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
t.x0 = event.x - t.x
–t.y0 = event.y - t.y

elseif “moved” == phase then
t.x = event.x - t.x0
–t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil)
t.isFocus = false

end
return true
end

– Create Paddle
local paddleP1 = display.newRect(150, H - 75, 75, 10);
physics.addBody(paddleP1, “static”, {bounce = 0, friction = 1, density = 5})
paddleP1:addEventListener(“touch”, onTouch)

– Create Ball
–local aBall = display.newCircle(175, 190, 7.5)
–physics.addBody (aBall, {bounce = .5, friction = -1, density = 1})

local xdirection,ydirection = 5,5 – Ball Speed
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
ball = display.newCircle( xpos, ypos, 8 ); – Ball size
ball:setFillColor(255,255,255,255);
physics.addBody (ball, {bounce = 1, friction = -1, density = 1})

local function animate(event)
xpos = xpos + ( 2.2 * xdirection );
ypos = ypos + ( 2.8 * ydirection );

if (xpos > display.contentWidth - 20 or xpos < 20) then
xdirection = xdirection * -1;
end
if (ypos > display.contentHeight - 20 or ypos < 20) then
ydirection = ydirection * -1;
end

ball:translate( xpos - ball.x, ypos - ball.y)
end

Runtime:addEventListener( “enterFrame”, animate )

ball.class = “ball”
paddleP1.class = “paddle”

function paddleP1:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text + 1
–print(e.target.class,“collided with”,e.other.class)
end
–return true
end

paddleP1:addEventListener(“collision”)

ball.class = “ball”
negWall.class = “wall”

function negWall:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text - 1
end
–return true
end

negWall:addEventListener(“collision”) [import]uid: 20272 topic_id: 34736 reply_id: 138094[/import]

Hi, well done.

Please remember that you are animating in pixels, meaning you move the ball around by changing the xPos and yPos. So, the 4 walls is irrelevant. Removing them and their physics bodies still work.
You need the walls and their physics bodies if you want to let the physics handle the ball movement. Example, you touch the ball and drag or throw it around. Then all physics properties like bounce, density, gravity and friction will take effect.

So, for your earlier case where you put leftWall eventListener and it does not work is because the ball is not in contact with the left wall yet but are “asked” to change direction. Move the wall inwards and you’ll see what i mean.

Happy Coding!

burhan [import]uid: 74883 topic_id: 34736 reply_id: 138186[/import]

Understood… Thank you. [import]uid: 20272 topic_id: 34736 reply_id: 138299[/import]

Hi, try this.
[lua]function paddleP1:collision(e)
if (e.phase == “began”) then
print(e.target.class,“collided with”,e.other.class)
– add 1 to your score on collision
cScore.text = cScore.text + 1
– change direction as the ball hits the paddle
xdirection = xdirection * -1
end
return true
end[/lua]

burhan [import]uid: 74883 topic_id: 34736 reply_id: 138020[/import]

Works perfectly thanks! I just cScore = cScore + 1 … didn’t have .text on there. I want something similar for the leftWall variable also so that when collided with it does another action. I did something like this with no luck… What am I missing to why this wont work? Appreciate all the help guys… I’m starting to pick up on it.

EDIT: Oh ya… I also tried making leftWall a Global Variable and still no luck…

[lua]ball.class = “ball”
paddleP1.class = “paddle”

function paddleP1:collision(e)
if (e.phase == “began”) then
xdirection = xdirection * -1
cScore.text = cScore.text + 1
–print(e.target.class,“collided with”,e.other.class)
end
return true
end

paddleP1:addEventListener(“collision”)
ball.class = “ball”
leftWall.class = “wall”

function leftWall:collision(e)
if (e.phase == “began”) then
xdirection = xdirection * -1
cScore.text = cScore.text - 1
–print(e.target.class,“collided with”,e.other.class)
end
return true
end

leftWall:addEventListener(“collision”) [import]uid: 20272 topic_id: 34736 reply_id: 138086[/import]

I switched some things and orientation and got it figured out now doing that very thing but with a new Rect…

[lua]-----------------------------------------------------------------------------------------

– main.lua


W = display.contentWidth
H = display.contentHeight
display.setStatusBar(display.HiddenStatusBar);

require(“physics”)
physics.start()
physics.setGravity(0,0)

–physics.setDrawMode(“hybrid”)

– Define Walls
leftWall = display.newRect (0, 0, 1, H)
rightWall = display.newRect (W - 1, 0, 1, H)
topWall = display.newRect (0, 0, W, 1)
bottomWall = display.newRect (0, H - 1, W, H)
negWall = display.newRect (0, 450, W, H)

– Make Walls Physical
physics.addBody (leftWall, “static”, {bounce = .1})
physics.addBody (rightWall, “static”, {bounce = .1})
physics.addBody (topWall, “static”, {bounce = .1})
physics.addBody (bottomWall, “static”, {bounce = .1})
physics.addBody (negWall, “static”, {bounce = .1})

–Background Image
backImage = display.newImage (“background.png”);
backImage:toBack();

– Player Score
–function scores()
cScore = 0
cScore = display.newText(cScore, 50, 0, nil, 14);
dScore = display.newText(“Score:”, 10, 3, nil, 12)
dScore:setTextColor(255,255,255)
– Drag Handler
local function onTouch(event)
local t = event.target
local phase = event.phase

if “began” == phase then
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
t.x0 = event.x - t.x
–t.y0 = event.y - t.y

elseif “moved” == phase then
t.x = event.x - t.x0
–t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil)
t.isFocus = false

end
return true
end

– Create Paddle
local paddleP1 = display.newRect(150, H - 75, 75, 10);
physics.addBody(paddleP1, “static”, {bounce = 0, friction = 1, density = 5})
paddleP1:addEventListener(“touch”, onTouch)

– Create Ball
–local aBall = display.newCircle(175, 190, 7.5)
–physics.addBody (aBall, {bounce = .5, friction = -1, density = 1})

local xdirection,ydirection = 5,5 – Ball Speed
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
ball = display.newCircle( xpos, ypos, 8 ); – Ball size
ball:setFillColor(255,255,255,255);
physics.addBody (ball, {bounce = 1, friction = -1, density = 1})

local function animate(event)
xpos = xpos + ( 2.2 * xdirection );
ypos = ypos + ( 2.8 * ydirection );

if (xpos > display.contentWidth - 20 or xpos < 20) then
xdirection = xdirection * -1;
end
if (ypos > display.contentHeight - 20 or ypos < 20) then
ydirection = ydirection * -1;
end

ball:translate( xpos - ball.x, ypos - ball.y)
end

Runtime:addEventListener( “enterFrame”, animate )

ball.class = “ball”
paddleP1.class = “paddle”

function paddleP1:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text + 1
–print(e.target.class,“collided with”,e.other.class)
end
–return true
end

paddleP1:addEventListener(“collision”)

ball.class = “ball”
negWall.class = “wall”

function negWall:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text - 1
end
–return true
end

negWall:addEventListener(“collision”) [import]uid: 20272 topic_id: 34736 reply_id: 138094[/import]

Hi, well done.

Please remember that you are animating in pixels, meaning you move the ball around by changing the xPos and yPos. So, the 4 walls is irrelevant. Removing them and their physics bodies still work.
You need the walls and their physics bodies if you want to let the physics handle the ball movement. Example, you touch the ball and drag or throw it around. Then all physics properties like bounce, density, gravity and friction will take effect.

So, for your earlier case where you put leftWall eventListener and it does not work is because the ball is not in contact with the left wall yet but are “asked” to change direction. Move the wall inwards and you’ll see what i mean.

Happy Coding!

burhan [import]uid: 74883 topic_id: 34736 reply_id: 138186[/import]

Understood… Thank you. [import]uid: 20272 topic_id: 34736 reply_id: 138299[/import]