I have been playing around with the corona SDK and I ran into a problem. I do not know where to put an event listener to make a ball bounce. I do not know where to put “Runtime:addEventListener(“touch”, onTouch)” so the ball can bounce. Most this code is from the game template but I am just trying to figure out how it works.
Thanks
[lua]-----------------------------------------------------------------------------------------
– level1.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start();
physics.setGravity(0, 9.8)
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
–Runtime:addEventListener(“touch”, onTouch)
– BEGINNING OF YOUR IMPLEMENTATION
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– 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:setFillColor( 128 )
local circle = display.newImage(“Physball_Ball50.png”)
physics.addBody( circle, { density = 1.0, friction = 100, bounce = 0.3, radius = 25 } )
circle:applyForce(100, 100, circle.x, circle.y)
local onTouch = function(event)
circle:applyLinearImpulse(50,0, circle.x, circle.y)
end
timer.performWithDelay(2000,“touch”)
Runtime:addEventListener(“touch”, onTouch)
– create a grass object and add physics (with custom shape)
local grass = display.newImageRect( “grass.png”, screenW, 82 )
grass:setReferencePoint( display.BottomLeftReferencePoint )
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 } )
– all display objects must be inserted into group
group:insert( background )
group:insert( grass)
group:insert( crate )
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
physics.start()
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
return scene
[import]uid: 82872 topic_id: 28321 reply_id: 328321[/import]