[lua]local director = require( “director” )
– include Corona’s “physics” library
local physics = require “physics”
physics.start()
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
_W = display.contentWidth;
_H = display.contentHeight;
local physics = require “physics”
physics.start()
physics.setGravity(0, 2)
–Status Bar
display.setStatusBar(display.HiddenStatusBar)
local sky = display.newImage( “sky.png” )
local spawnTrue = true;
–physics.setDrawMode ( “hybrid” )
physics.setDrawMode ( “hybrid” )
local function spawnOrb()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
orb = display.newImage( “orb.png” )
orb.x = randomPos
orb.y = -100
orb.myName = “orb”
physics.addBody(orb , “dynamic”, {isSensor = true, radius = 15})
end
end
timer.performWithDelay( 1500, spawnOrb, 500 )
local function onCollideObject(event)
if ( event.phase == “began” ) then
if event.object1.myName == “orb” and event.object2.myName == “penguin” then
event.object2:removeSelf()
event.object2 = nil
end
end
end
Runtime:addEventListener( “collision”, onCollideObject)
–Penguin
–Define wall graphics (rectangles)
local leftWall = display.newRect( 0, 0, 1, display.contentHeight )
local rightWall = display.newRect( display.contentWidth, 0, 1, display.contentHeight )
–Turn wall graphics into physical bodies
physics.addBody(leftWall, “static”, { bounce = 0.1} )
physics.addBody(rightWall, “static”, { bounce = 0.1} )
local score = display.newText(“Score: 0”, 40, 5)
score:setTextColor(0, 0, 0)
score.rotation = 0
score.size = 28
scoreCtr = 0
local lastGoalTime = 1000
scoreCtr = 0
local lastGoalTime = 1000
local grass = display.newImage( “grass.png” )
grass.y = 480
local penguin = display.newImage(“penguin.png”)
penguin.x = 240
penguin.y = 225
physics.addBody(penguin, “static”,{radius=45})
– ARROWS –
local left = display.newImage (“leftx.png”)
left.x = 30
left.y = 260
local right = display.newImage (“rightx.png”)
right.x = 110
right.y = 260
– Puts in all four movement arrow images and positions them
– MOVE PEACH –
local motionx = 0
local speed = 10
– Speed can be adjusted here to easily change how fast my picture moves. Very convenient!
local function stop (event)
if event.phase ==“ended” then
motionx = 0
end
end
Runtime:addEventListener(“touch”, stop )
– When no arrow is pushed, this will stop me from moving.
local function movepenguin (event)
penguin.x = penguin.x + motionx
end
Runtime:addEventListener(“enterFrame”, movepenguin)
– When an arrow is pushed, this will make me move.
function left:touch()
motionx = -speed
end
left:addEventListener(“touch”,left)
function right:touch()
motionx = speed
end
right:addEventListener(“touch”,right)
– The above four functions are stating the arrows should all listen for touches and defining
– the way I should move based on each touch.
local function wrap (event)
if penguin.x < 50 then
penguin.x = 50
end
if penguin.x > 430 then
penguin.x = 430
end
if speed < 0 then
speed = 0
end
end
Runtime:addEventListener(“enterFrame”, wrap)
score = require (“score”)
local border = 5
local scoreInfo = score.getInfo()
score.init({
x = 160,
y = 130}
)
score.setScore(0)
local function addtoit (event)
score.setScore (score.getScore()+2)
end
timer1 = timer.performWithDelay(1000,addtoit,0)
timer.cancel (timer1)
score.getScore()
score = score.getScore()
print (score)
[import]uid: 132369 topic_id: 24251 reply_id: 98055[/import]