I have a few questions,
1, How do I set different physics properties to each params.type for the platforms?
2, Should I require the physics in the main.lua or in the game.lua, what is best?
At the moment, all platforms have the same physics properties, when I add a platform to the game.lua the player and platform doesn’t collide with each other but instead the player fall right through it.
I’m using the director class and the _W, _H variables are declared in the main.lua
My game module;
game.lua
module(..., package.seeall)
local player = require("player")
local platform = require("platform")
function new()
local localGroup = display.newGroup()
local platform = platform.new({type = "startPlatform"});
platform.x = \_W/2;
platform.y = \_H/2;
-- When setting up the physics properties here the player collides with the platform.
--physics.addBody(platform, "static", startPlatform)
localGroup:insert(platform)
local player = player.new({type = "normal"})
localGroup:insert(player)
return localGroup
end
My player module;
player.lua
module(..., package.seeall)
function new(params)
local localGroup = display.newGroup()
local img;
if(params) then
if(params.type == "normal") then
img = "images/dude.png";
end
end
local player = display.newImageRect(img, 35, 64)
player:setReferencePoint(display.CenterReferencePoint)
player.x = \_W/2
player.y = \_H/2;
physics.addBody(player, "dynamic", {density =1.0, friction = 0.1, bounce = 0.25 , radius = 32})
player.isFixedRotation = false;
localGroup:insert(player);
-- Set up the Accelerometer values in Portrait
local motionX = 0
local motionY = 0
local function onAccelerate( event )
motionX = 20 \* event.xGravity;
motionY = 20 \* event.yGravity;
end
Runtime:addEventListener ("accelerometer", onAccelerate);
local function movePlayer (event)
player.x = player.x + motionX;
player.y = player.y - motionY;
end
Runtime:addEventListener("enterFrame", movePlayer)
-- A screen wrapper.
local function screenWrapper (event)
if player.x \< 0 then
player.x = display.contentWidth
end
if player.x \> display.contentWidth then
player.x = 0
end
if player.y \< 0 then
player.y = display.contentHeight
end
if player.y \> display.contentHeight then
player.y = 0
end
end
Runtime:addEventListener("enterFrame", screenWrapper)
return localGroup
end
My Platform module;
platform.lua
module(..., package.seeall)
function new(params)
local localGroup = display.newGroup()
local img;
startPlatform = { density=1.0, friction=0.1, bounce=0.5}
normalPlatform = { density=1.0, friction=0.6, bounce=0.5}
movingPlatform = { density=1.0, friction=1.0, bounce=1.5}
if(params) then
if(params.type == "startPlatform") then
img = "images/platform1.png";
elseif(params.type == "normalPlatform") then
img = "images/platform2.png";
elseif(params.type == "movingPlatform") then
img = "images/platform3.png";
end
end
local platform = display.newImageRect(img, 64, 8)
platform:setReferencePoint(display.CenterReferencePoint);
physics.addBody(platform, "kinematic", startPlatform)
platform.isFixedRotation = true
platform.isSensor = true;
localGroup:insert(platform);
return localGroup
end
[import]uid: 34126 topic_id: 10418 reply_id: 310418[/import]