Hey guys!
I'm having some difficulties to spawn an object where another hit... (get object.x then spawn an object in there)
The problem is... I want to make some fruits fall down the screen...
some parts of my code:
--ball ballx=64 bally=64 ballsize=3 ballxdir=5 ballydir=3 --fruit fspr=1 fx=0 fy=0 --top if bally<ballsize then ballydir=-ballydir fx=0 (RESET FX) fx=ballx (GET FX) end --create fruits function createfruit() fruit = spr(fspr,fx,fy) end function _draw() rectfill(0,0,128,128,color2) --draw the paddle rectfill(padx,pady,padx+padw,pady+padh,color1) --draw ball circfill(ballx,bally,ballsize,color1) --fruit spawn if fx>0 or fx==-1 then createfruit() end end |
but when I do this just one fruit fell down (fy-=3)
I need do something like foreach or Am I missing something?
Thanks!
the code
--create fruits function createfruit() fruit = spr(fspr,fx,fy) end |
should be
--create fruits function createfruit() local fruit = spr(fspr,fx,fy) return fruit end |
before you were assigning it to a global variable then just overwriting the same one everytime you called it
by making it local to the function it will create a seperate one each time and then you can just store them all in a fruit table like this.
--this function is auto-called once --at the start of the game function _init() fruit_table={} end --fruit spawn if fx>0 or fx==-1 then add(fruit_table,createfruit()) end |
My bad, it was late and my brain was wonky for some reason I didn't even think about pico's built in spr function i thought it was some function of yours that did have a return.
This code assumes you have drawn sprites in the first three sprite table slots:
function _init() fruit_table={} froot1 = create_fruit(1,10,10) froot2 = create_fruit(2,40,40) froot3 = create_fruit(3,25,50) --create_fruit(sprite,x,y) put your applicable fruit variables here. --each create_fruit you call, adds to the fruit_table --what it is adding exactly is another table --the table it adds contains {fspr,fx,fy} end function create_fruit(fspr,fx,fy) local fruit = {fspr=fspr,fx=fx,fy=fy} add(fruit_table,fruit) return fruit end function draw_fruit(fruit) spr(fruit.fspr,fruit.fx,fruit.fy) end function _draw() cls(0) foreach(fruit_table,draw_fruit) end |
If you wanted to move froot1 around you just change it's fx:
froot1.fx=75 |
If you have any questions about the code, let me know I'll try to help!
L O L Cabledragon I was adding the fruit_table() in the _update hahaah after you said that "tables" do the job I searched alot about it and found a solution studing everything that you said and... Worked! I did:
function _init() fruit_table={} end |
--create fruits function create_fruit() local fruit = { fspr=1, fx=ballx, fy=0 } add(fruit_table,fruit) end |
function _update() for fruit in all(fruit_table) do fruit.fy+=3 end |
function _draw() --fruit spawn for fruit in all(fruit_table) do spr(fruit.fspr,fruit.fx,fruit.fy) end end |
Thank you both very much! I've learned a lot with you about spawn,create;items,objects.
Thank you guys! I've finished my game.
[Please log in to post a comment]