Log In  

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!

P#51061 2018-03-30 13:13 ( Edited 2018-04-01 21:45)

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
P#51069 2018-03-30 16:38 ( Edited 2018-03-30 20:38)

Didn't work Cabledragon

P#51118 2018-03-31 19:08 ( Edited 2018-03-31 23:08)

The spr() function doesn't create sprites, it draws them. It doesn't return anything.

When you call spr(#,x,y) you're literally drawing that sprite onto the screen at that x,y this frame.

P#51124 2018-03-31 20:20 ( Edited 2018-04-01 00:20)

ok what I want to do is make every fruit independent... How can I achieve this in Pico's lua?

P#51136 2018-04-01 04:27 ( Edited 2018-04-01 08:27)

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!

P#51137 2018-04-01 04:43 ( Edited 2018-04-01 08:45)

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.

P#51144 2018-04-01 13:39 ( Edited 2018-04-01 18:24)

Thank you guys! I've finished my game.

https://www.lexaloffle.com/bbs/?tid=31064

P#51148 2018-04-01 14:41 ( Edited 2018-04-01 18:41)

Awesome! Really glad I could help!

P#51154 2018-04-01 17:45 ( Edited 2018-04-01 21:45)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 02:07:00 | 0.008s | Q:16