Trying to Understand

Hello everyone. Glad to be here. I am new to all of this and will certainly need a lot of help :slight_smile:

I am trying to work with the Ghosts vs Monsters example and am not sure about the screen position on the start of a level. There seems to be a lot of code for swipe and screen position and I’m just not sure what I am looking at. I also tried to search for these without much success.

What I am trying to do is build a similar game. I have two backgrounds and two grounds loaded in the level. The position seems to be focused on the right end and will not move.

I think if I knew what I was looking for… which function does what, that would be a huge help. So I’m trying to understand what makes the game start by focusing on the targets and then switch to the left. Can someone please help me understand where in GvM level1 this is happening? Thanks! [import]uid: 93625 topic_id: 16261 reply_id: 316261[/import]

Hey Cheryl,

Firstly, welcome!

Secondly - Ghosts Vs Monsters is not really a sample app that a person can just go “X function does this, Y function does that!”

It’s fairly advanced stuff and as a new user you really should be focussing on mastering some of the fundamentals before delving into unravelling code like that.

With a little time spent mastering the basics you’ll find you can read the code and have it all make sense :slight_smile:

Peach [import]uid: 52491 topic_id: 16261 reply_id: 61014[/import]

Thanks Peach. And thanks for your awesome tutorials also!!!

I am trying to work through it piece by piece and it seems now that the simulator is referencing errors with director.lua…

Copyright (C) 2009-2011 A n s c a , I n c . Version: 2.0.0 Build: 2011.591 Loaded Background File Loaded Externals Loaded Objects Loaded Variables Drew Background Created Targets Created Ground Start touch Function Runtime error ...rs\Me\Documents\TiniOlives\App Working\director.lua:271: Error: table expected. If this is a function call, you might have used '.' instead of ':' stack traceback: [C]: ? [C]: in function 'insert' ...rs\Me\Documents\Tini Olives\App WRuntime error ...rs\Me\Documents\Tini Olives\App Working\director.lua:305: ERROR: table expected. If this is a function call, you might have used '.' instead of ':' stack traceback: [C]: ? [C]: in function 'insert' ...rs\Me\Documents\Tini Olives\App W

All the stuff at the beginning is just print commands I stuck in to try and follow along :slight_smile:

In trying to decipher the debugger, it looks like there is a problem either within director, or with something in my code that is calling on director. Is that correct?

As far as taking on something so complex… well, that is just how I do things. Thanks for the help! [import]uid: 93625 topic_id: 16261 reply_id: 61022[/import]

Hi.

Yeah that error usually means something is wrong in your code, and thus, director cant execute it. Care to post up some code? [import]uid: 84637 topic_id: 16261 reply_id: 61235[/import]

Sure. This is all basically copied from GvM where I was just taking what I thought was needed.

[code]module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()

local gameGroup = display.newGroup()
gameGroup.x = -960

– MODULE-SPECIFIC VARIABLES
local backgroundFilename1 = “bar_background_01.png”
local backgroundFilename2 = “bar_background_02.png”
print( “Loaded Background File”)
– EXTERNAL MODULES / LIBRARIES

local movieclip = movieclip --require( “movieclip” )
local physics = require “physics”
local ui = ui --require(“ui”)
print( “Loaded Externals” )
– OBJECTS

local backgroundImage1
local backgroundImage2
local glass1
local glass2
local glass3
print( “Loaded Objects”)
–VARIABLES

local gameIsActive = false
local waitingForNewRound
local restartTimer
local ghostTween
local screenPosition = “left” --> “left” or “right”
local canSwipe = true
local swipeTween
local gameLives = 4
local gameScore = 0
local bestScore
local monsterCount
print( “Loaded Vairables” )
local drawBackground = function()
– Background gets drawn in this order: backdrop, clouds, trees, red glow

– BACKDROP
backgroundImage1 = display.newImageRect( backgroundFilename1, 960, 640 )
backgroundImage1:setReferencePoint( display.CenterLeftReferencePoint )
backgroundImage1.x = 0; backgroundImage1.y = display.contentHeight/2

backgroundImage2 = display.newImageRect( backgroundFilename2, 960, 640 )
backgroundImage2:setReferencePoint( display.CenterLeftReferencePoint )
backgroundImage2.x = 960; backgroundImage2.y = display.contentHeight/2

gameGroup:insert( backgroundImage1 )
gameGroup:insert( backgroundImage2 )
end
print( “Drew Background” )
local createTargets = function()

– GLASSES
glass1 = display.newImage( “to_mart_glass.png” )
glass1.x = 1220; glass1.y = 439
leftside = { -60,-75, -55,-75, 0,0, -5,0 }
rightside = { 55,-75, 65,-75, 0,5, 0,0 }
stem = { -3,0, 3,0, 3,76, -3,76 }
base = { -40,76, 40,76, 40,80, -40,80 }

glass2 = display.newImage( “to_mart_glass.png”)
glass2.x = 1410; glass2.y = 439

glass3 = display.newImage( “to_mart_glass.png” )
glass3.x = 1600; glass3.y = 439

