Hi guys,
Im writing to ask about Dr Brian Burtons code in his corona sdk book.
Its a simple gravity example test. Up down left right all affecting the gravity of a crate
Ive typed it exactly as it is in the book, and it seems very finiky, like Initially SetGravity(0,0) and adjusting the gravity buttons does nothing.
unless i set gravity to 0, 9.8, then im able to adjust the falling crates gravity using buttons. but even that works sometimes and sometimes theres no response. am i missing something? also the values in the text suddenly jump to additional numbers…
require “CiderDebugger”;-- Project: Ch8PlayingWithGravity
– Description:
–
– Version: 1.0
– Managed with http://CoronaProjectManager.com
–
– Copyright 2011 Brian Burton. All Rights Reserved.
---- cpmgen main.lua
local physics = require (“physics”)
physics.start()
–set initial
physics.setGravity(0, 9.8)
–initialise gx and gy
gx = 0
gy = 0
local ground = display.newRect(0, 768, 768, 10)
ground:setFillColor(255, 255, 255, 255)
local leftSide = display.newRect(1,1,10,768)
leftSide:setFillColor(255,255,255,255)
local rightSide = display.newRect(758,0,768,768)
rightSide:setFillColor(255,255,255,255)
local top = display.newRect(0,0,768, 10)
top:setFillColor(255,255,255,255)
– border to phyisics as a static
physics.addBody(ground, “static”)
physics.addBody(leftSide, “static”)
physics.addBody(rightSide, “static”)
physics.addBody(top, “static”)
–crate loading
local crate = display.newImage(“crateB.png”)
crate.x = 389
crate.y = 389
physics.addBody(crate, {density=1.0, friction=0.2, bounce=0.3})
local gravityX = display.newText(“0.0”, 490, 875, native.systemFont, 36)
local gravityY = display.newText(“0.0”, 195, 875, native.systemFont, 36)
local upButton = display.newImage(“arrowButton.png”, 200, 800)
upButton.rotation = -90
local downButton = display.newImage(“arrowButton.png”, 200, 950)
downButton.rotation = 90
local leftButton = display.newImage(“arrowButton.png”, 400, 875)
leftButton.rotation = 180
local rightButton = display.newImage(“arrowButton.png”, 600, 875)
–update gravity function
local function updateGravity()
gx, gy = physics.getGravity()
gravityX.text = gx
gravityX.text = (gravityX.text:sub(1,4))
gravityY.text = gy
gravityY.text = (gravityY.text:sub(1,4))
end
– adjust gravity of buttons
local function upButtonEvent (event)
physics.setGravity(gx, gy + 0.1)
updateGravity()
end
local function downButtonEvent (event)
physics.setGravity(gx, gy - 0.1)
updateGravity()
end
local function leftButtonEvent (event)
physics.setGravity(gx - 0.1, gy)
updateGravity()
end
local function rightButtonEvent (event)
physics.setGravity(gx + 0.1, gy)
updateGravity()
end
–listeners
upButton:addEventListener(“tap”, upButtonEvent)
downButton:addEventListener(“tap”, downButtonEvent)
leftButton:addEventListener(“tap”, leftButtonEvent)
rightButton:addEventListener(“tap”, rightButtonEvent)
please could you guys help