Hello. Okay, so from what I can tell your philosophy is to give general information to allow for learning and such. Normally I would much appreciate and applaud such a manner of doing things. Unfortunately, due to time constraints I cannot really dedicate time to learning the most efficient way of doing things, I was aiming to get only the exact information I required. I understand that I may be doing it horribly inefficiently, but if it works well enough then it is fine, this will never actually be sold or even distributed. :P
By address I simply mean a variable that is not a table and just has one number - probably not the correct term but I use cheat engine quite a bit so it was the first word that came to mind.
Also, the buttons are meant to be held, so it being possible to constantly be 1 is fine, no?
I do not think the address is a global? Perhaps it would be best if I post both my level class and player class in their entirety, seeing as they are not that long or complex. Please try not to cringe too much at my poor programming :unsure:
--======================================================================-- --== Level Factory --======================================================================-- local widget = require( "widget" ) local physics = require( "physics" ) local loadsaveM = require("loadsave") local composer = require( "composer" ) local ButtonClass = require("Classes.Button") local PlayerClass = require("Classes.PlayerClass") local Level = class() -- define level as a class (notice the capitals) Level.\_\_name = "Level" --local LevelBlockClass = require("Classes.LevelBlock") function Level:\_\_init(levelNumber, fullh,centerX,centerY,groupName) self.groupName = groupName self.playState = 0 self.xLocation = xLocation self.yLocation = yLocation self.buttonLeftHeld = 0 self.buttonRightHeld = 0 self.buttonJumpHeld = 0 local playerXSpeed = 90 local playerJumpHeight = 12 self.size = size local tile = {} local tileDimensions = fullh/15 local levelGroup = display.newGroup() local directionBeingHeld = 0 LevelTable = nil print("\nThis is the table after I have nilled it out") --loadsaveM.print\_r(LevelTable) \<-- This prints the table after it has been nilled out and is for testing -- load the tables from files called LevelTable.json and Player Stats.json LevelTable = loadsaveM.loadTable("Level"..levelNumber.."Table.json", system.ResourceDirectory) StatsTable = loadsaveM.loadTable("Player Stats.json", system.ResourceDirectory) if StatsTable[1][isStatChanged] == 1 then playerXSpeed = StatsTable[1][playerXSpeed] playerJumpHeight = StatsTable[1][playerJumpHeight] end --local player = PlayerClass:new(fullh, display.contentCenterX, display.contentCenterY, groupName, levelGroup, self.playState, self.buttonLeftHeld, self.buttonRightHeld, self.buttonJumpHeld, playerXSpeed, playerJumpHeight) --Create the background for the level local function listenerName(event) end Runtime:addEventListener("enterFrame", listenerName) -- local background = display.newImage("images/BackGround"..levelNumber..".png") background.anchorX = 0 background.anchorY = 0 background.height = fullh background.width = fullh\*3 background.x = -45 background.y = 0 background:toBack() local levelEndCarrot = display.newImage("images/BigCarrot.png") levelEndCarrot.anchorX = 0 levelEndCarrot.anchorY = 28 levelEndCarrot.width = pixelScale\*28 levelEndCarrot.height = pixelScale\*35 levelEndCarrot.x = 193\*tileDimensions levelEndCarrot.y = 7\*tileDimensions levelEndCarrot.isFixedRotation=true physics.addBody(levelEndCarrot, "static",{friction=0, bounce=0 }) levelGroup:insert(levelEndCarrot) local levelWidth = #LevelTable --print("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ "..levelWidth.." ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■") for i = 1, levelWidth do tile[i]={} for j = 1, 15 do --LevelTable[i][j] --if LevelTable[i][j] == nil then break end local columnName = ("column"..j-1) --print(LevelTable[i][columnName]) \<-- This prints the level table after it has been adapted into a format that can be read by the maker if LevelTable[i][columnName] ~= (-1) then --print(LevelTable[i][j]) \<-- This prints the level table after it has been adapted into a format that can be read by the maker tile[i][j] = display.newImage( "images/overworld"..LevelTable[i][columnName]..".png") tile[i][j].anchorX = 0 tile[i][j].anchorY = 0 tile[i][j].width = tileDimensions tile[i][j].height = tileDimensions tile[i][j].x = (i-3)\*tileDimensions tile[i][j].y = (j-1)\*tileDimensions tile[i][j]:toBack() tile[i][j].isSleepingAllowed = false physics.addBody( tile[i][j], "static",{friction=0.1, bounce=0.01 }) levelGroup:insert(tile[i][j]) end end end -- Function to handle left button events local player = PlayerClass:new(fullh, display.contentCenterX, display.contentCenterY, groupName, levelGroup, self.playState, self.buttonLeftHeld, self.buttonRightHeld, self.buttonJumpHeld, playerXSpeed, playerJumpHeight) local handleLeftButtonEvent = function( event ) if event.phase == "began" and player.buttonRightHeld == 0 then player.buttonLeftHeld = 1 print("moving?") elseif event.phase == "ended" then player.buttonLeftHeld = 0 else print("failed "..player.buttonLeftHeld.." is button state "..event.phase.." is button phase") end print("tried to do stuff") end -- Function to handle right button events local handleRightButtonEvent = function( event ) if event.phase == "began" and player.buttonLeftHeld == 0 then player.buttonRightHeld = 1 elseif event.phase == "ended" then player.buttonRightHeld = 0 end end -- Function to handle jump button events local function handleJumpButtonEvent( event ) if event.phase == "began" and buttonJumpHeld == 0 then player.buttonJumpHeld = 1 elseif event.phase == "ended" and buttonJumpHeld == 1 then player.buttonJumpHeld = 0 end end local buttonLeft = ButtonClass:new(display.contentCenterX\*0.0025, 72\*pixelScale, 12, 12, "LEFT", handleLeftButtonEvent, groupName) local buttonRight = ButtonClass:new(display.contentCenterX\*0.2, 72\*pixelScale, 12, 12, "RIGHT", handleRightButtonEvent, groupName) local buttonJump = ButtonClass:new(display.contentCenterX\*1.9, 72\*pixelScale, 12, 12, "UP", handleJumpButtonEvent, groupName) end return Level
That is the level class, which is called inside a scene. Prior to my recent “cleaning up”, the below player class, as well as the buttons, were also part of the class. I plan to make the tiles their own class as well. A side effect of doing this seems to be that the player suddenly falls extremely slowly, I have no clue if this is due to framerate drops or some sort of glitch, I plan to figure that out once I get the buttons working (assuming that doesn’t fix it).
--======================================================================-- --== PlayerAndControls Class factory --======================================================================-- local widget = require( "widget" ) local PlayerClass = class() -- define Player as a class (notice the capitals) --local LevelClass = require("Classes.Levelmaker") PlayerClass.\_\_name = "PlayerClass" local directionBeingHeld = 0 local buttonJumpHeld = 0 --======================================================================-- --== Require dependant classes --======================================================================-- local physics = require( "physics" ) --======================================================================-- --== Initialization / Constructor --======================================================================-- function PlayerClass:\_\_init(fullh,centerX,centerY, groupName, secondaryGroupName, playState, buttonLeftHeld, buttonRightHeld, buttonJumpHeld, playerXSpeed, playerJumpHeight) self.buttonLeftHeld = 0 self.buttonRightHeld = 0 self.buttonJumpHeld = 0 self.playState = playState self.playerInAir = 0 self.secondaryGroupName = secondaryGroupName self.groupName = groupName self.playerYPos = centerY print(centerY.." is centery "..self.playerYPos.." is playerypos") self.playerXPos = centerX self:drawPlayerClass(fullh,centerX,centerY) local function listenerName(event) --print("here") if player.y \> fullh then playState = 2 print("player fell "..player.y.." "..fullh) Runtime:removeEventListener("enterFrame", listenerName) end if self.buttonLeftHeld == 1 then print("test") ------------------------------------------------------------- -- Timer for increasing horizontal velocity ------------------------------------------------------------- playerTimer = timer.performWithDelay( 10, function() vx, vy = player:getLinearVelocity( ) player:setLinearVelocity( -playerXSpeed, vy) difference = centerX-player.x self.levelGroup.x = difference end, 200) ------------------------------------------------------------- -- Timer for animation sequence changing ------------------------------------------------------------- animationTimer = timer.performWithDelay( 0, function() player:setSequence( "walking" ) player:play() player.xScale = -1\*pixelScale end, 1000) else print(self.buttonLeftHeld) player:setLinearVelocity( 0) directionBeingHeld = 0 player:setSequence( "standing" ) player:play() if playerTimer then timer.cancel(playerTimer) playerTimer = nil end if animationTimer then timer.cancel(animationTimer) animationTimer = nil end end if buttonRightHeld == 1 then ------------------------------------------------------------- -- Timer for increasing horizontal velocity ------------------------------------------------------------- playerTimer = timer.performWithDelay( 10, function() vx, vy = player:getLinearVelocity( ) player:setLinearVelocity( playerXSpeed, vy) difference = centerX-player.x self.levelGroup.x = difference end, 200) ------------------------------------------------------------- -- Timer for animation sequence changing ------------------------------------------------------------- animationTimer = timer.performWithDelay( 0, function() player:setSequence( "walking" ) player:play() player.xScale = 1\*pixelScale end, 1000) else player:setLinearVelocity(0) directionBeingHeld = 0 player:setSequence( "standing" ) player:play() if playerTimer then timer.cancel(playerTimer) playerTimer = nil end if animationTimer then timer.cancel(animationTimer) animationTimer = nil end end if buttonJumpHeld == 1 and playerInAir == 0 then print("jumping") player:applyLinearImpulse( 0, playerJumpHeight) end print(buttonRightHeld) end Runtime:addEventListener("enterFrame", listenerName) end --======================================================================-- --== Code / Methods --======================================================================-- function PlayerClass:drawPlayerClass(fullh,centerX,centerY) --======================================================================-- --== Set up player spritesheet --======================================================================-- -- consecutive frames local sequenceData = { {name="walking", start=1, count=8, time=1000, loopCount = 0, loopDirection = "forward"}, {name="standing", start=1, count=1, time=1000, loopCount = 0, loopDirection = "forward"}, {name="jumping", start=9, count=1, time=1000, loopCount = 0, loopDirection = "forward"} } local playerOptions = { -- Array of tables representing each frame (required) frames = { -- Frame 1 { x = 0, y = 0, width = 9, height = 11 }, -- Frame 2 { x = 9, y = 0, width = 9, height = 11 }, -- Frame 3 { x = 18, y = 0, width = 9, height = 11 }, -- Frame 4 { x = 27, y = 0, width = 9, height = 11 }, -- Frame 5 { x = 36, y = 0, width = 9, height = 11 }, -- Frame 6 { x = 45, y = 0, width = 9, height = 11 }, -- Frame 7 { x = 54, y = 0, width = 9, height = 11 }, -- Frame 8 { x = 63, y = 0, width = 9, height = 11 }, -- Frame 9 { x = 72, y = 0, width = 9, height = 11 }, }, -- Optional parameters; used for scaled content support --sheetContentWidth = 1024, --sheetContentHeight = 1024 } local imageSheet = graphics.newImageSheet( "images/BunnyProtag.png", playerOptions ) --======================================================================-- --== Initializate player --======================================================================-- player = display.newSprite( imageSheet, sequenceData ) player:setSequence( "standing" ) player:play() player:scale( pixelScale, pixelScale ) player.width = pixelScale\*9 player.height = pixelScale\*11 player.x = centerX player.y = centerY physics.addBody(player, "dynamic",{friction=0.1, bounce=0.01 }) player.isFixedRotation=true player.isSleepingAllowed = false --function to handle collision to ground player.myName = "player" local isContact = 0 local function onLocalCollision( self, event ) if ( event.phase == "began" ) --[[and event.y == 0]] then isContact = isContact + 1 playerInAir = 0 elseif ( event.phase == "ended" ) then isContact = isContact - 1 playerInAir = 1 end if isContact \> 0 then playerInAir = 0 elseif isContact \< 0 then playerInAir = 1 end end player.collision = onLocalCollision player:addEventListener( "collision" ) self.groupName:insert( player ) self.secondaryGroupName:insert( player ) return player end function PlayerClass:displayInfo() end --======================================================================-- --== Return factory --======================================================================-- return PlayerClass
This is the player class. All I am trying to do is get it so I can change a variable inside the playerclass and have it actually update. My only solution right now that falls within my knowledge would be to make the button variables globals. Unless I am misunderstanding how complex it would be to implement the things you suggest above, I think I may just ask my teacher or classmates for a more simple solution.
I am sorry if I should not have posted my entire code, but I figured it was small enough to where it was not that much an issue.
If you are still interested in helping and want to call me something just call me Stanley.