physics.addBody( glass1, “dynamic”,
{ density=1.0, friction=0.5, bounce=0.2, shape=leftside },
{ density=1.0, friction=0.5, bounce=0.2, shape=rightside },
{ density=1.0, friction=0.5, bounce=0.2, shape=stem },
{ density=1.0, friction=0.5, bounce=0.2, shape=base }
)

physics.addBody( glass2, “dynamic”,
{ density=1.0, friction=0.5, bounce=0.2, shape=leftside },
{ density=1.0, friction=0.5, bounce=0.2, shape=rightside },
{ density=1.0, friction=0.5, bounce=0.2, shape=stem },
{ density=1.0, friction=0.5, bounce=0.2, shape=base }
)
physics.addBody( glass3, “dynamic”,
{ density=1.0, friction=0.5, bounce=0.2, shape=leftside },
{ density=1.0, friction=0.5, bounce=0.2, shape=rightside },
{ density=1.0, friction=0.5, bounce=0.2, shape=stem },
{ density=1.0, friction=0.5, bounce=0.2, shape=base }
)
gameGroup:insert( glass1 )
gameGroup:insert( glass2 )
gameGroup:insert( glass3 )
end
print( “Created Targets” )

local createGround = function()
–groundLight1 = display.newImageRect( “groundlight.png”, 228, 156 )
–groundLight1.x = 150; groundLight1.y = 190

groundObject1 = display.newImageRect( “bar_top1.png”, 960, 122 )
groundObject1:setReferencePoint( display.BottomLeftReferencePoint )
groundObject1.x = 0; groundObject1.y = display.contentHeight

groundObject2 = display.newImageRect( “bar_top2.png”, 960, 122 )
groundObject2:setReferencePoint( display.BottomLeftReferencePoint )
groundObject2.x = 960; groundObject2.y = display.contentHeight

groundObject1.myName = “ground”
groundObject2.myName = “ground”

local groundShape = { -480,-61, 480,-61, 480,61, -480,61 }
physics.addBody( groundObject1, “static”, { density=2.0, bounce=0, friction=0.5, shape=groundShape } )
physics.addBody( groundObject2, “static”, { density=2.0, bounce=0, friction=0.5, shape=groundShape } )

–gameGroup:insert( groundLight1 )
gameGroup:insert( groundObject1 )
gameGroup:insert( groundObject2 )
end
print( “Created Ground” )
print( “Start touch Funstion” )

local onScreenTouch = function( event )
if gameIsActive then

– Swipe to view other end of the screen

if leftRight == “left” and screenPosition == “left” and event.xStart > 360 then – Swiped game screen to the left
print( “Swiped left!” )
canSwipe = false

local switchPosition = function()
screenPosition = “right”
local swipeTimer = timer.performWithDelay( 200, function() canSwipe = true; end, 1 )
end

if swipeTween then transition.cancel( swipeTween )
end

if (event.xStart - event.x) >= 300 then
swipeTween = transition.to( gameGroup, { time=700, x=-960, onComplete=switchPosition } )
else
swipeTween = transition.to( gameGroup, { time=100, x=0, onComplete= function() canSwipe = true; end } )
end

elseif leftRight == “right” and screenPosition == “right” then – Swiped screen to the right
print( “Swiped right!” )
canSwipe = false

local switchPosition = function()
screenPosition = “left”
local swipeTimer = timer.performWithDelay( 200, function() canSwipe = true; end, 1 )
end

if swipeTween then
transition.cancel( swipeTween )
end

if (event.x - event.xStart) >= 100 then
swipeTween = transition.to( gameGroup, { time=700, x=0, onComplete=switchPosition } )
else
swipeTween = transition.to( gameGroup, { time=100, x=-960, onComplete=function() canSwipe = true; end } )
end

end
if canSwipe == true then

if screenPosition == “left” then
– Swipe left to go right
if event.xStart > 360 then
gameGroup.x = event.x - event.xStart

if gameGroup.x > 0 then
gameGroup.x = 0
canSwipe = true
end
end

elseif screenPosition == “right” then
– Swipe right to go to the left
gameGroup.x = (event.x - event.xStart) - 960

if gameGroup.x < -960 then
gameGroup.x = -960
canSwipe = true
end
end
end
end
print( “Game Init” )
local gameInit = function()

– PHYSICS
physics.start( true )
physics.setDrawMode( “hybrid” ) – set to “debug” or “hybrid” to see collision boundaries
physics.setGravity( 0, 11 ) --> 0, 9.8 = Earth-like gravity

– DRAW GAME OBJECTS
drawBackground()
createGround()
createTargets()
–createGhost()

– CREATE LEVEL
–createLevel()

– DRAW HEADS-UP DISPLAY (score, lives, etc)
–drawHUD()
end
gameInit()

– MUST return a display.newGroup()
return gameGroup

end
end[/code]

I hope this appears okay. I had a bit of trouble with the copy and paste. Also, I have left in some of the stuff that I commented out.

Any input would be helpful. There seems to be an awful lot of code involving swipetween and screenPosition.

Thanks! [import]uid: 93625 topic_id: 16261 reply_id: 61244[/import]