Last active
August 29, 2015 13:58
-
-
Save dabing1022/9946880 to your computer and use it in GitHub Desktop.
quick-cocos2dx-physics-test(2.2.1rc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ResourcesData = require("data.ResourcesData") | |
local scheduler = require("framework.scheduler") | |
local Bing_ganPhysics = require("data.bing_ganQuick") | |
local ChipmunkTestScene = class("ChipmunkTestScene", function() | |
return display.newScene("ChipmunkTestScene") | |
end) | |
local GRAVITY = -200 | |
local COIN_MASS = 100 | |
local COIN_RADIUS = 46 | |
local COIN_FRICTION = 0.8 | |
local COIN_ELASTICITY = 0.8 | |
local WALL_THICKNESS = 64 | |
local WALL_FRICTION = 1.0 | |
local WALL_ELASTICITY = 0.5 | |
function ChipmunkTestScene:ctor() | |
local contents = io.readfile(device.writablePath .. "excelData1.json") | |
print(contents) | |
local test = json.decode(contents) | |
dump(test) | |
local m = {} | |
for i, v in ipairs(test) do | |
m[v.name] = v | |
end | |
dump(m) | |
display.addSpriteFramesWithFile(ResourcesData.S_ALL_SPRITES_PLIST, ResourcesData.S_ALL_SPRITES_PNG) | |
-- create touch layer | |
self.layer = display.newLayer() | |
self.layer:addTouchEventListener(function(event, x, y) | |
return self:onTouch(event, x, y) | |
end) | |
self:addChild(self.layer) | |
self.stuff = {} | |
-- create label | |
self:addChild(ui.newTTFLabel({ | |
text = "TAP SCREEN", | |
size = 32, | |
x = display.cx, | |
y = display.cy, | |
align = ui.TEXT_ALIGN_CENTER | |
})) | |
-- create batch node | |
-- self.batch = display.newBatchNode(GAME_TEXTURE_IMAGE_FILENAME) | |
-- self:addChild(self.batch) | |
self.batch = display.newNode():addTo(self) | |
-- create physics world | |
self.world = CCPhysicsWorld:create(0, 0) | |
-- add world to scene | |
self:addChild(self.world) | |
local centerEarth = display.newSprite(ResourcesData.S_EARTH1) | |
centerEarth:setScaleX(2.5) | |
centerEarth:setScaleY(2.5) | |
self.batch:addChild(centerEarth) | |
local centerEarthBody = self.world:createCircleBody(0, 100) | |
centerEarthBody:setFriction(WALL_FRICTION) | |
centerEarthBody:setElasticity(WALL_ELASTICITY) | |
centerEarthBody:bind(centerEarth) | |
centerEarthBody:setCollisionType(1) | |
centerEarthBody:setCollisionGroup(0) | |
-- centerEarthBody:setCollisionLayers(1) | |
centerEarthBody:setPosition(display.cx, display.cy ) | |
-- add static body | |
local leftWallSprite = display.newSprite("#Wall.png") | |
leftWallSprite:setScaleY(display.height / WALL_THICKNESS) | |
self.batch:addChild(leftWallSprite) | |
local leftWallBody = self.world:createBoxBody(0, WALL_THICKNESS, display.height) | |
leftWallBody:setFriction(WALL_FRICTION) | |
leftWallBody:setElasticity(WALL_ELASTICITY) | |
leftWallBody:bind(leftWallSprite) | |
leftWallBody:setPosition(display.left + WALL_THICKNESS / 2, display.cy + WALL_THICKNESS) | |
local rightWallSprite = display.newSprite("#Wall.png") | |
rightWallSprite:setScaleY(display.height / WALL_THICKNESS) | |
self.batch:addChild(rightWallSprite) | |
local rightWallBody = self.world:createBoxBody(0, WALL_THICKNESS, display.height) | |
rightWallBody:setFriction(WALL_FRICTION) | |
rightWallBody:setElasticity(WALL_ELASTICITY) | |
rightWallBody:bind(rightWallSprite) | |
rightWallBody:setPosition(display.right - WALL_THICKNESS / 2, display.cy + WALL_THICKNESS) | |
local bottomWallSprite = display.newSprite("#Wall.png") | |
bottomWallSprite:setScaleX(display.width / WALL_THICKNESS) | |
self.batch:addChild(bottomWallSprite) | |
local bottomWallBody = self.world:createBoxBody(0, display.width, WALL_THICKNESS) | |
bottomWallBody:setFriction(WALL_FRICTION) | |
bottomWallBody:setElasticity(WALL_ELASTICITY) | |
bottomWallBody:bind(bottomWallSprite) | |
bottomWallBody:setPosition(display.cx, display.bottom + WALL_THICKNESS / 2) | |
-- add debug node | |
self.worldDebug = self.world:createDebugNode() | |
self:addChild(self.worldDebug) | |
scheduler.scheduleUpdateGlobal(function() | |
for i, v in ipairs(self.stuff) do | |
local stuffPosX, stuffPosY = v:getPosition() | |
local centerX, centerY = display.cx, display.cy | |
local diff = ccp(centerX - stuffPosX, centerY - stuffPosY) | |
if diff.x >= 100 then diff.x = 100 end | |
if diff.y >= 100 then diff.y = 100 end | |
v:applyImpulse(diff.x, diff.y) | |
end | |
end) | |
end | |
function ChipmunkTestScene:createCoin(x, y) | |
local coinSprite = display.newSprite(ResourcesData.dirImg .. "bing_gan.png") | |
self.batch:addChild(coinSprite) | |
local pointarr1 = CCPointArray:create(5) | |
pointarr1:add(ccp(-2.00000, -79.00000)) | |
pointarr1:add(ccp(-38.00000, -49.00000)) | |
pointarr1:add(ccp(-59.00000, 1.00000)) | |
pointarr1:add(ccp(-27.00000, 40.00000)) | |
pointarr1:add(ccp(64.00000, -1.00000)) | |
local coinBody = self.world:createPolygonBody(100, pointarr1) | |
coinBody:setFriction(COIN_FRICTION) | |
coinBody:setElasticity(COIN_ELASTICITY) | |
coinBody:bind(coinSprite) | |
coinBody:setPosition(x, y) | |
coinBody:setCollisionType(2) | |
coinBody:setCollisionGroup(0) | |
-- coinBody:setCollisionLayers(2) | |
self.stuff[#self.stuff+1] = coinBody | |
end | |
function ChipmunkTestScene:onTouch(event, x, y) | |
if event == "began" then | |
self:createCoin(x, y) | |
end | |
end | |
function ChipmunkTestScene:onEnter() | |
self.layer:setTouchEnabled(true) | |
self.world:start() | |
self.world:addCollisionScriptListener(function(event) | |
if event == "begin" then | |
echoInfo("collision begin") | |
return true | |
elseif event == "preSolve" then | |
-- echoInfo("collision preSolve") | |
return true | |
elseif event == "postSolve" then | |
-- echoInfo("collision postSolve") | |
elseif event == "separate" then | |
-- echoInfo("collision separate") | |
end | |
end, 1, 2) | |
end | |
function ChipmunkTestScene:onExit() | |
end | |
return ChipmunkTestScene |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment