0

I am trying to write a code that does paralax scrolling. All the sprites in the "pieces" table need to shoot up into the air, then have their position reset to y=200 and x=randomnumber. Later on, I'm going to make them smaller or bigger depending on how fast they're moving for a true 3D effect.

Here's a naive attempt:

local speed = math.random(250,1000)
pieces = { "sprite", "sprite1", "sprite2", "sprite3", "sprite4",
"sprite5", "sprite6", "sprite7", "sprite8", "sprite9", "sprite10",
"sprite11", "sprite12", "sprite13", "sprite14", "sprite15" } 

function update(self, dt)
    for i, v in ipairs(pieces) do
        p = go.get_position(v)
        p.y = p.y + speed * dt
        print(v)
            if p.y > 800  then
            p.y = -200
            p.x = math.random(1,25) * math.random(10,35)*2
            local speed = math.random(250,1000)
        end
        go.set_position(v)
    end
end

If i print "v" inside the ipairs i get the desired output (sprite, sprite1, sprite, etc), so I thought that what I have above would work. It doesn't. Any help?

6
  • 1
    Well, you wanted to loop it - it loops ok, printing v shows it. You'll have to explain what you want and what you get. "It doesn't work" is not an explanation. If it doesn't alter 'speed' - it's because you're declaring local variable 'speed' inside of 'if p.y>800' branch, so new random value is not used outside if that branch. If it doesn't work in some other way, you'll have to explain. Commented Nov 20, 2016 at 11:32
  • You're right. I forgot to put that the specific problem is that p = go.get_position(v) gives an error message. I believe I am not correctly referencing the object. Commented Nov 20, 2016 at 12:30
  • What's that 'go'? I assume it's a collection of game_objects, indexed by names you have in 'pieces' array. But where it is in your sample? Does it have functions get_position()/set_position()? How those defined? Commented Nov 20, 2016 at 12:41
  • go stands for game objects (in Defold). go.get_position(a) gets the position of the object called "a". If I put go.get_position(".") the code works fine (but it moves the object that the script is attached to, because "." = self). the problem is definitely in the way that go.get_position(v) references v. I think it's getting the word "sprite" instead of the instance "sprite" (or something similar), but I don't know how to reference the object rather than the word. Thanks for your help so far. Commented Nov 20, 2016 at 13:07
  • Defold manual say you can pass a string as object's id when calling to get_position, so at least here it's correct. Do you have all those objects created already? Do you run script within Defold, or testing outside of it? What's exact error message? Commented Nov 20, 2016 at 13:17

1 Answer 1

1

Here's the correct code which I figured out. If anyone sees this and needs it explaining, let me know.

local speed = math.random(250,1000)
--pieces = { "sprite1", "sprite2", "sprite3", "sprite4", "sprite5", "sprite6", "sprite7", "sprite8", "sprite9", "sprite10", "sprite11", "sprite12", "sprite13", "sprite14", "sprite15" } 
pieces = { "go1", "go2", "go3" } 

function update(self, dt)
    for i, v in ipairs(pieces) do
        p = go.get_position(v)
        p.y = p.y + speed * dt
        if p.y > 800  then
            p.y = math.random(200,800)/-1
            p.x = math.random(1,25) * math.random(10,35) * 2
            local speed = math.random(250,1000)
            go.set_position(p, v)
            else
            go.set_position(p, v)
        end 
    end
    end
Sign up to request clarification or add additional context in comments.

1 Comment

Tip: instead of storing strings in the pieces table you can store the id:s of the objects: pieces = { go.get_id("go1"), go.get_id("go2"), go.get_id("go3") }. For cases where you do a lot of iterations this saves lookup from string to id (hash) that the engine otherwise has to do each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.