It looks like you're new here. If you want to get involved, click one of these buttons!
MovingBackground = gideros.class(Sprite) function MovingBackground:init() self.pixelsPerSecond = 30; self.backgroundTexture = Texture.new("art/background.png", true, {wrap = Texture.REPEAT}) self.position=0 self.originX = (application:getLogicalWidth() - application:getDeviceWidth())/2 self.originY = (application:getLogicalHeight() - application:getDeviceHeight())/2 self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end function MovingBackground:onEnterFrame(event) local matrix = Matrix.new(1, 0, 0, 1, 0, 0) matrix:setPosition(self.position,self.position) self.position = self.position + self.pixelsPerSecond * event.deltaTime; if (self.position > 256) then self.position = 0 end if self:getNumChildren() > 0 then self:removeChildAt(1) end local shape = Shape.new() shape:setFillStyle(Shape.TEXTURE, self.backgroundTexture, matrix) shape:beginPath(Shape.NON_ZERO) shape:moveTo(self.originX,self.originY) shape:lineTo(application:getDeviceWidth(), self.originY) shape:lineTo(application:getDeviceWidth(), application:getDeviceHeight()) shape:lineTo(self.originX, application:getDeviceHeight()) shape:lineTo(self.originX,self.originY) shape:endPath() self:addChild(shape) end |
Comments
2) you can create 2 shapes and just change their x, y position to move them and when one goes out of screen, jump it back after the current one, etc
Likes: sergiandreplace
about moving them, afaik I need 4, movement is diagonal
Likes: totebo
Likes: antix, hgy29
Likes: antix
Likes: n1cke
Plus maybe have the scroll in your main event listener rather than in it's own - events are a killer!
Likes: antix
https://deluxepixel.com
@antix: Bitmap is 10% faster than textured mesh in my synthetic tests. However I like textured mesh features. Would be nice to add `setTexturePosition`, `setDimensions` and `setColor` (if you don't use a texture) methods to Bitmap i.e. mix-in Pixel class methods to make it flexible.
> You may only need to set the vertices once
@SinisterSoft: You are right, it affects performance a little. I wanted to show that you can also dynamically modify background width and height i.e. clip it. Useful for roll/unroll animation effects. You can safely move it if you don't need it.
Likes: SinisterSoft
@n1cke cool, I'll revert my changes right away
Likes: SinisterSoft