Hi all,
I am running in to an issue that is stumping me and I was hoping for some tips to point me in the right direction.
The problem I am facing is how to add my character sprite and associated physics body when the Player:init() method is called. I am running in to an error: "index '__userdata' cannot be found" when I try to add the player to the Player object:
Just for some background, I am using SceneManager, and I am adding the Player to my Game scene like so:
self.player = Player:new(self.world, centerX, centerY, 5, levelSpeed, 0, "gfx/pig.png")
self:addChild(self.player) |
Here is my Player class where the problem resides:
Player = Core.class(Sprite)
-- b2World: the box2d World object to add the player body to
function Player:init(self, b2World, xVal, yVal, lives, speed, burnRadius, spriteLocation)
self.lives = lives
self.speed = speed
self.burnRadius = burnRadius
local player = Bitmap.new(Texture.new(spriteLocation))
player:setAnchorPoint(0.5,0.5)
player:setPosition(xVal, yVal)
local radius = player:getWidth()/2
local body = b2World:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(player:getX(),player:getY())
body:setAngle(player:getRotation() * math.pi/180)
local circle = b2.CircleShape.new(0,0,radius)
local fixture = body:createFixture{
shape = circle,
density = 1.0,
friction = 0.1,
restitution = 0.8
}
player.body = body
player.body.type = "player"
--THE PROBLEM LINE BELOW-----------------------------
self:addChild(player)
end |
Comments
I looked at your code, and all I saw is the problem with the constructor, you initiate class like this:
Likes: mt206
This had the nice side effect of fixing a peculiarity in my Player:init() call. I had to add 'self' as one of the arguments passed to the init() method which I didn't understand the need for (since the colon means it is self referencing anyways). I was able to remove that after fixing my error.
I am really liking Gideros (not so much a fan of Lua but that's another story) and it's good to see that there is a community supporting it.
Thanks for your help!
Likes: gorkem