I should probably ask if anyone knows of a lua-specific forum where I could be asking these questions…!
I am trying to write a code that does parralax scrolling. All the sprites in the table need to shoot up into the air, then have their position reset to y=200 and x=randomnumber. Later on, I’m gonna 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(current)
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(p)
end
end
If i print “v” inside the ipairs i get the ID of the object, so I thought that what I have above would work. It doesn’t. Any help?
ERROR:SCRIPT: main/starscript.script:6: Instance sprite not found
stack traceback:
[C]: in function 'get_position’
main/starscript.script:6: in function <main/starscript.script:4>
though print(v) correctly prints the ID of the sprite. I have checked the ID of the sprites and they are all correct. The script and the sprites are both part of the same game object.
They are sprites. They have an ID, but they’re not game objects, right?
print(go.get_id(v))
returns
hash: [/sprite6]
but i still get an error “ERROR:SCRIPT: main/starscript.script:6: Instance (null) not found” when running go.get_position
edit: just checked, it doesn’t matter if the sprites are game objects are just sprites
By the way… I really am looking for a place to read up on lua. The official manual and basically all other resources are really complicated (at least for someone with a none coding background)
local speed = math.random(250,1000)
pieces = { "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
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) *1
go.set_position(p, v)
else
go.set_position(p, v)
end
end
end
the only problem is that the random numbers come out the same for each iteration. I am investigating.
You need to call math.randomseed(os.time()) sometime early in your code to seed the pseudo-random number generator with a value that will make it behave differently on each run.
Also, what is p in the example above? It looks like a global variable. You should try to avoid those unless you know what you’re doing (prefix it with the local keyword).
Finally the local speed = math.random(250,1000) *1 will do nothing. Are you supposed to change the speed declared at the top of the snippet of code you pasted? If that is the case you need to remove the local keyword in front of where you are redeclaring it.
Thanks britzl. That code is a bit of a mess, I was trying lots of different things without properly clearing up inbetween each one. In the end I gave up and just made a script and then copied it to each object.