Adding player class instance into sceneGroup (to draw on-screen)

Hi,

I’m struggling a lot here and desperately hoping I’m not just being a total fool.

I have a character class that I have created using 30logglobal, I can’t for some reason figure out how to draw it in the game.lua sceneGroup using the insert command.

character.lua


local Character = class()
Character.__name = “Character”

function Character:__init( name )
    self.name = name
   
    self = display.newImageRect( “Content/hero.png”, 0, 15, 50, 50 )
    self.rotation = 90
    self.anchorX, self.anchorY = 0.5, 0.6
    self.direction =  “right”
end

return Character


player.lua


local Character = require( “Character” )

local Player = Character:extends()
Player.__name = “Player”

function Player:__init( name )
    Player.super.__init( self, name )    

return Player


game.lua


local composer = require( “composer” )
local scene = composer.newScene()

local Player = require( “player” )

local player = Player:new( “playerOne” )

local widget = require “widget”

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )

    local sceneGroup = self.view

    local background = display.newImageRect( “Content/levelOneMap.png”, display.actualContentWidth, display.actualContentHeight )
    background.anchorX = 0
    background.anchorY = 0
    background.x = 0 + display.screenOriginX
    background.y = 0 + display.screenOriginY

    sceneGroup:insert( background )
    sceneGroup:insert( player )
end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
    
    if phase == “will” then
       
    elseif phase == “did” then

    end
end

function scene:hide( event )
    local sceneGroup = self.view
    
    local phase = event.phase
    
    if event.phase == “will” then

    elseif phase == “did” then

    end    
    
end

function scene:destroy( event )

    local sceneGroup = self.view

end

scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene


Many thanks in advance for any help that can be offered!

Alex

Over complication of code… try this

function scene:create( event ) local sceneGroup = self.view local background = display.newImageRect(sceneGroup, "Content/levelOneMap.png", display.actualContentWidth, display.actualContentHeight ) background.x, background.y = display.actualContentWidth/2, display.actualContentHeight/2 --create new player local player = display.newImageRect(sceneGroup, "Content/hero.png", 0, 15, 50, 50 ) player.rotation = 90 end

Hi adrianm,

Thank you for your advice, this actually helped me by stepping back. I do however want entirely separated entities that are self-contained within their classes. After taking your approach though, I re-entered the code into the class and found the solution in doing this:

character.lua


   self.image = display.newImage( “Content/hero.png”, 0, 15, 50, 50 )


game.lua


local player = Player:new( self, “playerOne” )

    sceneGroup:insert( player.image )


It seems so obvious now but I realise that when I tried this same thing previously, I hadn’t added ‘self’ when initialising the ‘player’ in the game.lua file.

I’m getting there now, I just need to figure out how to apply the rotation and anchors using this approach.

Thanks again for your help!

Great glad it helped…

Personally, I would advise player.lua to return a display group on it’s new() like this.  You shouldn’t need to pass a reference of self as that is passed by default.

local aNewPlayer = Player:new( "playerOne" ) sceneGroup:insert( aNewPlayer )

This way you are free to modify and extend the display capabilities of the player in the future without making any code changes to game.lua

Over complication of code… try this

function scene:create( event ) local sceneGroup = self.view local background = display.newImageRect(sceneGroup, "Content/levelOneMap.png", display.actualContentWidth, display.actualContentHeight ) background.x, background.y = display.actualContentWidth/2, display.actualContentHeight/2 --create new player local player = display.newImageRect(sceneGroup, "Content/hero.png", 0, 15, 50, 50 ) player.rotation = 90 end

Hi adrianm,

Thank you for your advice, this actually helped me by stepping back. I do however want entirely separated entities that are self-contained within their classes. After taking your approach though, I re-entered the code into the class and found the solution in doing this:

character.lua


   self.image = display.newImage( “Content/hero.png”, 0, 15, 50, 50 )


game.lua


local player = Player:new( self, “playerOne” )

    sceneGroup:insert( player.image )


It seems so obvious now but I realise that when I tried this same thing previously, I hadn’t added ‘self’ when initialising the ‘player’ in the game.lua file.

I’m getting there now, I just need to figure out how to apply the rotation and anchors using this approach.

Thanks again for your help!

Great glad it helped…

Personally, I would advise player.lua to return a display group on it’s new() like this.  You shouldn’t need to pass a reference of self as that is passed by default.

local aNewPlayer = Player:new( "playerOne" ) sceneGroup:insert( aNewPlayer )

This way you are free to modify and extend the display capabilities of the player in the future without making any code changes to game.lua