Advertisement
Guest User

Perspective

a guest
Feb 17th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.27 KB | None | 0 0
  1. -----------------------------------------------------------------------------------------
  2. --
  3. -- level1.lua
  4. --
  5. -----------------------------------------------------------------------------------------
  6.  
  7. local storyboard = require( "storyboard" )
  8. local scene = storyboard.newScene()
  9.  
  10. -- include Corona's "physics" library
  11. local physics = require "physics"
  12. physics.start(); physics.pause()
  13.  
  14.  
  15.  
  16. --------------------------------------------
  17.  
  18. -- forward declarations and other locals
  19. local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
  20. local hero = {}
  21. local controls = {}
  22.  
  23. local perspective=require("perspective")
  24. local camera=perspective.createView()
  25.  
  26.  
  27. -----------------------------------------------------------------------------------------
  28. -- BEGINNING OF YOUR IMPLEMENTATION
  29. --
  30. -- NOTE: Code outside of listener functions (below) will only be executed once,
  31. --       unless storyboard.removeScene() is called.
  32. --
  33. -----------------------------------------------------------------------------------------
  34.  
  35. local function setHeroProperties()
  36.  
  37. hero.speed = 70
  38.  
  39.  
  40. end
  41.  
  42. local  function  setHeroVelocity()
  43.  
  44.     local heroHorizontalVelocity, heroVerticalVelocity = hero:getLinearVelocity()
  45.     hero:setLinearVelocity( hero.velocity, heroVerticalVelocity )
  46. end
  47.  
  48.  
  49. local function doControlsTouch( event )
  50.      if event.phase == "began" then
  51.        if event.target.id == "jump" then
  52.        print( "Jump Pressed" )
  53.         hero:applyLinearImpulse( 0, -40, hero.x, hero.y )
  54.        else
  55.          Runtime:addEventListener( "enterFrame", setHeroVelocity )
  56.        print ("Left Right Pressed")
  57.         local touchX = event.x
  58.         print ("Touch: " .. touchX)
  59.  
  60.            local middleOfControlPad = controls.x
  61.  
  62.             if touchX < middleOfControlPad then
  63.                 hero.velocity = -hero.speed
  64.             else
  65.                 hero.velocity = hero.speed
  66.             end
  67.  
  68.        end
  69.        elseif event.phase == "ended" then
  70.        if event.target.id == "jump" then
  71.           print("Jump Released")
  72.       else
  73.           print("Left Right Released")
  74.           hero.velocity = 0
  75.           Runtime:removeEventListener( "enterFrame", setHeroVelocity )
  76.  
  77.         end
  78.      end
  79. end
  80.  
  81.  
  82. -- Called when the scene's view does not exist:
  83. function scene:createScene( event )
  84.     local group = self.view
  85.  
  86.     -- create a grey rectangle as the backdrop
  87.  
  88.     local background = display.newRect( 0, 0, screenW, screenH )
  89.     background.anchorX = 0
  90.     background.anchorY = 0
  91.     background:setFillColor( .5 )
  92.    
  93.     -- Create the Hero
  94.  
  95.     hero = display.newRect ( 160, 60, 64, 96 )
  96.    
  97.     -- Controles
  98.  
  99.      controls = display.newRect( 70, 290, 96, 32 )
  100.      controls.id = "LeftRight"
  101.      controls:addEventListener( "touch", doControlsTouch )
  102.  
  103.     local btnJump = display.newCircle( 450, 290, 16 )
  104.     btnJump.id = "jump"
  105.     btnJump:addEventListener( "touch", doControlsTouch )
  106.  
  107.  
  108.     -- add physics to the hero
  109.      physics.addBody( hero, { density=1.0, friction=0.3, bounce=0 } )
  110.      setHeroProperties()
  111.    
  112.    
  113.     -- create a grass object and add physics (with custom shape)
  114.     local grass = display.newImageRect( "grass.png", screenW, 82 )
  115.     grass.anchorX = 0
  116.     grass.anchorY = 1
  117.     grass.x, grass.y = 0, display.contentHeight
  118.    
  119.     -- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see)
  120.     local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
  121.     physics.addBody( grass, "static", { friction=0.3, shape=grassShape } )
  122.  
  123.      camera:add(hero, 1, true)
  124.      camera:add(grass, 2, false)
  125.      camera:add(background, 3, false)
  126.    
  127.  
  128.      camera:setBounds(60,display.contentWidth, 0,display.contentHeight)
  129.  
  130.  
  131.    
  132.     -- all display objects must be inserted into group
  133.     group:insert( background )
  134.     group:insert( grass)
  135.     group:insert( controls )
  136.     group:insert( btnJump )
  137.     group:insert( hero )
  138.  
  139. end
  140.  
  141. -- Called immediately after scene has moved onscreen:
  142. function scene:enterScene( event )
  143.     local group = self.view
  144.     camera:track()
  145.     physics.start()
  146.  
  147.    
  148. end
  149.  
  150. -- Called when scene is about to move offscreen:
  151. function scene:exitScene( event )
  152.     local group = self.view
  153.    
  154.     physics.stop()
  155.    
  156. end
  157.  
  158. -- If scene's view is removed, scene:destroyScene() will be called just prior to:
  159. function scene:destroyScene( event )
  160.     local group = self.view
  161.    
  162.     package.loaded[physics] = nil
  163.     physics = nil
  164. end
  165.  
  166. -----------------------------------------------------------------------------------------
  167. -- END OF YOUR IMPLEMENTATION
  168. -----------------------------------------------------------------------------------------
  169.  
  170. -- "createScene" event is dispatched if scene's view does not exist
  171. scene:addEventListener( "createScene", scene )
  172.  
  173. -- "enterScene" event is dispatched whenever scene transition has finished
  174. scene:addEventListener( "enterScene", scene )
  175.  
  176. -- "exitScene" event is dispatched whenever before next scene's transition begins
  177. scene:addEventListener( "exitScene", scene )
  178.  
  179. -- "destroyScene" event is dispatched before view is unloaded, which can be
  180. -- automatically unloaded in low memory situations, or explicitly via a call to
  181. -- storyboard.purgeScene() or storyboard.removeScene().
  182. scene:addEventListener( "destroyScene", scene )
  183.  
  184. -----------------------------------------------------------------------------------------
  185.  
  186. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement