Couldn’t agree more Horace. I just can’t crack this nut though and can’t see how I can simplify this function anymore, other than a few performance related changes. Any help would be awesome
What you are looking at is the accelerometer event listener. It is one of the only constant event listeners in my game as I have eliminated the main game loop (among others) to increase FPS
[lua]
–Player Movement Function (Runtime Listener)
playerMovement = function(event)
if accelerometerReady then
local player = player
local LevelBoundsXmin = LevelBoundsXmin
local playerBoundsXMax = playerBoundsXMax
local playerBoundsYMin = playerBoundsYMin
local playerBoundsYMax = playerBoundsYMax
local _yG = _yG – This value is taken from the calibration button
local _xG = _xG – This value is taken from the calibration button
local newXPos
local newYPos
local flr = math.floor
local atn2 = math.atan2
local pi = math.pi
– Set User Calibration X&Y Values
local currentYValue = event.xGravity - _xG
local currentXValue = event.yGravity - _yG
–Set Speed Of Player
currentYValue = currentYValue * player.speed
currentXValue = currentXValue * player.speed
–Store New Pos
newXPos = player.x + currentXValue
newYPos = player.y + currentYValue
–Move Player To New Pos If In Level Bounds
if newXPos > playerBoundsXMin and newXPos < playerBoundsXMax then
player.x = newXPos
end
if newYPos > playerBoundsYMin and newYPos < playerBoundsYMax then
player.y = newYPos
end
–Work Out Rotation
– Take X&Y radian values and convert into degrees
local currentRotation = (flr(atn2( currentYValue , currentXValue ) * ( 180 / pi ) ) )
if currentRotation < 0 then
currentRotation = 360 + currentRotation
end
local isWithinRightFacingLimits = currentRotation >= 0 and currentRotation <= 25 or currentRotation >= 325 and currentRotation <= 360
local isWithinLeftFacingLimits = currentRotation >= 155 and currentRotation <= 205
if currentXValue >= 0.2 then
– Facing Left
player.xScale = 1 – Using this to reflect image for now. Will be replaved by left facing sprite
if isWithinLeftFacingLimits then
player.rotation = currentRotation
end
elseif currentXValue < 0.2 then
– Facing Right
player.xScale = -1 – Using this to reflect image for now. Will be replaved by right facing sprite
if isWithinRightFacingLimits then
player.rotation = currentRotation
end
end
–Camera Follow Player w Centred View. If within Camera Bounds
if gameIsActive then
local cameraBoundsXMin = cameraBoundsXMin
local cameraBoundsXMax = cameraBoundsXMax
local cameraBoundsYMin = cameraBoundsYMin
local cameraBoundsYMax = cameraBoundsYMax
–Camera X
if player.x >= cameraBoundsXMin and player.x <= cameraBoundsXMax then
camera.x = -player.x + _W/2
end
–Camera Y
if player.y >= cameraBoundsYMin and player.y <= cameraBoundsYMax then
camera.y = -player.y + _H/2
end
end
end
end
[/lua]
I’m thinking of using boolean flags when the player changes direction to change to limits to which it can be rotated. What do you think of this idea?
Thanks [import]uid: 26289 topic_id: 35884 reply_id: 142727[/import]