Advertisement
Rakoonic

Camera offsetting

Sep 5th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. local physics = require("physics")
  2.     physics.start(); physics.pause()
  3.    
  4.     local _W = display.contentWidth
  5.     local _H = display.contentHeight
  6.    
  7.     -- used to change the scale as needed
  8.     local myScale = 1
  9.            
  10.     local group = display.newGroup()
  11.    
  12.     -- set the scale of the group
  13.     group.xScale = myScale
  14.     group.yScale = myScale
  15.            
  16.     -- create a moving object for the sample
  17.     local ship = display.newCircle(60, 60, 60)
  18.     ship.x = 100
  19.     ship.y = 100
  20.     physics.addBody(ship, 'dynamic')
  21.     group:insert(ship)
  22.    
  23.     -- create a lister so we can touch the ball to move it
  24.     ship.myTouchListener = function(event)
  25.    
  26.             if event.phase == 'began' then
  27.                 event.target:setLinearVelocity(400, 0)
  28.             end
  29.  
  30.         --phases(began, moved, ended)
  31.         end
  32.    
  33.     ship:addEventListener( "touch", ship.myTouchListener )
  34.    
  35.    
  36.  
  37.    
  38.     -- create ground for context
  39.     for i = 1,100 do
  40.         --local myRectangle[i] = {}
  41.        
  42.         local myRectangle = display.newRect(group, 50*i, _H - 100, 100, 100)
  43.         myRectangle:setFillColor(math.random(100, 255), math.random(100, 255), math.random(100, 255), 255)
  44.        
  45.         physics.addBody(myRectangle, 'static')
  46.     end
  47.    
  48.    
  49.    
  50.     -- todo:
  51.     -- this is the function that needs tweaking to slide the screen back depending
  52.     -- on which direction we move
  53.     local screenCenter   = display.contentWidth / 2
  54.     local offset         = 0
  55.     local camChangeSpeed = 1
  56.     local function moveCamera()
  57.            
  58.         -- check which way the ball is moving
  59.         local myX, myY = ship:getLinearVelocity() --if ship.ve
  60.        
  61.         -- Find limit by speed    
  62.         local desiredOffset = myX / 4
  63.         if desiredOffset < offset then     offset = math.max( offset - camChangeSpeed, desiredOffset )
  64.         elseif desiredOffset > offset then offset = math.min( offset + camChangeSpeed, desiredOffset ) ; end
  65.        
  66.         group.x = -ship.x + screenCenter - offset
  67.     end
  68.  
  69.     -- move the group as the ship moves
  70.     Runtime:addEventListener('enterFrame', moveCamera)
  71.  
  72.     -- begin the physics
  73.     physics.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement