add a player to the scene group

Hey i am making a game (first one) and i made a player class with image in it, and i want to add the player to the camera but getting an error, how do i manage to do it?

Player class:

local physics = require "physics" local Player = {} local Player\_mt = { \_\_index = Player } -- metatable local img physics.start() ------------------------------------------------- -- PRIVATE FUNCTIONS ------------------------------------------------- ------------------------------------------------- -- PUBLIC FUNCTIONS ------------------------------------------------- function Player.new( x, y) -- constructor img=display.newImage("crate.png") img.x = x img.y = y physics.addBody( img, { friction=1.0, density=1.0, bounce=0.3, radius=35 } ) img.isFixedRotation = true local newPlayer = { -- img=display.newImage("crate.png",100,100); x = x or 0, y = y or 0, } return setmetatable( newPlayer, Player\_mt ) end ------------------------------------------------- function Player:getX() return self.x end ------------------------------------------------- function Player:getY() return self.y end ------------------------------------------------- function Player:setX(x) self.x = x end function Player:setY(y) self.y = y end function Player:moveRight() self.x = self.x+2 img.x = self.x end function Player:moveLeft() self.x = self.x-2 img.x = self.x end function Player:Jump() img:applyForce( 0, -300, self.x, self.y ) end function Player:isJumping(ground) if img.y\>ground then self.x = img.x self.y = img.y return true else self.x = img.x self.y = img.y print("crate:"..img.x..","..img.y) print("player:"..self.x..","..self.y) return false end end ------------------------------------------------- return Player

Level 1 class:

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local perspective=require("perspective") local camera=perspective.createView() local Player = require("Player") local controlLeft local controlRight local controlJump local playerTexture local player local isJumping = false local where = "none" local scene = composer.newScene() local Player = require("Player") player = Player.new(100,200) local physics = require "physics" physics.start() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 local function ons(event) if event.phase == "began" then -- make character jump forward --player:setX(playerTexture.x) --player:setY(playerTexture.y) end return true end local function gameLoop() player:isJumping(200) if where == "left" then player:moveLeft() elseif where == "right" then player:moveRight() end --print ("crate:") end local function Jump(event) player:Jump() isJumping = true end local function moveLeft(event) local phase = event.phase where = "left" --display.currentStage:setFocus(moveLeft) if phase == "ended" then where = "none" --display.currentStage:setFocus(nil). end end local function moveRight(event) local phase = event.phase where = "right" --display.currentStage:setFocus(moveRight) if phase == "ended" then where = "none" --display.currentStage:setFocus(nil). end end function scene:create( event ) ---------------------------------------------------------------------- local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 grass.x, grass.y = 0, display.contentHeight local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) ------------------------------------------------------------------------ ------------------ player:setX(100) player:setY(200) ------------------ ----------------------------- local sceneGroup = self.view ------------------------------ ------------------------------------------------------------- local background = display.newRect( 0, 0, screenW, screenH ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( .5 ) --------------------------------------------------------------- -------------------------------------------------- controlLeft = display.newRect(50,screenH-40,70,30) controlLeft:addEventListener("touch",moveLeft) controlRight = display.newRect(controlLeft.x+controlLeft.width+10,screenH-40,70,30) controlRight:addEventListener("touch",moveRight) controlJump = display.newCircle(400,screenH-40,20) controlJump:addEventListener("touch",Jump) ------------------------------------------------------ ------------------------------------------------------------- camera:add(player,1,true) camera:add(grass,2,false) camera:add(background,3,false) camera:setBounds(display.contentWidth/2,display.contentWidth,-1000,display.contentHeight-160) camera.damping=3 -------------------------------------------------------------- sceneGroup:insert(camera) sceneGroup:insert(controlLeft) sceneGroup:insert(controlRight) sceneGroup:insert(controlJump) -- sceneGroup:insert( playerTexture ) end function scene:show( event ) local sceneGroup = self.view camera:track() physics.start() local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) Runtime:addEventListener("enterFrame",gameLoop) Runtime:addEventListener("touch",ons) ----------------------------------------------------------------------------------------- return scene

caveat:  i skimmed your code for about a second…

but you should probably add “player.img” rather than “player” to group

[quote name=“davebollinger” post=“314079” timestamp=“1450032917”]caveat:  i skimmed your code for about a second… but you should probably add “player.img” rather than “player” to group[/quote] But i need to add the image from the player class to the group scene, how do i do it?

your “img” remains local to the module, you need something like “newPlayer.img=img” to expose it

caveat:  i skimmed your code for about a second…

but you should probably add “player.img” rather than “player” to group

[quote name=“davebollinger” post=“314079” timestamp=“1450032917”]caveat:  i skimmed your code for about a second… but you should probably add “player.img” rather than “player” to group[/quote] But i need to add the image from the player class to the group scene, how do i do it?

your “img” remains local to the module, you need something like “newPlayer.img=img” to expose it