In my current assignment at college i have this code so far:
–Carz game–
local friction = 0.8
local gravity = .09
local speedX, speedY, prevX, prevY, lastTime, prevTime = 0, 0, 0, 0, 0, 0
–Hide the status bar
display.setStatusBar(display.HiddenStatusBar)
–Display The Background
local background = display.newImage (“bg.png”)
–Add score
local points = 0
scoreText = display.newText("Score: " …points, 0, 20, arial, 30)
scoreText:setTextColor(0,0,0)
ScreenW = display.contentWidth
ScreenH = display.contentHeight
scoreText.x = ScreenW/2
scoreText.y = ScreenH-30 – Centres the score centre bottom
–Physics
local physics = require “physics”
physics.start()
physics.setGravity(0, 4.95) – 4.95 m/s
physics.setDrawMode(“normal”)
–Spawn Petrol 1
local Fuel = display.newImage(“Petrol.png”)
Fuel.x=math.random(0, 320)
Fuel.y=math.random(-2000, -1000)
physics.addBody(Fuel, dynamic,{friction=2})
–Create Car Image
local myCar = display.newImage(“car.png”)
ScreenW = display.contentWidth
ScreenH = display.contentHeight
myCar.x = ScreenW/2
myCar.y = ScreenH/2 – Centres the car in the middle of the screen.
myCar.y = ScreenH/2 – Centres the car in the middle of the screen.
–Touch listener Function
function myCar:touch(event)
if event.phase ==“began” then
self.markX = self.x --Stores x location of the car
self.markY = self.y --Stores y location of the car
elseif event.phase == “moved” then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y --Moves car based on calculations above
end
return true
end
–make ‘myObject’ listen for touch events
myCar:addEventListener(“touch”, myCar)
–add function of moving car
function onMoveCar(event)
local timePassed = event.time - lastTime
lastTime = lastTime + timePassed
speedY = speedY + gravity
myCar.x = myCar.x + speedX*timePassed
myCar.y = myCar.y + speedY*timePassed
end
–Make the car not able to move off screen
local function movemyCar()
if myCar.x<40 then
myCar.x=40
end
if myCar.x>280 then
myCar.x=280
end
myCar.x = myCar.x + motionX
myCar.y= myCar.y + motionY
end
–Dont Allow the car to pass the border of the screen
local function wrap (event)
if myCar.x<0 then
myCar.x=0
end
if myCar.x>ScreenW then
myCar.x=330
end
if myCar.y>ScreenH then
myCar.y=480
end
if myCar.y<0 then
myCar.y=0
end
end
Runtime:addEventListener(“enterFrame”, wrap)
– add “name” to fuel and car
Fuel.name = “Fuel”
myCar.name=“myCar”
–Collision event function
function onCollision (event)
if event.target1.name==“myCar” and event.target2.name == “Fuel” then
print(“HIT”)
event.target2:removeSelf()
event.target2 = nil
points = points + 20
end
end
myCar:addEventListener(“collision”, onCollision)
When myCar and Fuel collide nothing happens. I then tried putting print(“HIT”) this allowed me to find out that the collision is not being registered.
Any solutions?
Love,
Dan. [import]uid: 223003 topic_id: 35676 reply_id: 335676[/import]