Hi,
I am trying to build the foundations for a slot car style racing game, I am having trouble though getting the display area to follow the player when it moves off screen. I can’t move a display group as the co-ordinates for the car to follow are fixed, so can’t be moved. My code is below, any help appreciated.
I apologise for the code, its probably not the best way of doing what I want, but it is working.
Thanks
[code]
point = {}
local i = 1
currentLoc = 1
previousX = 0
previousY = 0
function straight()
local track = display.newRect(0,50,150,50)
track.alpha = 1
track.id = “track”
markerX = track.x - track.width * 0.5
for i = currentLoc,200,1 do
point[i] = {}
point[i].x = markerX + track.width / 100
point[i].y = track.y
print ("X: " … point[i].x)
print ("Y: " … point[i].y)
markerX = point[i].x
currentLoc = currentLoc + 0.5
print(currentLoc)
previousX = point[i].x
previousY = point[i].y
print ("previous x: " … previousX)
end
end
timer.performWithDelay(10,straight,1)
function corner()
local track = display.newRect((previousX) - previousX * 0.5,previousY,50,150)
track.alpha = 1
track.id = “track”
track.rotation = 155
markerY = track.y - track.height * 0.5
markerX = track.x - track.width * 0.5
for i = currentLoc,currentLoc + 100,1 do
point[i] = {}
point[i].x = markerX + 1
point[i].y = markerY + track.height / 100
print ("X1: " … point[i].x)
print ("Y1: " … point[i].y)
markerY = point[i].y
markerX = point[i].x
currentLoc = currentLoc + 1
print(currentLoc)
previousX = point[i].x
previousY = point[i].y
print ("previous x: " … previousX)
end
end
timer.performWithDelay(25,corner,1)
function straight1()
local track = display.newRect(previousX,previousY,50,150)
track.x = previousX
track.alpha = 1
track.id = “track”
markerY = track.y - track.height * 0.5
for i = currentLoc,currentLoc + 100,1 do
point[i] = {}
point[i].x = track.x
point[i].y = markerY + track.height / 100
print ("X2: " … point[i].x)
print ("Y2: " … point[i].y)
markerY = point[i].y
currentLoc = currentLoc + 1
print(currentLoc)
previousX = point[i].x
previousY = point[i].y
print ("previous x: " … previousX)
end
end
timer.performWithDelay(50,straight1,1)
function corner1()
local track = display.newRect(previousX,previousY,50,150)
track.alpha = 1
track.id = “track”
track.rotation = 155
markerY = track.y - track.height * 0.5
markerX = track.x - track.width * 0.5
for i = currentLoc,currentLoc + 100,1 do
point[i] = {}
point[i].x = markerX + 1
point[i].y = markerY + track.height / 100
print ("X1: " … point[i].x)
print ("Y1: " … point[i].y)
markerY = point[i].y
markerX = point[i].x
currentLoc = currentLoc + 1
print(currentLoc)
previousX = point[i].x
previousY = point[i].y
print ("previous x: " … previousX)
end
end
timer.performWithDelay(65,corner1,1)
function corner2()
local track = display.newRect(previousX,previousY,50,75)
track.alpha = 1
track.id = “track”
track.rotation = 125
markerY = track.y - track.height * 0.5
markerX = track.x - track.width * 0.5
for i = currentLoc,currentLoc + 50,1 do
point[i] = {}
point[i].x = markerX + 2
point[i].y = markerY + track.height / 100
print ("X1: " … point[i].x)
print ("Y1: " … point[i].y)
markerY = point[i].y
markerX = point[i].x
currentLoc = currentLoc + 1
print(currentLoc)
previousX = point[i].x
previousY = point[i].y
print ("previous x: " … previousX)
end
end
timer.performWithDelay(65,corner2,1)
function straight2()
local track = display.newRect(previousX,previousY,150,50)
track.y = previousY
track.alpha = 1
track.id = “track”
markerX = track.x - track.width * 0.5
for i = currentLoc,currentLoc + 100,1 do
point[i] = {}
point[i].x = markerX + track.width / 100
point[i].y = track.y
print ("X: " … point[i].x)
print ("Y: " … point[i].y)
markerX = point[i].x
currentLoc = currentLoc + 0.5
print(currentLoc)
previousX = point[i].x
previousY = point[i].y
print ("previous x: " … previousX)
end
end
timer.performWithDelay(75,straight2,1)
function createCar()
car = display.newCircle(0,100,30)
car:setFillColor(255,0,0)
car.speed = 0
–physics.addBody(car,“kinematic”,{ isSensor = true })
function set()
if i <= #point then
car.x = point[i].x
car.y = point[i].y
i=i + car.speed
–print("point: " … i)
end
end
timer.performWithDelay(10,set,0)
local button = display.newRect(100,500,100,100)
function button:touch(event)
if event.phase == “began” then
local function increaseSpeed()
car.speed = car.speed + 1
end
timer.performWithDelay(10,increaseSpeed,3)
end
end
button:addEventListener(“touch”,button)
end
timer.performWithDelay(1000,createCar,1)
[/code] [import]uid: 8699 topic_id: 35561 reply_id: 335561[/import]