Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Sprite:setLocation(x,y) acting weird in derived class — Gideros Forum

Sprite:setLocation(x,y) acting weird in derived class

jasimmonsjasimmons Member
edited March 2012 in Bugs and issues
Hey everyone,

I'm just getting started with Gideros Mobile to develop a game for a class. I followed some beginner tutorials and am getting a feel for it all. However, I have hit a wall and am not sure what is wrong...

--------------------------------

file: dog.lua
Dog = Core.class(Sprite)
 
function Dog:init()
    self = Bitmap.new(Texture.new("dog.png"))
    stage:addChild(self)
end
--------------------------------

file: main.lua
require "dog"
 
local d = Dog.new()
print(d:getPosition())
 
-- As expected, this returns "0 0".
-- As expected, the "dog.png" image is displayed in the upper left corner.
 
d:setPosition(100,100)
print(d:getPosition())
 
-- As expected, this returns "100 100"
-- Not as expected, the "dog.png" image is still displayed in the upper left corner (didn't move)
--------------------------------

So it seems setPosition() and getPosition() are reading values/writing values properly, but the image itself isn't moving. Could anybody point me in the right direction as to why?

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited March 2012
    Firstly, you don't need to use require "dog" as all files ar loaded automatically.

    Secondly, inside Dog:init method, you create bitmap and add it to the Stage, so bitmap is not associated with class Dog in any way, you give another meaning to variable self, which is now Bitmap instance, not Dog's instance. Thus when you move Dog's instance, it does not move the image.

    What you wanted to do is:
    Dog = Core.class(Sprite)
     
    function Dog:init()
        local dog = Bitmap.new(Texture.new("dog.png"))
        self:addChild(dog)
    end
    And then create instance, and add it to the stage:
    local d = Dog.new()
    stage:addChild(d)
    And everything should work as you expected. ;)
  • Perfect explanation, and it works. Thank you! :)
  • MikeHartMikeHart Guru
    edited March 2012
    or like this:
    Dog = Core.class(Sprite)
     
    function Dog:init()
        local dog = Bitmap.new(Texture.new("dog.png"))
        self:addChild(dog)
        stage:addChild(self)
    end
     
    local d = Dog.new()
    d:setPosition(20,50)
Sign In or Register to comment.