[Resolved] Help with function

Hi all,

im trying to be clever and create a function that when called creates a game object. My function looks like this:

function createItem(x, y, itemtype)  
  
 if ( itemttype == star ) then  
 star = display.newImageRect( "images/star.png", 25, 25 )  
 star.myName = "star"  
 star.x, star.y = x,y  
 physics.addBody( star, "static", {isSensor = true} )  
 star:setReferencePoint(display.CenterLeftReferencePoint);  
 levelGroup:insert(3, star )  
 star.collision = onLocalCollision  
 star:addEventListener( "collision", star )  
 print (itemtype)  
 end  
  
end  

and from my main lua file i call it 3 times using

 --x, y, itemtype  
 createLevelItem.createItem(120, 150, "star")  
 createLevelItem.createItem(250, 150, "star")  
 createLevelItem.createItem(400, 250, "star")  

…however its only called once…any ideas as to why?

[import]uid: 162458 topic_id: 30049 reply_id: 330049[/import]

@samo8076

Re-type this line of your code to make “star” as string:

[lua]if ( itemttype == “star” ) then[/lua]

Also,

You call the function 3x in a roll at “same time” and so it would be an issue if am not wrong, so, try to give some “milliseconds” of time between each call of [lua]createLevelItem.createItem(120, 150, “star”)[/lua] (as eg the API.: [lua]timer.performWithDelay(delay, listener, iterations)[/lua]
Let me know how it goes for you.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30049 reply_id: 120325[/import]

thanks but no luck…iv change my lines to:

timer.performWithDelay(1, createLevelItem.createItem(120, 150, "star"), 1)  
 timer.performWithDelay(5000, createLevelItem.createItem(250, 150, "star"), 1)  
 timer.performWithDelay(10000, createLevelItem.createItem(400, 250, "star"), 1)  

also when i set the itemtype == “star” it doesnt fire at all [import]uid: 162458 topic_id: 30049 reply_id: 120327[/import]

wow awesome SegaBoy, thanks so much, did the trick. still getting the hand of corona (coming from vb background) [import]uid: 162458 topic_id: 30049 reply_id: 120329[/import]

No worries - happy to help… [import]uid: 33275 topic_id: 30049 reply_id: 120330[/import]

Declare star as a local variable within your constructor function. You’ve initialised it as a global, so subsequent calls are just reusing that one global variable.

You’ll also want to return star from the function and store the returned star inside a local variable.

Something like this:

function createItem(x, y, itemtype)  
  
 if ( itemttype == star ) then  
 local star = display.newImageRect( "images/star.png", 25, 25 )  
 star.myName = "star"  
 star.x, star.y = x,y  
 physics.addBody( star, "static", {isSensor = true} )  
 star:setReferencePoint(display.CenterLeftReferencePoint);  
 levelGroup:insert(3, star )  
 star.collision = onLocalCollision  
 star:addEventListener( "collision", star )  
 print (item type)  
  
 return star  
 end  
  
end  
  
local newStar = createLevelItem.createItem(120, 150, "star")  

Haven’t tested any of this code, but the gist of it should point you in the right direction.
[import]uid: 33275 topic_id: 30049 reply_id: 120328[/import]

I would recommend passing table variables to your function. It will give you more flexibly for future variables you may need.

[lua]function createItem(data)

if ( data.itemttype == “star” ) then
local star = display.newImageRect( “images/star.png”, 25, 25 )
star.myName = “star”
star.x, star.y = data.x,data.y
physics.addBody( star, “static”, {isSensor = true} )
star:setReferencePoint(display.CenterLeftReferencePoint);
levelGroup:insert(3, star )
star.collision = onLocalCollision
star:addEventListener( “collision”, star )
print (item type)

return star
end

end

local newStar = createLevelItem.createItem({ x =120, y= 150, itemttype =“star” }) [import]uid: 7177 topic_id: 30049 reply_id: 120370[/import]

@samo8076

Re-type this line of your code to make “star” as string:

[lua]if ( itemttype == “star” ) then[/lua]

Also,

You call the function 3x in a roll at “same time” and so it would be an issue if am not wrong, so, try to give some “milliseconds” of time between each call of [lua]createLevelItem.createItem(120, 150, “star”)[/lua] (as eg the API.: [lua]timer.performWithDelay(delay, listener, iterations)[/lua]
Let me know how it goes for you.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30049 reply_id: 120325[/import]

thanks but no luck…iv change my lines to:

timer.performWithDelay(1, createLevelItem.createItem(120, 150, "star"), 1)  
 timer.performWithDelay(5000, createLevelItem.createItem(250, 150, "star"), 1)  
 timer.performWithDelay(10000, createLevelItem.createItem(400, 250, "star"), 1)  

also when i set the itemtype == “star” it doesnt fire at all [import]uid: 162458 topic_id: 30049 reply_id: 120327[/import]

wow awesome SegaBoy, thanks so much, did the trick. still getting the hand of corona (coming from vb background) [import]uid: 162458 topic_id: 30049 reply_id: 120329[/import]

No worries - happy to help… [import]uid: 33275 topic_id: 30049 reply_id: 120330[/import]

Declare star as a local variable within your constructor function. You’ve initialised it as a global, so subsequent calls are just reusing that one global variable.

You’ll also want to return star from the function and store the returned star inside a local variable.

Something like this:

function createItem(x, y, itemtype)  
  
 if ( itemttype == star ) then  
 local star = display.newImageRect( "images/star.png", 25, 25 )  
 star.myName = "star"  
 star.x, star.y = x,y  
 physics.addBody( star, "static", {isSensor = true} )  
 star:setReferencePoint(display.CenterLeftReferencePoint);  
 levelGroup:insert(3, star )  
 star.collision = onLocalCollision  
 star:addEventListener( "collision", star )  
 print (item type)  
  
 return star  
 end  
  
end  
  
local newStar = createLevelItem.createItem(120, 150, "star")  

Haven’t tested any of this code, but the gist of it should point you in the right direction.
[import]uid: 33275 topic_id: 30049 reply_id: 120328[/import]

I would recommend passing table variables to your function. It will give you more flexibly for future variables you may need.

[lua]function createItem(data)

if ( data.itemttype == “star” ) then
local star = display.newImageRect( “images/star.png”, 25, 25 )
star.myName = “star”
star.x, star.y = data.x,data.y
physics.addBody( star, “static”, {isSensor = true} )
star:setReferencePoint(display.CenterLeftReferencePoint);
levelGroup:insert(3, star )
star.collision = onLocalCollision
star:addEventListener( “collision”, star )
print (item type)

return star
end

end

local newStar = createLevelItem.createItem({ x =120, y= 150, itemttype =“star” }) [import]uid: 7177 topic_id: 30049 reply_id: 120370[/import]