Can someone help me with this bug. Directors Class & Joystick

Hi guys I know you are gong to be good for this,

I’ve got a game I’m making which utilizes the directors class and the joystick code that was kindly provided by Matthew Pringle in the code exchange. It was all running smoothly before I started to try and use the directors class. Now I get a bug that is doing my head in.

/Users/CELL/Desktop/untitled folder 5/star.lua:25: attempt to index global ‘player’ (a nil value)
stack traceback:
[C]: ?

/Users/CELL/Desktop/untitled folder 5/star.lua:25: in function ‘movePlayer’
/Users/CELL/Desktop/untitled folder 5/star.lua:51: in function
?: in function <?:215>
Runtime error
So I understand that it is having trouble finding the player variable that is created in the other .lua file. Why has this only cropped up now I have started using the directors class. The only thing I can think of is because everything is now wrapped in another function ( new() ). Is this lexical scoping?

Can anyone help me?

[import]uid: 26289 topic_id: 13398 reply_id: 313398[/import]

so your player variable is in another lua file? was that global or local if global then you can access it via yourfilename.player [import]uid: 12482 topic_id: 13398 reply_id: 49194[/import]

Yeah my player variable is created in my main game file called level1.lua here

[lua]function drawPlayer()

– Load Player
playersheet = sprite.newSpriteSheet(“squid.png”, 150, 43)
– Our sprite sheet
– 32 is the width of each “box”, this is the image width divided by the number of images across
– 36 is the height of each “box”, this is the image height divided by the number of images down

playerset = sprite.newSpriteSet (playersheet, 1, 2)
sprite.add (playerset, “playerleft”, 2, 2, 300, 0)
sprite.add (playerset, “playerright”, 1, 1, 300, 0)

– The sprite set uses images 1 to 12 (all of them)
– “heroup” starts at image 1 and includes 3 frames. (In this case 1, 2, 3.)
– The “300” indicates .3 seconds per frame, the “0” indicates it will repeat until we stop it.

player = sprite.newSprite (playerset)
player.x = 300
player.y = 1700
– We insert out hero sprite

player:prepare(“playerright”)

–Load initial camera shot
camera:insert(player)
camera.x = -player.x
camera.y = -player.y + 260
physics.addBody( player, { isSensor = true } )

end[/lua]

And is needed in my joystick control file called star.lua

[lua]------------------------------------------------------------------------------------------------------------------------
module(…, package.seeall)

local function movePlayer( xStrength , yStrength )

– Set Plane Speed Mutliplyer
local speed = 8

– Move X
if xStrength ~= false then
player.x = math.floor( player.x + ( xStrength * speed ) )
end

– Move Y
if yStrength ~= false then
player.y = math.floor( player.y + ( yStrength * speed ) )
end

– Boundries
if player.x < 30 then
player.x = 30
elseif player.x > 1690 then
player.x = 1690
end
if player.y < 220 then
player.y = 220
elseif player.y > 1700 then
player.y = 1700
end

end

function playerControl( event )

local joyX = event.joyX
local joyY = event.joyY
local joyAngle = event.joyAngle
movePlayer( joyX , joyY )
animatePlayer( joyAngle )
– rotatePlayer( joyAngle, joyX )
end [/lua]

This works fine without directors class. Where would I call yourfilename.player?

Cheers [import]uid: 26289 topic_id: 13398 reply_id: 49201[/import]

Got it!! Thanks a lot. Just added

[lua]function playerControl( event )
player = level1.player -------RIGHT HERE
local joyX = event.joyX
local joyY = event.joyY
local joyAngle = event.joyAngle
movePlayer( joyX , joyY )
animatePlayer( joyAngle )
– rotatePlayer( joyAngle, joyX )
end [/lua]

And it works.
Much appreciated hgvyas123 [import]uid: 26289 topic_id: 13398 reply_id: 49258[/import]

glad that worked [import]uid: 12482 topic_id: 13398 reply_id: 49392[/import]