Level design, large level, spawning objects, removing them and more! (need help)

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 :slight_smile: …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]

@nicholasclayg,
Thanks you for posting this. I am in the middle of programming my first camera into my game. My camera will follow a ball that is dropped from the sky while keeping it in the center until it gets the ground. about 1400px in height but just 320 in width. there will be objects that you will have to dodge that are stationart/static in the sky. I hope I dont make a spaghetti mess of my code :wink: Thanks again.

~jeff [import]uid: 12540 topic_id: 13923 reply_id: 51231[/import]

Hey Jeff, the code I posted is from Egg Breaker (you know, the demo game that comes with corona). Most of the code is what I learned from others and modified it to fit my needs (that’s how I am learning right now). But as you can see, you can make something pretty cool (at least I think so hehe).

You can mess with it however you please. Keep in mind make sure you have things in groups (it creates a group called game, and then actions are taken against the group).
These are the 2 lines you should be concerned with.

  
if (Player.y \> -20000 and Player.y \< 300) then  
game.y= -Player.y + 280   

For the x movement

if (Player.x \> 200 and Player.x \< 200) then  
   
game.x= -Player.x + 200 --Player.x 200 says where it shows   

If you design a game that’s more than one screen (my other 4 games have screens that are divisible by 16 or 32 pixels and are like 8 iphone4 screens long and 4 high) then you can adjust the camera to not “snap”.
What I mean is --> going this way >>>>>>>>>>>>|

| = the wall. Well if you don’t adjust it right the wall will keep going and you will see the outside of the map. Same goes for vertical movement.
Also, if your level is really high like mine 20,000 pixels or something, then make sure you adjust that as well (I put 20,000 of course). Then you take the highest number and subtract a couple hundred from that to get a perfect border at the top where your character doesn’t see the outside of the map. It’s fine tuning I guess.

Then you can use that for a template for the rest of the game, which makes life easier.
The full extracted camera code is this (just cleaned up from my original post above).

<br>--Camera Follow Player<br>local function moveCamera()<br> <br> if (Player.x &gt; 200 and Player.x &lt; 200) then<br> <br> game.x= -Player.x + 200 --Player.x 200 says where it shows up<br> <br> end<br> <br>end<br><br>local function moveCamera1()<br> <br>--if (Player.y &gt; #, the higher you put this number the bigger the map will be vertically<br> <br>--for endless maps, you can set this to some ridiculous number like I dunno 99,999?<br> <br>if (Player.y &gt; -20000 and Player.y &lt; 300) then<br> <br> game.y= -Player.y + 280 <br> <br> end<br> <br> end<br><br><br>--For the cameras for X and Y<br>Runtime:addEventListener( "enterFrame", moveCamera )<br>Runtime:addEventListener( "enterFrame", moveCamera1 )<br><br>


As for others, any tips on how to remove platforms and other objects that are not within the screen I’d like to know. [import]uid: 61600 topic_id: 13923 reply_id: 51335[/import]

Why not just have one camera?

[lua]–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
if (Player.y > -20000 and Player.y < 300) then
game.y= -Player.y + 280
end
end

–For the cameras for X and Y
Runtime:addEventListener( “enterFrame”, moveCamera )[/lua]
[import]uid: 19626 topic_id: 13923 reply_id: 51365[/import]

@robmiracle
That’s an excellent question. I don’t know, I just remember fooling around with the egg breaker demo and I wanted left and right and up and down. So I kind of put band aids on the code to get what I want and it worked so I left it alone :slight_smile:

When I get to the end of my project and everything is how I want it, I’ll then start cleaning up stuff that doesn’t make sense and make it easy on the eyes.

I’ll have to try that code and plug it in to see what happens later today.

I’m such a beginner and when something starts working i’m like “DONT TOUCH IT!” lol.

:slight_smile:

ng
[import]uid: 61600 topic_id: 13923 reply_id: 51375[/import]

Hey guys,
would this code work with the Director class?? and if so how?
Being that you have groups for the objects/camera, how would you have those objects in the Director LocalGroup also?

~Jeff [import]uid: 12540 topic_id: 13923 reply_id: 51615[/import]

@Jeff.

I haven’t used director class. Well, not exactly I’ve “used” it as in I loaded it up and played around with it. From what I understand, you need things in groups, and other things as there is removing and clean up of each “scene” so you can keep an eye on memory leaks.

That’s what I know about it, but for whatever reason I’ve been avoiding it like the plague, mostly due to me not understanding and having zero coding experience as of 4 months ago.

Using groups though, is REALLY cool. Instead of writing physics body for each…and…every…object if you have a lot of the same thing you can just put things in a group, then apply to the group. Pretty neat, but ya on director you would have to hit up some other people that know better than I :slight_smile:

ng [import]uid: 61600 topic_id: 13923 reply_id: 53867[/import]