@GrahamRanson - I tried what you suggested:
if player.x > display.contentWidth-buffer then
It got rid of the error, but I notice you both are using a method called “move”. I don’t have that defined anywhere in the sample code I am working from. Is this something built in like “transition.to”? Here the error I am getting now:
sandbox_tribal/main.lua:58: attempt to call method ‘move’ (a nil value)
Here’s the code setup:
[code]
local function moveCamera ()
local buffer = 80
local speed = 2
if(p.x > display.contentWidth - buffer) then
map:move(speed,0)
end
if(p.y > display.contentHeight - buffer) then
map:move(0, - speed)
end
end
Runtime:addEventListener(“enterFrame”, moveCamera)
I feel like I am close and thank you guys for all the help. If you can just help me
-
Move th eplayer based on touch (WORKS)
-
Keep character centered in map when moves/ Have camera follow character around 2048x2048 map
-
Have map move/adjust to keep character centered when player gets to the edge of the iphone screen… Pretty much I think two would handle that. Thanks…
Here’s all the code I have so far:
[code]
local physics = require(“physics”)
–local lime = require(“lime”)
physics.start()
physics.setScale( 50 )
physics.setGravity( 0,0)
physics.setDrawMode(“hybrid”)
local eventX
local eventY
local levelMap = display.newGroup ()
–local map = display.newRect(0,0,1024,1024)
– map:setFillColor(35, 230, 100)
– levelMap:insert(map)
local map = display.newImage(“img/test.jpg”, true)
map.x = display.contentWidth / 2
map.y = display.contentHeight - 80
levelMap:insert(map)
local p = display.newRect(150, 200, 15, 15)
p:setFillColor(20, 20, 25)
physics.addBody(p, {bounce = 0.5, friction = 0.5, density = 2.0, radius = 10 })
p.name = “p”
–local function moveCamera ()
– if(p.x > 80 and p.x < 1024) then
– levelMap.x = -p.x + 20
– end
– if(p.y > 80 and p.y < 1024) then
– levelMap.y = -p.y + 20
– end
–end
–Runtime:addEventListener(“enterFrame”, moveCamera)
local function moveCamera ()
local buffer = 80
local speed = 2
if(p.x > display.contentWidth - buffer) then
map:move(speed,0)
end
if(p.y > display.contentHeight - buffer) then
map:move(0, - speed)
end
end
Runtime:addEventListener(“enterFrame”, moveCamera)
local stopP = function ()
p:setLinearVelocity(0,0)
end
–
local moveP = function ()
if t then timer.cancel(t) end
– PLayer speed
vmax = 50
dx = eventX-p.x
dy = eventY-p.y
ds = (dx*dx + dy*dy)
scalefactor = (vmax^2/ds)^0.5
vely = dy * scalefactor
velx = dx * scalefactor
traveltime = 1000 * (ds^0.5) / vmax
p:setLinearVelocity(velx, vely)
t = timer.performWithDelay(traveltime, stopP)
end
local l = function ( event )
if(event.phase == “began”) then
eventX = event.x
eventY = event.y
moveP ()
end
end
map:addEventListener(“touch”, l)
–local function onCollision(self, event)
– if(self.name == “wall” and event.other.name == “p”) then
– print(“Player has reached a wall”)
– t = timer.performWithDelay(traveltime, stopP)
– end
–end
–wall.collision = onCollision
–wall:addEventListener(“collision”, wall)
[code]
[import]uid: 53149 topic_id: 10000 reply_id: 36980[/import]