forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
September 10, 2010, 06:32:27 AM
.
News: 08/07/2009 - Welcome to the revived and revamped evilmana forums. Take a look around  for new features such as:New Theme, New Reputation System, and Arcade!
Coming Soon: An all new evilmana.com main site!
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read August 01, 2007, 08:17:07 AM #0
Zion

Simple Scrolling Function

This is a simple method i made for a scrolling function. It is used in my Halo Genocide Game

So lets start.


First of all we will load the images we will be using for our backgrounds. I use 1 480x272 image for this.

Code:
Background = Image.load("Background.png")

The next step is to set up our background variables. (note that the 3 background variables use the same image (saves memory)


Code:
Background_A = {}   ---the first scrolling background (positioned as the first image you see)
Background_A.x = 0
Background_A.y = 0
Background_A.Img = Background_Image

Background_B = {} --- the second scrolling background (positioned directly after Background_A)
Background_B.x = 480
Background_B.y = 0
Background_B.Img = Background_Image

Background_C = {} ---the third scrolling background (positioned directly before Background_A)
Background_C.x = - 480
Background_C.y = 0
Background_C.Img = Background_Image

Now thats done lets move onto the code that will make this scroll.

Code:
function Scroll()

if pad:right() then
Background_A.x = Background_A.x - 4
Background_B.x = Background_B.x - 4
Background_C.x = Background_A.x - 480
end

if pad:left() then
Background_A.x = Background_A.x + 4
Background_B.x = Background_B.x + 4
Background_C.x = Background_C.x + 4
end

if Background_A.x <= -480 then
Background_A.x = 0
end

if Background_B.x <= 0 then
Background_B.x = 480
end

if Background_C.x >= 0 then
Background_C.x = -480
Background_A.x = 0
Background_B.x = 480
end
end

The way this works, is if you press either left or right, the background starts to scroll in the opposite direction.

The code underneath that, basically resets the backgrounds to there original positions so that it should, in theory, scroll indefinitely.


Now we start our main loop


Code:
while true do
screen:clear() ---stops the background from getting garbled
pad = Controls.read()  --read pad input

Now to call our scrolling function

Code:
Scroll()  ---call scrolling code

Now to blit our backgrounds (note that we only blit them when needed to prevent slowdown)

Code:
if Background_A.x <=480 then
screen:blit(Background_A.x, Background_A.y, Background_A.Img)
end

if Background_B.x <=480 then
screen:blit(Background_B.x, Background_B.y, Background_B.Img)
end

if Background_C.x <=480 then
screen:blit(Background_C.x, Background_C.y, Background_C.Img)
end

Now to wrap it all up

Code:
screen.waitVblankStart()
screen.flip()
end

Hope that helps a few people get started with scrolling. You can test that with any png image. Just make the image 480x272 and name it "Background.png"


The whole code is below :

Code:
Background = Image.load("Background.png")


Background_A = {}   ---the first scrolling background (positioned as the first image you see)
Background_A.x = 0
Background_A.y = 0
Background_A.Img = Background_Image

Background_B = {} --- the second scrolling background (positioned directly after Background_A)
Background_B.x = 480
Background_B.y = 0
Background_B.Img = Background_Image

Background_C = {} ---the third scrolling background (positioned directly before Background_A)
Background_C.x = - 480
Background_C.y = 0
Background_C.Img = Background_Image

function Scroll()

if pad:right() then
Background_A.x = Background_A.x - 4
Background_B.x = Background_B.x - 4
Background_C.x = Background_A.x - 480
end

if pad:left() then
Background_A.x = Background_A.x + 4
Background_B.x = Background_B.x + 4
Background_C.x = Background_C.x + 4
end

if Background_A.x <= -480 then
Background_A.x = 0
end

if Background_B.x <= 0 then
Background_B.x = 480
end

if Background_C.x >= 0 then
Background_C.x = -480
Background_A.x = 0
Background_B.x = 480
end
end


while true do
screen:clear() ---stops the background from getting garbled
pad = Controls.read()  --read pad input


Scroll() ---call scrolling code

if Background_A.x <=480 then
screen:blit(Background_A.x, Background_A.y, Background_A.Img)
end

if Background_B.x <=480 then
screen:blit(Background_B.x, Background_B.y, Background_B.Img)
end

if Background_C.x <=480 then
screen:blit(Background_C.x, Background_C.y, Background_C.Img)
end

screen.waitVblankStart()
screen.flip()
end


« Last Edit: August 01, 2007, 08:18:41 AM by Zion »
 
Read December 02, 2007, 11:22:41 AM #1
joshparrisjosh

Re: Simple Scrolling Function

I really interested in learn how this works... but I couldn't get the code to execute.

I got this "bad arguement #2 to blit" error.
Offline  
Read May 05, 2008, 08:31:10 AM #2
zref21

Re: Simple Scrolling Function

hmm, i think i've got some good use for this ôÔ

oh, and plz post working code next time ^^

Error:
Code:
Background = Image.load("Background.png")

should be
Code:
Background_Image = Image.load("Background.png")
no way to access a variable that doesnt exist .òÓ?
Offline  
Read February 27, 2009, 12:47:14 AM #3
peter_g760

Re: Simple Scrolling Function

thanks for the scrolling function

i needed that, you will be credited in my game because im using its

is there any way for diagnal and up-and-down scrolling using analog stick ???

thanks
Offline  
Read February 27, 2009, 01:22:32 AM #4
PL3X

Re: Simple Scrolling Function

There is if you program it.
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re