Help with writing a function to add bodies to the scrolling background.

Hi there!

I’m playing with the scrolling background in the JungleScene example. I’d like to find a way to do a few things before I star my main project as a whole.

Currently, the jungle scene adds the array of bodies to the background like this:

[lua]-- a bunch of foliage
local tree = {}

tree[1] = display.newImage( “img/Palm-arecaceae.png” )
tree[1].xScale = 0.7; tree[1].yScale = 0.7
tree[1]:setReferencePoint( display.BottomCenterReferencePoint )
tree[1].x = 20; tree[1].y = baseline
tree[1].dx = 0.1
tree[2] = display.newImage( “img/Greenhouse-Palm-jubaea01.png” )
tree[2].xScale = 0.6; tree[2].yScale = 0.6
tree[2]:setReferencePoint( display.BottomCenterReferencePoint )
tree[2].x = 120; tree[2].y = baseline
tree[2].dx = 0.2
tree[3] = display.newImage( “img/Greenhouse-Palm-cycas01.png” )
tree[3].xScale = 0.8; tree[3].yScale = 0.8
tree[3]:setReferencePoint( display.BottomCenterReferencePoint )
tree[3].x = 200; tree[3].y = baseline
tree[3].dx = 0.3
tree[4] = display.newImage( “img/Ginger.png” )
tree[4].xScale = 0.7; tree[4].yScale = 0.7
tree[4]:setReferencePoint( display.BottomCenterReferencePoint )
tree[4].x = baseline; tree[4].y = baseline
tree[4].dx = 0.4
tree[5] = display.newImage( “img/Greenhouse-Palm-acai01.png” )
tree[5].xScale = 0.8; tree[5].yScale = 0.8
tree[5]:setReferencePoint( display.BottomCenterReferencePoint )
tree[5].x = 300; tree[5].y = baseline
tree[5].dx = 0.5
tree[6] = display.newImage( “img/Dracena.png” )
tree[6].xScale = 0.4; tree[5].yScale = 0.4
tree[6]:setReferencePoint( display.BottomCenterReferencePoint )
tree[6].x = 320; tree[6].y = baseline
tree[6].dx = 0.6
tree[7] = display.newImage( “img/Banana.png” )
tree[7].xScale = 0.4; tree[7].yScale = 0.4
tree[7]:setReferencePoint( display.BottomCenterReferencePoint )
tree[7].x = 380; tree[7].y = baseline
tree[7].dx = 0.7
tree[8] = display.newImage( “img/Bamboo-rgba.png” )
tree[8].xScale = 0.8; tree[8].yScale = 0.8
tree[8]:setReferencePoint( display.BottomCenterReferencePoint )
tree[8].x = 420; tree[8].y = baseline
tree[8].dx = 0.8[/lua]

I’d like to change this around in a few ways.

First, I’m trying to change it so that it works like this:

[lua]local function add_background_element(img, xScale, yScale, x, y, dX)
local obj = display.newImage( img )
obj.xScale = xScale;
obj.yScale = yScale;
obj:setReferencePoint( display.BottomCenterReferencePoint )
obj.x = x;
obj.y = y;
obj.dx = dx
return obj
end
tree[1] = add_background_element(“img/Palm-arecaceae.png”, 0.7, 0.7, 20, baseline, 0.1)
tree[2] = add_background_element(“img/Greenhouse-Palm-jubaea01.png”, 0.6, 0.6, 120, baseline, 0.2)
tree[3] = add_background_element(“img/Greenhouse-Palm-cycas01.png”, 0.8, 0.8, 200, baseline, 0.3)
tree[4] = add_background_element(“img/Ginger.png”, 0.7, 0.7, 280, baseline, 0.4)
tree[5] = add_background_element(“img/Greenhouse-Palm-acai01.png”, 0.8, 0.8, 300, baseline, 0.5)
tree[6] = add_background_element(“img/Dracena.png”, 0.4, 0.4, 320, baseline, 0.6)
tree[7] = add_background_element(“img/Banana.png”, 0.4, 0.4, 380, baseline, 0.7)
tree[8] = add_background_element(“img/Bamboo-rgba.png”, 0.8, 0.8, 420, baseline, 0.8)[/lua]

Second, I’d like it to be in it’s own file “background.lua” maybe with a couple of methods
load_background
move_background (optional:speed)

I’m sure there’s other useful stuff I could pack up - like for example the ground… but for now, there’s a number of nuances missing from my understanding of the language. Learning a new language is a bit funny - it’s like trying to build something with oven mitts on. You know what you *want* to do - but - you just don’t have the right control.

Thanks for your patience!

  • A
    [import]uid: 14327 topic_id: 5347 reply_id: 305347[/import]

So there were two problems

[lua]local function add_background_element(img, xScale, yScale, x, y, dX)
local obj = display.newImage( img )
obj.xScale = xScale;
obj.yScale = yScale;
obj:setReferencePoint( display.BottomCenterReferencePoint )
obj.x = x;
obj.y = y;
obj.dx = dX – HERE
return obj
end
local tree = {} – HERE
tree[1] = add_background_element(“img/Palm-arecaceae.png”, 0.7, 0.7, 20, baseline, 0.1)
tree[2] = add_background_element(“img/Greenhouse-Palm-jubaea01.png”, 0.6, 0.6, 120, baseline, 0.2)
tree[3] = add_background_element(“img/Greenhouse-Palm-cycas01.png”, 0.8, 0.8, 200, baseline, 0.3)
tree[4] = add_background_element(“img/Ginger.png”, 0.7, 0.7, 280, baseline, 0.4)
tree[5] = add_background_element(“img/Greenhouse-Palm-acai01.png”, 0.8, 0.8, 300, baseline, 0.5)
tree[6] = add_background_element(“img/Dracena.png”, 0.4, 0.4, 320, baseline, 0.6)
tree[7] = add_background_element(“img/Banana.png”, 0.4, 0.4, 380, baseline, 0.7)
tree[8] = add_background_element(“img/Bamboo-rgba.png”, 0.8, 0.8, 420, baseline, 0.8)[/lua] [import]uid: 14327 topic_id: 5347 reply_id: 17950[/import]