{"id":15722435,"url":"https://github.com/defold/sample-parallax","last_synced_at":"2025-07-03T00:05:07.285Z","repository":{"id":208572181,"uuid":"721963605","full_name":"defold/sample-parallax","owner":"defold","description":"This sample shows how to create a parallax effect","archived":false,"fork":false,"pushed_at":"2024-10-22T09:25:27.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-02T11:04:59.641Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/defold.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-22T06:31:09.000Z","updated_at":"2024-10-22T09:25:30.000Z","dependencies_parsed_at":"2024-10-27T16:31:36.357Z","dependency_job_id":"117faa74-f693-488e-848d-d44f8f850a02","html_url":"https://github.com/defold/sample-parallax","commit_stats":null,"previous_names":["defold/sample-parallax"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/defold/sample-parallax","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-parallax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-parallax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-parallax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-parallax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/defold","download_url":"https://codeload.github.com/defold/sample-parallax/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-parallax/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263234940,"owners_count":23434917,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T22:07:39.806Z","updated_at":"2025-07-03T00:05:07.259Z","avatar_url":"https://github.com/defold.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Parallax\n\n[![Parallax](https://img.youtube.com/vi/UdNA7kanRQE/0.jpg)](https://www.youtube.com/watch?v=UdNA7kanRQE)\n\nIn this sample, we demonstrate how to use a parallax effect to simulate depth in the game world.\nThere are two layers of clouds, where one of the layers has the appearance of being further back than the other. There is also an animated saucer for flavor.\n\nThe cloud layers are built as two separate game objects, containing a *Tile Map* and *Script* each.\nThe layers are moved at different speeds, to give the parallax effect. This is done in `update()` of *background1.script* and *background2.script* below.\n\n```lua\n-- file: background1.script\n\nfunction init(self)\n    msg.post(\"@render:\", \"clear_color\", { color = vmath.vector4(0.52, 0.80, 1, 0) } )\nend\n\n-- the background is a tilemap in a gameobject\n-- we move the gameobject for the parallax effect\n\nfunction update(self, dt)\n    -- decrease x-position by 1 units per frame for parallax effect\n    local p = go.get_position()\n    p.x = p.x + 1\n    go.set_position(p)\nend\n```\n\n```lua\n-- file: background2.script\n\n-- the background is a tilemap in a gameobject\n-- we move the gameobject for the parallax effect\n\nfunction update(self, dt)\n    -- decrease x-position by 0.5 units per frame for parallax effect\n    local p = go.get_position()\n    p.x = p.x + 0.5\n    go.set_position(p)\nend\n```\n\nThe saucer is a separate game object, containing a *Sprite* and a *Script*.\nIt is moved to the left at a constant speed. The up-down-motion is obtained by animating its y-component around a fixed value using the Lua sine function (`math.sin()`). This is done in `update()` of *spaceship.script*.\n\n\n```lua\n-- file: spaceship.script\n\nfunction init(self)\n    -- remeber initial y position such that we\n    -- can move the spaceship without changing the script\n    self.start_y = go.get_position().y\n    -- set counter to zero. use for sin-movement below\n    self.counter = 0\nend\n\nfunction update(self, dt)\n    -- decrease x-position by 2 units per frame\n    local p = go.get_position()\n    p.x = p.x - 2\n\n    -- move the y position around initial y\n    p.y = self.start_y + 8 * math.sin(self.counter * 0.08)\n\n    -- update position\n    go.set_position(p)\n\n    -- remove shaceship when outside of screen\n    if p.x \u003c - 32 then\n        go.delete()\n    end\n\n    -- increase the counter\n    self.counter = self.counter + 1\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefold%2Fsample-parallax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefold%2Fsample-parallax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefold%2Fsample-parallax/lists"}