This is all of my code:
--\> Hide status bar
display.setStatusBar( display.HiddenStatusBar )
--\> Add physics engine, start up the engine, and apply gravity
local physics = require("physics")
local gameUI = require("gameUI")
physics.start()
physics.setGravity(0, 100)
system.activate( "multitouch" )
--physics.setDrawMode("hybrid")
--\> Add background image
local background = display.newImage("background2.png")
--\> Add balls to stage and position
local ball = display.newImage("hacky2.png")
ball.x = display.contentWidth/2
physics.addBody(ball, {density = 6 , bounce = .1, radius = 75, friction = 1.0} )
-- Define wall graphics (rectangles)
local leftWall = display.newRect(-1, 0, 1, display.contentHeight )
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight )
local ceiling = display.newRect(0, 1, display.contentWidth, 1 )
local floor = display.newRect(0, 970, display.contentWidth, 0 )
-- Turn wall graphics into physical bodies
physics.addBody(leftWall, "static", { bounce = 0.2} )
physics.addBody(rightWall, "static", { bounce = 0.5} )
physics.addBody(ceiling, "static", { bounce = 0.3} )
physics.addBody(floor, "static", { bounce = 0, friction = 200} )
--\> Add floor image and position
-- Define our touch event listener.
function doImpulse(event)
media.playEventSound("click.caf")
ball:applyLinearImpulse( -8000, -10000, event.x, event.y )
end
-- Add the listener to our ball
ball:addEventListener("touch", doImpulse)
ball.angularDamping = 6
local d = ball.angularDamping
--------Score
io.output():setvbuf('no') -- disable output buffering for console in device!
Json = require("Json")
score = require("score")
local currentScore =0
local scList=0
local bestScore = 0
--
-- ////////////////////
--
local showScore=display.newText( "", 250, 10, nil, 30)
showScore:setTextColor( 0,0,0 )
local yourScore=display.newText( "YOUR SCORE:", 20, 10, nil, 30)
yourScore:setTextColor( 0,0,0 )
local showBestScore=display.newText( "", 550, 10, nil, 30)
showBestScore:setTextColor( 0,0,0 )
local yourBestScore=display.newText( "BEST SCORE:", 320, 10, nil, 30)
yourBestScore:setTextColor( 0,0,0 )
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
function ball:touch(event)
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
currentScore=currentScore+1
if currentScore\>=bestScore then scList:StoreEntry(1,currentScore,"best") end
scList:Save("score3.txt")
bestScore=scList:GetPoints(1)
showScore.text=currentScore
showBestScore.text=bestScore
end
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
local onSystemEvent = function( event )
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
print ("onSystemEvent-\>"..event.type)
if event.type == "applicationExit" then
--
elseif event.type == "applicationStart" then
print ("applicationStart")
scList = score.newList(1)
scList:Load("score3.txt")
bestScore=scList:GetPoints(1)
print ("bestscore:"..bestScore)
elseif "applicationSuspend" == event.type then
tSuspend = system.getTimer()
elseif "applicationResume" == event.type then
--add missing time to tPrevious
tPrevious = tPrevious + ( system.getTimer() - tSuspend )
end
end
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
local onFrame = function( event )
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
end
ball:addEventListener("touch", ball)
Runtime:addEventListener( "system", onSystemEvent )
Runtime:addEventListener( "enterFrame", onFrame )
--currentScore = 0 Function
local function onLocalCollision (self, event)
if (event.phase == "began") then
currentScore = 0
showScore.text = currentScore
end
end
floor.collision = onLocalCollision
floor:addEventListener("collision", floor)
local start = display.newImage("start.png")
local function disappear ( event )
transition.to(start, {time = 500, alpha = 0} )
end
Runtime:addEventListener("touch", disappear)
Im not sure but i also think you have to have this file as a separate file in your game folder and name it “gameUI.lua”…my game doesn’t work without it:
-- gameUI library
module(..., package.seeall)
-- A general function for dragging physics bodies
-- Simple example:
-- local dragBody = gameUI.dragBody
-- object:addEventListener( "touch", dragBody )
function dragBody( event, params )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if "began" == phase then
stage:setFocus( body, event.id )
body.isFocus = true
-- Create a temporary touch joint and store it in the object for later reference
if params and params.center then
-- drag the body from its center point
body.tempJoint = physics.newJoint( "touch", body, body.x, body.y )
else
-- drag the body from the point where it was touched
body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
end
-- Apply optional joint parameters
if params then
local maxForce, frequency, dampingRatio
if params.maxForce then
-- Internal default is (1000 \* mass), so set this fairly high if setting manually
body.tempJoint.maxForce = params.maxForce
end
if params.frequency then
-- This is the response speed of the elastic joint: higher numbers = less lag/bounce
body.tempJoint.frequency = params.frequency
end
if params.dampingRatio then
-- Possible values: 0 (no damping) to 1.0 (critical damping)
body.tempJoint.dampingRatio = params.dampingRatio
end
end
elseif body.isFocus then
if "moved" == phase then
-- Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
stage:setFocus( body, nil )
body.isFocus = false
-- Remove the joint when the touch ends
body.tempJoint:removeSelf()
end
end
-- Stop further propagation of touch event
return true
end
-- A function for cross-platform event sounds
function newEventSoundXP( params )
local isAndroid = "Android" == system.getInfo("platformName")
if isAndroid and params.android then
soundID = media.newEventSound( params.android ) -- return sound file for Android
elseif params.ios then
soundID = media.newEventSound( params.ios ) -- return sound file for iOS/MacOS
end
return soundID
end
-- A function for cross-platform fonts
function newFontXP( params )
local isAndroid = "Android" == system.getInfo("platformName")
if isAndroid and params.android then
font = params.android -- return font for Android
elseif params.ios then
font = params.ios -- return font for iOS/MacOS
else
font = native.systemFont -- default font (Helvetica on iOS, Android Sans on Android)
end
return font
end
Let me know if you have any questions. [import]uid: 25129 topic_id: 5558 reply_id: 23707[/import]