Scrollview Code

I am using the scrollview code that’s in the sample code area. I have it set up and it’s working great. One change I wanted to make but not sure where to make it in the file is the size of the scrollview screen. Right now it fills up the entire screen and you can’t even see my background page. Plus I have a backbutton that’s on this page and I need to adjust the size of the screen so you can see that button. So to make it even I just want to make the overall size of the scroll box smaller. Is anybody familiar with that file and can help me?

I want it in a small box kind of like the tilt monster help screen.

This is the text from the main file in the code.

[lua]module(…, package.seeall)

–>main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


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

–import the scrolling classes
local scrollView = require(“scrollView”)
local util = require(“util”)

–>Background
local background = display.newImage (“blackbackground.png”)
localGroup:insert(background)
–> This sets the background

– Setup a scrollable content group
local topBoundary = display.screenOriginY
local bottomBoundary = display.screenOriginY
local scrollView = scrollView.new{ top=topBoundary, bottom=bottomBoundary }

local myText = display.newText(“Move Up to Scroll”, 0, 0, native.systemFontBold, 16)
myText:setTextColor(0, 0, 0)
myText.x = math.floor(display.contentWidth*0.5)
myText.y = 48
scrollView:insert(myText)

– add some text to the scrolling screen
local lotsOfText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur imperdiet consectetur euismod. Phasellus non ipsum vel eros vestibulum consequat. Integer convallis quam id urna tristique eu viverra risus eleifend.\n\nAenean suscipit placerat venenatis. Pellentesque faucibus venenatis eleifend. Nam lorem felis, rhoncus vel rutrum quis, tincidunt in sapien. Proin eu elit tortor. Nam ut mauris pellentesque justo vulputate convallis eu vitae metus. Praesent mauris eros, hendrerit ac convallis vel, cursus quis sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque fermentum, dui in vehicula dapibus, lorem nisi placerat turpis, quis gravida elit lectus eget nibh. Mauris molestie auctor facilisis.\n\nCurabitur lorem mi, molestie eget tincidunt quis, blandit a libero. Cras a lorem sed purus gravida rhoncus. Cras vel risus dolor, at accumsan nisi. Morbi sit amet sem purus, ut tempor mauris.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur imperdiet consectetur euismod. Phasellus non ipsum vel eros vestibulum consequat. Integer convallis quam id urna tristique eu viverra risus eleifend.\n\nAenean suscipit placerat venenatis. Pellentesque faucibus venenatis eleifend. Nam lorem felis, rhoncus vel rutrum quis, tincidunt in sapien. Proin eu elit tortor. Nam ut mauris pellentesque justo vulputate convallis eu vitae metus. Praesent mauris eros, hendrerit ac convallis vel, cursus quis sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque fermentum, dui in vehicula dapibus, lorem nisi placerat turpis, quis gravida elit lectus eget nibh. Mauris molestie auctor facilisis.\n\nCurabitur lorem mi, molestie eget tincidunt quis, blandit a libero. Cras a lorem sed purus gravida rhoncus. Cras vel risus dolor, at accumsan nisi. Morbi sit amet sem purus, ut tempor mauris. "

local lotsOfTextObject = util.wrappedText( lotsOfText, 39, 14, native.systemFont, {0,0,0} )
scrollView:insert(lotsOfTextObject)
lotsOfTextObject.x = 24
lotsOfTextObject.y = math.floor(myText.y + myText.height)

– Important! Add a background to the scroll view for a proper hit area
local scrollBackground = display.newRect(0, 0, display.contentWidth, scrollView.height+64)
scrollBackground:setFillColor(255, 255, 255)
scrollView:insert(1, scrollBackground)

scrollView:addScrollBar()
local backbutton = display.newImage (“backbutton.png”)
localGroup:insert(backbutton)
backbutton.x = 75
backbutton.y = 300
backbutton.xScale = .5
backbutton.yScale = .5

local function touchedBackbutton (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touchedBackbutton)

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first

return localGroup

end[/lua]

this is the scrollview.lua file

[lua]-- scrollView.lua

– Version 1.0

– Copyright © 2010 ANSCA Inc. All Rights Reserved.

– Permission is hereby granted, free of charge, to any person obtaining a copy of
– this software and associated documentation files (the “Software”), to deal in the
– Software without restriction, including without limitation the rights to use, copy,
– modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
– and to permit persons to whom the Software is furnished to do so, subject to the
– following conditions:

– The above copyright notice and this permission notice shall be included in all copies
– or substantial portions of the Software.

– THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
– INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
– PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
– FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
– OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
– DEALINGS IN THE SOFTWARE.

module(…, package.seeall)

– set some global values for width and height of the screen
local screenW, screenH = display.contentWidth, display.contentHeight
local viewableScreenW, viewableScreenH = display.viewableContentWidth, display.viewableContentHeight
local screenOffsetW, screenOffsetH = display.contentWidth - display.viewableContentWidth, display.contentHeight - display.viewableContentHeight

local prevTime = 0

function new(params)
– setup a group to be the scrolling screen
local scrollView = display.newGroup()

scrollView.top = params.top or 0
scrollView.bottom = params.bottom or 0

function scrollView:touch(event)
local phase = event.phase
print(phase)

if( phase == “began” ) then
print(scrollView.y)
self.startPos = event.y
self.prevPos = event.y
self.delta, self.velocity = 0, 0
if self.tween then transition.cancel(self.tween) end

Runtime:removeEventListener(“enterFrame”, scrollView )

self.prevTime = 0
self.prevY = 0

transition.to(self.scrollBar, { time=200, alpha=1 } )

– Start tracking velocity
Runtime:addEventListener(“enterFrame”, trackVelocity)

– Subsequent touch events will target button even if they are outside the stageBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true

elseif( self.isFocus ) then

if( phase == “moved” ) then
local bottomLimit = screenH - self.height - self.bottom

self.delta = event.y - self.prevPos
self.prevPos = event.y
if ( self.y > self.top or self.y < bottomLimit ) then
self.y = self.y + self.delta/2
else
self.y = self.y + self.delta
end

scrollView:moveScrollBar()

elseif( phase == “ended” or phase == “cancelled” ) then
local dragDistance = event.y - self.startPos
self.lastTime = event.time

Runtime:addEventListener(“enterFrame”, scrollView )
Runtime:removeEventListener(“enterFrame”, trackVelocity)

– Allow touch events to be sent normally to the objects they “hit”
display.getCurrentStage():setFocus( nil )
self.isFocus = false
end
end

return true
end

function scrollView:enterFrame(event)
local friction = 0.9
local timePassed = event.time - self.lastTime
self.lastTime = self.lastTime + timePassed

–turn off scrolling if velocity is near zero
if math.abs(self.velocity) < .01 then
self.velocity = 0
Runtime:removeEventListener(“enterFrame”, scrollView )
transition.to(self.scrollBar, { time=400, alpha=0 } )
end

self.velocity = self.velocity*friction

self.y = math.floor(self.y + self.velocity*timePassed)

local upperLimit = self.top
local bottomLimit = screenH - self.height - self.bottom

if ( self.y > upperLimit ) then
self.velocity = 0
Runtime:removeEventListener(“enterFrame”, scrollView )
self.tween = transition.to(self, { time=400, y=upperLimit, transition=easing.outQuad})
transition.to(self.scrollBar, { time=400, alpha=0 } )
elseif ( self.y < bottomLimit and bottomLimit < 0 ) then
self.velocity = 0
Runtime:removeEventListener(“enterFrame”, scrollView )
self.tween = transition.to(self, { time=400, y=bottomLimit, transition=easing.outQuad})
transition.to(self.scrollBar, { time=400, alpha=0 } )
elseif ( self.y < bottomLimit ) then
self.velocity = 0
Runtime:removeEventListener(“enterFrame”, scrollView )
self.tween = transition.to(self, { time=400, y=upperLimit, transition=easing.outQuad})
transition.to(self.scrollBar, { time=400, alpha=0 } )
end

scrollView:moveScrollBar()

return true
end

function scrollView:moveScrollBar()
if self.scrollBar then
local scrollBar = self.scrollBar

scrollBar.y = -self.y*self.yRatio + scrollBar.height*0.5 + self.top

if scrollBar.y < 5 + self.top + scrollBar.height*0.5 then
scrollBar.y = 5 + self.top + scrollBar.height*0.5
end
if scrollBar.y > screenH - self.bottom - 5 - scrollBar.height*0.5 then
scrollBar.y = screenH - self.bottom - 5 - scrollBar.height*0.5
end

end
end

function trackVelocity(event)
local timePassed = event.time - scrollView.prevTime
scrollView.prevTime = scrollView.prevTime + timePassed

if scrollView.prevY then
scrollView.velocity = (scrollView.y - scrollView.prevY)/timePassed
end
scrollView.prevY = scrollView.y
end

scrollView.y = scrollView.top

– setup the touch listener
scrollView:addEventListener( “touch”, scrollView )

function scrollView:addScrollBar(r,g,b,a)
if self.scrollBar then self.scrollBar:removeSelf() end

local scrollColorR = r or 0
local scrollColorG = g or 0
local scrollColorB = b or 0
local scrollColorA = a or 120

local viewPortH = screenH - self.top - self.bottom
local scrollH = viewPortH*self.height/(self.height*2 - viewPortH)
local scrollBar = display.newRoundedRect(viewableScreenW-8,0,5,scrollH,2)
scrollBar:setFillColor(scrollColorR, scrollColorG, scrollColorB, scrollColorA)

local yRatio = scrollH/self.height
self.yRatio = yRatio

scrollBar.y = scrollBar.height*0.5 + self.top

self.scrollBar = scrollBar

transition.to(scrollBar, { time=400, alpha=0 } )
end

function scrollView:removeScrollBar()
if self.scrollBar then
self.scrollBar:removeSelf()
self.scrollBar = nil
end
end

function scrollView:cleanUp()
Runtime:removeEventListener(“enterFrame”, trackVelocity)
Runtime:removeEventListener( “touch”, scrollView )
Runtime:removeEventListener(“enterFrame”, scrollView )
scrollView:removeScrollBar()
end

return scrollView
end[/lua] [import]uid: 72372 topic_id: 12834 reply_id: 312834[/import]

No Solutions?!

Regards, [import]uid: 89165 topic_id: 12834 reply_id: 58680[/import]

I believe the solution (if I recall correctly) was to examine the code in Tilt Monster and take it from there and modify it to suit the project :slight_smile:

There was another thread by the same user regarding this.

Peach :slight_smile: [import]uid: 52491 topic_id: 12834 reply_id: 58836[/import]

Hey Peach,

Thanks for the orientation!

BTW would be a good habit to post just one thread of the same question as it would help everybody including myself as in this case here that I asked about solutions because it would serve for me as well.

But that`s okay.

See yah,
Rodrigo. [import]uid: 89165 topic_id: 12834 reply_id: 58993[/import]

It would be a good habit, yes - it actually states in the rules to do so, but sometimes users mistakenly create more than one, unfortunately.

Peach :slight_smile: [import]uid: 52491 topic_id: 12834 reply_id: 59044[/import]