Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Scrolling background - properly creating a new class — Gideros Forum

Scrolling background - properly creating a new class

stetsostetso Member
edited November 2016 in General questions
Hi everyone,
I am trying to figure out how everything works in Gideros, so this is most likely a stupid beginner question. I am trying to implement an infinitely scrolling background using the Pixel class in 2016.10. It was easy when doing everything just in the main.lua file "by hand" (as long as the texture has power-of-two size :-) ). However, then I tried about wrapping everything in a class but it does not seem to work:
ScrollingBackground = Core.class(Pixel)
 
function ScrollingBackground:init(texturePath, x, y, width, height, scrollx, scrolly)
	self:setPosition(x, y)
	self:setWidth(width)
	self:setHeight(height)
	self:setTexture(Texture.new(texturePath, false, {wrap = Texture.REPEAT}))
	self.scrollx = scrollx or 0
	self.scrolly = scrolly or 0
 
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
 
function ScrollingBackground:setScrollSpeed(dx, dy)
	self.scrollx = dx
	self.scrolly = dy
end
 
function ScrollingBackground:getScrollSpeed()
	return self.scrollx, self.scrolly
end
 
function ScrollingBackground:onEnterFrame(event)
	local tx, ty = self:getTexturePosition()
	tx = tx + self.scrollx * event.deltaTime
	ty = ty + self.scrolly * event.deltaTime
 
	self:setTexturePosition(tx, ty)
end
When just trying to create an instance in main.lua like so:
bg = ScrollingBackground.new("flowers_bg.jpg", 0, 0, 320, 480, 0, 1)
stage:addChild(bg)
I get the error:
[string "property.lua"]:46: bad argument #1 to '__new' (number expected, got string)
stack traceback:
	[string "property.lua"]:46: in function '__new'
	[string "property.lua"]:59: in function 'new'
	main.lua:2: in main chunk
Although the class is super simple, I can't seem to be able to spot the mistake. Any help would be really appreciated.
Thanks in advance!

Comments

  • keszeghkeszegh Member
    edited November 2016 Accepted Answer
    probably the problem is that Pixel has already an overloaded init function, and because of this somehow your new init function does not work as default. i don't know enough about lua to help you with that but i guess it would be cleaner to make pixel only a property of your class. this works e.g.:
    ScrollingBackground = Core.class(Sprite)
     
    function ScrollingBackground:init(texturePath, x, y, width, height, scrollx, scrolly)
    	self.pixel=Pixel.new()
    	self:addChild(self.pixel)
    	self:setPosition(x, y)
    	self.pixel:setWidth(width)
    	self.pixel:setHeight(height)
    	self.pixel:setTexture(Texture.new(texturePath, false, {wrap = Texture.REPEAT}))
    	self.scrollx = scrollx or 0
    	self.scrolly = scrolly or 0
     
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function ScrollingBackground:setScrollSpeed(dx, dy)
    	self.scrollx = dx
    	self.scrolly = dy
    end
     
    function ScrollingBackground:getScrollSpeed()
    	return self.scrollx, self.scrolly
    end
     
    function ScrollingBackground:onEnterFrame(event)
    	local tx, ty = self.pixel:getTexturePosition()
    	tx = tx + self.scrollx * event.deltaTime
    	ty = ty + self.scrolly * event.deltaTime
     
    	self.pixel:setTexturePosition(tx, ty)
    end
     
    bg = ScrollingBackground.new("flowers_bg.jpg", 0, 0, 320, 480, 0, 1)
    stage:addChild(bg)

    Likes: stetso, antix

    +1 -1 (+2 / -0 )Share on Facebook
  • Ah, good to know. Still finding my way around Lua and this behavior is new to me too.
    Thanks a lot @keszegh!
  • n1cken1cke Maintainer
    edited November 2016
    Yes, Pixel and few other classes have overloaded constructors. Although you can use this method to inherit from classes without Gideros help:
    ScrollingBackground = {} -- create class without `Core.class`
     
    -- we are implementing `new` method directly without `init` help
    function ScrollingBackground.new(texturePath, x, y, width, height, scrollx, scrolly)
    	local self = Pixel.new() -- create instance without `init`
    	setmetatable(self, Pixel) -- inheritance from Pixel class
            -- your code here
    	return self -- our `new` method returns instance
    end
    +1 -1 (+5 / -0 )Share on Facebook
  • I like @keszegh's approach as it then becomes easy to add more layers for a parallax effect.
  • I think possibly the best thing to do re a large scrolling map would be to draw your backgrounds to render targets - eg 2 512x256 images to a 512x512 render texture.
    Then use the TileMap feature to render these images using smaller cells. Because this is made into a mesh of cells that is the size of the display screen then it will take less time to draw as the clipping area will effectively be smaller and less memory accessed by the gpu.

    Likes: MoKaLux

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.