Ok I have some questions.
Don’t make fun of my code I am very new to programming and don’t know what’s “best practice” or what “clean code” really means
…yet.
I have this demo skeleton I am using for a game. I noticed that if I was to design it and manually trial and error it It would take me a long while (which is ok I guess, but I want to learn to be more efficient).
I am not using lime, or svglevel editor, or level helper etc etc (although I own all of these). I instead am being “pure” and going pure corona.
I wrote this demo (plug n play code below, system generated shapes as per normal with my example posts).
It includes:
-Following the main player and sensing when a border is coming so it doesn’t “jump” and you see the outside of the map.
-Wrapping - the player will wrap from side to side, meaning it goes out the right side, re appears on the left (thanks to peach pellen, this was dead simple)
-constant force on the player when you press the screen (anywhere on the screen, I don’t like d pads, or silly complex controls)
-big map this is 20,000 pixels high!
-tilt controls (Only left and right)
Ok I have questions:
I have these platforms (platX X being the number, so like plat1 etc)
They are placed all over the place, starting at the bottom and going all the way up to -19000 (top of the map)
The questions are:
How do I remove platforms that are outside the view of the player (i’m thinking isSensor and doing some if Y = distance from player, then display image or something?) Obviously, I want them to spawn a half screen away before the player reaches it, and respawn if the player falls a ways etc.
I have some other questions, and I’ll tack them on to here after I figure this piece out.
I thought about using Tiled editor and saving png’s from that and then making a grid of
625 squares high by 20 wide (20,000 / 32 pixel grid size = 625, and assuming 640 width on iphone 4 / 32 = 20)
Then printing out the grid on some long ass paper (I have this plotter paper at work which is epic cool)
then draw w/ pencil and mark out the X and Y coordinates (form center reference point of each object) then using that to code into Corona…
phew! Is that crazy? Or is it a stupid idea? I have tried it and while it’s accurate it seems to take a pretty long time.
Anyway, yea suggestions welcome and feel free to use the code below. I am fascinated with it.
Thanks for the input!
ng
[code]
display.setStatusBar( display.HiddenStatusBar )
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
–Each function and set of locals are blocked off marked “TOP” and “BOTTOM” to denote
–Beginning and end of each function/method/listener(s) etc etc. With Double Spacing…Keep it clean!
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
–Quick Reference for searching doc for functions (make sure to update the list as you add/replace)
–[[
DisplayGroup()
InitPhysics()
CreateGround()
SpawnPlayer()
Wrap()
–]]
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local physics = require (“physics”)
local PlayerUpDownSpeed = 300
local MovePlayerUp = 0
local MovePlayerDown = 0
local _W = display.contentWidth
local _H = display.contentHeight
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function DisplayGroup()
game = display.newGroup()
game.x = 0
game.y = 0
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
–level is 20,000 pixels high
local function SpawnPlatforms()
–Left side platforms
local plat = display.newRect( 0 , -19000 , 130 , 40 )
physics.addBody( plat , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
plat:setFillColor(165, 210, 255)
game:insert(plat)
local plat2 = display.newRect( 0 , -10000 , 130 , 40 )
physics.addBody( plat2 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat2)
local plat3 = display.newRect( 0 , -8000 , 130 , 40 )
physics.addBody( plat3 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat3)
local plat4 = display.newRect( 0 , -6000 , 130 , 40 )
physics.addBody( plat4 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat4)
local plat5 = display.newRect( 0 , -5000 , 130 , 40 )
physics.addBody( plat5, “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat5)
local plat6 = display.newRect( 0 , -4000 , 130 , 40 )
physics.addBody( plat6 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat2)
local plat7 = display.newRect( 0 , -3000 , 130 , 40 )
physics.addBody( plat7 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat7)
local plat8 = display.newRect( 0 , -2000 , 130 , 40 )
physics.addBody( plat8 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat8)
local plat9 = display.newRect( 0 , -1000 , 130 , 40 )
physics.addBody( plat9 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat9)
local plat10 = display.newRect( 0 , 60 , 130 , 40 )
physics.addBody( plat10 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat10)
–Right side platforms
local plat11 = display.newRect( 250 , -400 , 130 , 40 )
physics.addBody( plat11 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat11)
local plat12 = display.newRect( 250 , -800 , 130 , 40 )
physics.addBody( plat12 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat12)
local plat13 = display.newRect( 250 , -800 , 130 , 40 )
physics.addBody( plat13 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat13)
local plat14 = display.newRect( 250 , -1300 , 130 , 40 )
physics.addBody( plat13 , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(plat14)
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function SpawnPlayer()
Player = display.newRect( 0 , 400 , 50 , 50 )
physics.addBody( Player , “dynamic” ,{ bounce = 0 , friction = 0 , density = 1 } )
Player:setFillColor(165, 210, 255)
game:insert(Player)
Player.x = _W/2
Player.y = _H/2 -50
physics.addBody( Player , “dynamic” ,{ bounce = 0 , friction = 0 , density = 1 , } )
Player.isFixedRotation = true
game:insert( Player)
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function Wrap (event)
–Set the border of “wrapping” adjust the number to adjust “snap” of image as it appears on either side
if Player.x < 0 then
Player.x = 320
end
if Player.x > 320 then
Player.x = 0
end
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function CreateGround()
local ground = display.newRect( 0 , 460 , 640 , 40 )
physics.addBody( ground , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )
game:insert(ground)
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function MovePlayerUpOrDown()
if MovePlayerUp == 1 then
Player:setLinearVelocity( 0 , - PlayerUpDownSpeed )
else
vx , vy = Player:getLinearVelocity()
Player:setLinearVelocity( vx , vy )
end
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function Touch(event)
if event.phase == “began” then
MovePlayerUp = 1
elseif ( event.phase == “ended” or event.phase == “canceled” ) then
MovePlayerUp = 0
end
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function EnterFrame(event)
MovePlayerUpOrDown()
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function InitPhysics()
physics.start( true )
physics.setScale( 100 )
physics.setDrawMode( “normal” )
SpawnPlatforms()
physics.setGravity( 0 , 5 )
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function InitGame()
DisplayGroup()
InitPhysics()
CreateGround()
SpawnPlayer()
Wrap()
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
–Camera Follow Player
local function moveCamera()
if (Player.x > 200 and Player.x < 200) then
game.x= -Player.x + 200 --Player.x 200 says where it shows up
end
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
local function moveCamera1()
–if (Player.y > #, the higher you put this number the bigger the map will be vertically
–for endless maps, you can set this to some ridiculous number like I dunno 99,999?
if (Player.y > -20000 and Player.y < 300) then
game.y= -Player.y + 280
end
end
------These 2 event listeners are at bottom of main.lua
–Runtime:addEventListener( “enterFrame”, moveCamera )
–Runtime:addEventListener( “enterFrame”, moveCamera1 )
–end of camera follow player
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
–Begin Tilt Controls (Left and Right)
system.setAccelerometerInterval( 100 ) – set accelerometer to maximum responsiveness
function onTilt( event )
physics.setGravity( ( 9.8 * event.xGravity ), ( 9.8) )
end
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
–+++++++++++++++++++++++++++++++TOP++++++++++++++++++++++++++++++++++++++++
–For the cameras for X and Y
Runtime:addEventListener( “enterFrame”, moveCamera )
Runtime:addEventListener( “enterFrame”, moveCamera1 )
Runtime:addEventListener(“enterFrame”, Wrap)
Runtime:addEventListener( “touch”, Touch )
Runtime:addEventListener( “enterFrame” , EnterFrame )
–Tilt controls listener
Runtime:addEventListener( “accelerometer”, onTilt )
–++++++++++++++++++++++++++BOTTOM++++++++++++++++++++++++++++++++++++++++
InitGame()
[/code] [import]uid: 61600 topic_id: 13923 reply_id: 313923[/import]
Thanks again.