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]