Creating an object for character in game

Hi there,
I’ve got this error after call a function to change our character’s animation from stand to turn left

Paused at file main.lua  
Type 'help' for commands  
\> run  
... Business/Game Projects/Hungry Bird/optionsscene.lua:28: attempt to call method 'turnLeft' (a nil value)  
Runtime script error at file optionsscene.lua line 28  

Please help me solve this error and here my “Prince” class

[code]
module(…, package.seeall)

function newPrince( params )
require “sprite”

local sheet1, instance1, spriteSet1

sheet1 = sprite.newSpriteSheet( “man.png”, 40, 60 )

spriteSet1 = sprite.newSpriteSet(sheet1, 1, 24)

sprite.add( spriteSet1, “left”, 9, 8, 500, 0 )
sprite.add( spriteSet1, “stand”, 17, 8, 500, 0 )

instance1 = sprite.newSprite( spriteSet1 )

instance1.x = display.contentWidth / 3
instance1.y = display.contentHeight / 4 * 3
instance1.xScale = 1.5
instance1.yScale = 1.5

instance1:prepare(“stand”)
instance1:play()

local function turnLeft()
instance1:prepare(“left”)
instance1:play()
end

return instance1
end

[/code] [import]uid: 9190 topic_id: 2976 reply_id: 302976[/import]

The error tells you that the object you have there, has no turnLeft method. Can you please post the code from optionsscene.lua where you initialize/create that object and where you call that medthod?

btw, place that require “sprite” statement in the 2nd line of the module. [import]uid: 5712 topic_id: 2976 reply_id: 8562[/import]

Hi, thanks for your help.

I have no problem with " require “sprite” " line. My “prince” character was initial and play “stand” animation normally.
As your request, here is the code from optionscene

local function buttonOption\_onpress ()  
 --dispose()  
 myPrince:turnLeft()  
 director:changeScene("mainmenuscene","overFromLeft")  
  
end  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 -- Background  
 local background = display.newImage("GFX/background/default.png")  
 localGroup:insert(background)  
  
 myPrince = prince.newPrince();  
 localGroup:insert(myPrince)  

Origin file:

optionscene.lua

[code]
module(…, package.seeall)

require “slider”

local prince = require(“prince”)

local myPrince
local press = function( event )
print( “press” )
end
local release = function( event )
print( “release” )
end

local eventHandler = function( event )
–print( "id = " … event.id … “, phase = " … event.phase … " value=” … event.value )
–[[
if event.value == 100 then
director:changeScene(“mainmenuscene”,“overFromLeft”)
end
]]–
end

local tm = function() print("Texture memory used: " … system.getInfo(“textureMemoryUsed”)) end

local function dispose ()
myPrince:dispose()
end

local function buttonOption_onpress ()
–dispose()
myPrince:turnLeft()
director:changeScene(“mainmenuscene”,“overFromLeft”)

end

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

– Background
local background = display.newImage(“GFX/background/default.png”)
localGroup:insert(background)

myPrince = prince.newPrince();
localGroup:insert(myPrince)

local buttonOption = ui.newButton{
default = “GFX/mainmenu/buttonover.png”,
over = “GFX/mainmenu/buttondefault.png”,
x = display.contentWidth / 2 - 2,
y = display.contentHeight - 40,
onRelease = buttonOption_onpress,
text = “BACK”,
emboss = true,
textColor = {123, 123, 123, 0}
}

localGroup:insert(buttonOption)

local mySlider = slider.newSlider(
{
track = “GFX/slider/track.png”,
thumbDefault = “GFX/slider/thumb.png”,
thumbOver = “GFX/slider/thumbDrag.png”,
onPress = press,
onRelease = release,
onEvent = eventHandler,
}
)

mySlider.x = display.contentWidth / 2
mySlider.y = display.contentHeight / 2
mySlider.value = 50

localGroup:insert(mySlider)

– MUST return a display.newGroup()
return localGroup
end

[/code] [import]uid: 9190 topic_id: 2976 reply_id: 8563[/import]

Finally!!

My method should be

function instance1:turnLeft()   

instead of

function turnLeft()   

Thanks! [import]uid: 9190 topic_id: 2976 reply_id: 8580[/import]