Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------------------------
- --
- -- level1.lua
- --
- -----------------------------------------------------------------------------------------
- local storyboard = require( "storyboard" )
- local scene = storyboard.newScene()
- -- include Corona's "physics" library
- local physics = require "physics"
- physics.start(); physics.pause()
- --------------------------------------------
- -- forward declarations and other locals
- local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
- local hero = {}
- local controls = {}
- local perspective=require("perspective")
- local camera=perspective.createView()
- -----------------------------------------------------------------------------------------
- -- BEGINNING OF YOUR IMPLEMENTATION
- --
- -- NOTE: Code outside of listener functions (below) will only be executed once,
- -- unless storyboard.removeScene() is called.
- --
- -----------------------------------------------------------------------------------------
- local function setHeroProperties()
- hero.speed = 70
- end
- local function setHeroVelocity()
- local heroHorizontalVelocity, heroVerticalVelocity = hero:getLinearVelocity()
- hero:setLinearVelocity( hero.velocity, heroVerticalVelocity )
- end
- local function doControlsTouch( event )
- if event.phase == "began" then
- if event.target.id == "jump" then
- print( "Jump Pressed" )
- hero:applyLinearImpulse( 0, -40, hero.x, hero.y )
- else
- Runtime:addEventListener( "enterFrame", setHeroVelocity )
- print ("Left Right Pressed")
- local touchX = event.x
- print ("Touch: " .. touchX)
- local middleOfControlPad = controls.x
- if touchX < middleOfControlPad then
- hero.velocity = -hero.speed
- else
- hero.velocity = hero.speed
- end
- end
- elseif event.phase == "ended" then
- if event.target.id == "jump" then
- print("Jump Released")
- else
- print("Left Right Released")
- hero.velocity = 0
- Runtime:removeEventListener( "enterFrame", setHeroVelocity )
- end
- end
- end
- -- Called when the scene's view does not exist:
- function scene:createScene( event )
- local group = self.view
- -- create a grey rectangle as the backdrop
- local background = display.newRect( 0, 0, screenW, screenH )
- background.anchorX = 0
- background.anchorY = 0
- background:setFillColor( .5 )
- -- Create the Hero
- hero = display.newRect ( 160, 60, 64, 96 )
- -- Controles
- controls = display.newRect( 70, 290, 96, 32 )
- controls.id = "LeftRight"
- controls:addEventListener( "touch", doControlsTouch )
- local btnJump = display.newCircle( 450, 290, 16 )
- btnJump.id = "jump"
- btnJump:addEventListener( "touch", doControlsTouch )
- -- add physics to the hero
- physics.addBody( hero, { density=1.0, friction=0.3, bounce=0 } )
- setHeroProperties()
- -- create a grass object and add physics (with custom shape)
- local grass = display.newImageRect( "grass.png", screenW, 82 )
- grass.anchorX = 0
- grass.anchorY = 1
- grass.x, grass.y = 0, display.contentHeight
- -- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see)
- local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
- physics.addBody( grass, "static", { friction=0.3, shape=grassShape } )
- camera:add(hero, 1, true)
- camera:add(grass, 2, false)
- camera:add(background, 3, false)
- camera:setBounds(60,display.contentWidth, 0,display.contentHeight)
- -- all display objects must be inserted into group
- group:insert( background )
- group:insert( grass)
- group:insert( controls )
- group:insert( btnJump )
- group:insert( hero )
- end
- -- Called immediately after scene has moved onscreen:
- function scene:enterScene( event )
- local group = self.view
- camera:track()
- physics.start()
- end
- -- Called when scene is about to move offscreen:
- function scene:exitScene( event )
- local group = self.view
- physics.stop()
- end
- -- If scene's view is removed, scene:destroyScene() will be called just prior to:
- function scene:destroyScene( event )
- local group = self.view
- package.loaded[physics] = nil
- physics = nil
- end
- -----------------------------------------------------------------------------------------
- -- END OF YOUR IMPLEMENTATION
- -----------------------------------------------------------------------------------------
- -- "createScene" event is dispatched if scene's view does not exist
- scene:addEventListener( "createScene", scene )
- -- "enterScene" event is dispatched whenever scene transition has finished
- scene:addEventListener( "enterScene", scene )
- -- "exitScene" event is dispatched whenever before next scene's transition begins
- scene:addEventListener( "exitScene", scene )
- -- "destroyScene" event is dispatched before view is unloaded, which can be
- -- automatically unloaded in low memory situations, or explicitly via a call to
- -- storyboard.purgeScene() or storyboard.removeScene().
- scene:addEventListener( "destroyScene", scene )
- -----------------------------------------------------------------------------------------
- return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement