Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Vertical scrolling with a hybrid 4x canvas

2,295 views
Skip to first unread message

Scott Ferguson

unread,
Mar 10, 2016, 11:04:53 PM3/10/16
to app-inventor-de...@googlegroups.com
See: video

Combine canvas scrolling with a single moving image sprite to make a vertical scroller with three z levels.

This project has a helicopter rising through a canyon.

Screen1.VerticalAlignment is set to 'center' to allow vertical scrolling to work.
Screen1.BackgroundImage contains the clouds image Background.png.
A vertical arrangment container CanvasContainer is set to 5x the Screen1.Height.
It's BackgroundColor is set to none to allow the Screen1.BackgroundImage clouds to show through.
Inside CamvasContainer are another vertical arrangement CanvasSpacer followed by a canvas GameCanvas.
CanvasSpacer.BackgroundColor is set to none to allow Screen1.BackgroundImage to show through.
CanvasSpacer.Height can be set from 0 to Screen1.Height to move GameCanvas below it up or down.
Below CanvasSpacer, canvas GameCanvas is set to 4x the Screen1.Height.
It' BackgroundImage and BackgroundColor are set to none to allow Screen1.BackgroundImage to show through.
Midground ImageSprite contains the midground image of the canyon walls, is 2x Screen1.Height and is fixed at Screen1.Height offset from the top of the canvas.
Foreground ImageSprite is 3x Screen1.Height and scrolls at the same ScrollingSpeed as GameCanvas.

CanvasSpacer.Height is initially set to 0 which positions the canvas at the top of CanvsContainer and displayes the area starting at 2x Screen1.Height from the top of the canvas on the screen.
CanvasSpacer.Height is gradually increased to move the canvas down until it has reached the bottom of CanvasContainer.
At that point it is moved back to the top of CanvasContainer by setting CanvasSpacer.Height back to 0 and the process repeats.
This is done inside the Scroll.Timer event block by using the 'modulo of' math operator block.
At the same time, Foreground moves down GameCanvas at ScrollingSpeed as well, which to the user looks like it is moving down twice as fast as Midground.
When Foreground has reached Screen1.Height offset from the top of GameCanvas it is reset back to the top.

Helicopter.Y must constantly be adjusted as the canvas moves down the screen so that it hovers in the same position relative to the screen viewport.

This is accomplished in the FlyHelicopter procedure by subtracting the current CanvasSpacer.Height from the helicopter's initial Y position (2.5x Screen1.Height.)
---

blocks.PNG
Foreground.png
VerticalScrollingHybrid4xCanvas.aia
Midground.png
Screenshot_2016-03-10-21-47-30.png

Gale Fulton

unread,
Mar 31, 2017, 2:35:49 PM3/31/17
to App Inventor Developers Library
Great tutorial.  I have been wanting to create a game that would use vertical scrolling.  I was messing around with yours to get a better understanding of how it all works and realized...I can't add buttons to control the helicopter sprite because the canvass constantly resizes.  Any ideas how this might be accomplished?

Thank you again...I would not have though of this without this tutorial!

Michael Ferguson

unread,
Apr 5, 2017, 2:19:38 AM4/5/17
to app-inventor-de...@googlegroups.com
See this example:
It keeps a sprite in one position on the screen as the canvas scrolls. That sprite could be a button.
---
msf
Reply all
Reply to author
Forward
0 new messages