Noob storyboard question :)

I’m currently trying to get my head around using storyboard and I’m creating a basic scrolly shooter. I’ve got some very basic two layer parallax scrolling working, but what I’d like to do is have some random scenery that spawns every 5 to 10 seconds to make it seem a bit more random. Just as a starter I’ve got eight different tree images (tree1.png, tree2.png etc) that I’d like it to pick from at random.
Problem is, not knowing storyboard very well, I’m not quite sure how to work it.

This is my code so far for the scrolling…
[lua]-- requires

local physics = require “physics”
physics.start()

require “sprite”

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
local _w=display.contentWidth
local _h=display.contentHeight
local _x=display.contentCenterX
local _y=display.contentCenterY
local _m=math.random
local fgSpeed=2
local bgSpeed=1

print(" “)
print (“Entered Game scene”)
print(” ")
function scene:createScene(event)

local screenGroup = self.view
local background=display.newGroup()
screenGroup:insert(background)
local midground=display.newGroup()
screenGroup:insert(midground)
local foreground=display.newGroup()
screenGroup:insert(foreground)

local bg=display.newRect(background,0,0,_w,_h)
local bgGrad=graphics.newGradient(
{0,0,105}, – Start Color
{68,0,144}, – End Color
“down” – direction
)
bg:setFillColor(bgGrad)

bgfloor=display.newRect(0,_h-50,_w, 50)
bgfloor:setFillColor(0,0,0)
foreground:insert(bgfloor)

ground1=display.newImage(“ground1.png”)
ground1:setReferencePoint(display.BottomLeftReferencePoint)
ground1.x = 0
ground1.startX=1024
ground1.y = _h-50
ground1.speed = fgSpeed
ground1.recycle=1024
foreground:insert(ground1)

ground2=display.newImage(“ground2.png”)
ground2:setReferencePoint(display.BottomLeftReferencePoint)
ground2.x = 1024
ground2.startX=1024
ground2.y = _h-50
ground2.speed = fgSpeed
ground2.recycle=1024
foreground:insert(ground2)

ground3=display.newImage(“ground1.png”)
ground3:setReferencePoint(display.BottomLeftReferencePoint)
ground3.x = 0
ground3.alpha=.5
ground3.startX=1024
ground3.y = _h-75
ground3.speed = bgSpeed
ground3.recycle=1024
background:insert(ground3)

ground4=display.newImage(“ground2.png”)
ground4:setReferencePoint(display.BottomLeftReferencePoint)
ground4.x = 1024
ground4.alpha=.5
ground4.startX=1023
ground4.y = _h-75
ground4.speed = bgSpeed
ground4.recycle=1024
background:insert(ground4)
end

function scrollStuff(self,event)
if self.x < -self.recycle then
self.x = self.startX
else
self.x = self.x - self.speed
end
end

function createStuff(event)
– create stuff here
end

function scene:enterScene(event)

ground1.enterFrame = scrollStuff
Runtime:addEventListener(“enterFrame”, ground1)

ground2.enterFrame = scrollStuff
Runtime:addEventListener(“enterFrame”, ground2)

ground3.enterFrame = scrollStuff
Runtime:addEventListener(“enterFrame”, ground3)

ground4.enterFrame = scrollStuff
Runtime:addEventListener(“enterFrame”, ground4)
end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)

return scene[/lua]

Any help at all would be greatfuly received.
[import]uid: 7841 topic_id: 35200 reply_id: 335200[/import]

You could try…

Forward declaration:
[lua]local treeSpawnTimer

local function onUpdateSpawnTimer( event )

– ADD RANDOM TREE TO VIEW HERE

– get new random delay and start again
treeSpawnTimer = timer.performWithDelay( math.random(5000, 10000), onUpdateSpawnTimer )
end[/lua]

In enterScene:

[lua]-- init
treeSpawnTimer = timer.performWithDelay( math.random(5000, 10000), onUpdateSpawnTimer )[/lua]

In exitScene:

[lua]-- be sure to clean up the timer
timer.cancel( treeSpawnTimer )[/lua]

Hope that helps. [import]uid: 202223 topic_id: 35200 reply_id: 139946[/import]

@develephant - Awesome stuff! Cheers for the prompt reply :slight_smile:
[import]uid: 7841 topic_id: 35200 reply_id: 139955[/import]

You’re very welcome. Cheers. [import]uid: 202223 topic_id: 35200 reply_id: 139956[/import]

Right, so I’ve got that working, but just one more very quick question (and I know this isn’t storyboard related, sorry). I have a table storing my trees, and when they go past the end of the screen I want them removed. With the following code as part of the scroll function…
[lua]if self.id==“tree” then
if self.x self:removeEventListener(“enterFrame”, scrollStuff)
self:removeSelf()
else
self.x=self.x+self.speed
end
end[/lua]
Would this also remove the entry from the table? If not then how do I do that?

Cheers
Chris
[import]uid: 7841 topic_id: 35200 reply_id: 140064[/import]

Appletreeman your code is awesome.It worked for me too.

go here to get more details about it. [import]uid: 217520 topic_id: 35200 reply_id: 140173[/import]

Well, I would add a numerical id to each tree so you can do a loop against the table. Something like…

Forward declarations:
[lua]local treeIdNum = 1 --master id

local function clearFromTreeTable( treeIdNum )
for i=#treeTable, 1, -1 do --backwards
if ( treeTable[i].idNum == treeIdNum ) then
table.remove( treeTable, i )
break
end
end
end[/lua]

When you are creating a tree, attach an idNum:

[lua]-- tree has been created, so stripe it with an id number for later use
tree.idNum = treeIdNum
–increment the master id
treeIdNum = treeIdNum + 1 [/lua]
In your function:
[lua]if self.id==“tree” then
if self.x self:removeEventListener(“enterFrame”, scrollStuff)

clearFromTreeTable( self.idNum ) --ADD CALL TO CLEANING FUNCTION

self:removeSelf()
else
self.x=self.x+self.speed
end
end[/lua]

This code isn’t tested, so it may not work the first time. Best of luck. [import]uid: 202223 topic_id: 35200 reply_id: 140188[/import]

You could try…

Forward declaration:
[lua]local treeSpawnTimer

local function onUpdateSpawnTimer( event )

– ADD RANDOM TREE TO VIEW HERE

– get new random delay and start again
treeSpawnTimer = timer.performWithDelay( math.random(5000, 10000), onUpdateSpawnTimer )
end[/lua]

In enterScene:

[lua]-- init
treeSpawnTimer = timer.performWithDelay( math.random(5000, 10000), onUpdateSpawnTimer )[/lua]

In exitScene:

[lua]-- be sure to clean up the timer
timer.cancel( treeSpawnTimer )[/lua]

Hope that helps. [import]uid: 202223 topic_id: 35200 reply_id: 139946[/import]

@develephant - Awesome stuff! Cheers for the prompt reply :slight_smile:
[import]uid: 7841 topic_id: 35200 reply_id: 139955[/import]

You’re very welcome. Cheers. [import]uid: 202223 topic_id: 35200 reply_id: 139956[/import]

Right, so I’ve got that working, but just one more very quick question (and I know this isn’t storyboard related, sorry). I have a table storing my trees, and when they go past the end of the screen I want them removed. With the following code as part of the scroll function…
[lua]if self.id==“tree” then
if self.x self:removeEventListener(“enterFrame”, scrollStuff)
self:removeSelf()
else
self.x=self.x+self.speed
end
end[/lua]
Would this also remove the entry from the table? If not then how do I do that?

Cheers
Chris
[import]uid: 7841 topic_id: 35200 reply_id: 140064[/import]

Appletreeman your code is awesome.It worked for me too.

go here to get more details about it. [import]uid: 217520 topic_id: 35200 reply_id: 140173[/import]

Well, I would add a numerical id to each tree so you can do a loop against the table. Something like…

Forward declarations:
[lua]local treeIdNum = 1 --master id

local function clearFromTreeTable( treeIdNum )
for i=#treeTable, 1, -1 do --backwards
if ( treeTable[i].idNum == treeIdNum ) then
table.remove( treeTable, i )
break
end
end
end[/lua]

When you are creating a tree, attach an idNum:

[lua]-- tree has been created, so stripe it with an id number for later use
tree.idNum = treeIdNum
–increment the master id
treeIdNum = treeIdNum + 1 [/lua]
In your function:
[lua]if self.id==“tree” then
if self.x self:removeEventListener(“enterFrame”, scrollStuff)

clearFromTreeTable( self.idNum ) --ADD CALL TO CLEANING FUNCTION

self:removeSelf()
else
self.x=self.x+self.speed
end
end[/lua]

This code isn’t tested, so it may not work the first time. Best of luck. [import]uid: 202223 topic_id: 35200 reply_id: 140188[/import]