I'll give a larger example with explanation
If you want to scroll the screen and let the character stay in the center, there is no
need to move the character.
So you can replace
move sprite Hero, HeroSpeed
with
paste sprite Hero, screen width()/2-sprite width(Hero)/2, screen height()/2-sprite
height(Hero)/2
To make sure the character is always in the center of the screen.
Now, if we want to let the character move to the right, we scroll the screen to the left
instead. Keeping the character in the center but still giving the impression it is
moving.
So we need to change
if upkey()=1
rotate sprite Hero, 0
move sprite Hero, HeroSpeed
rotate sprite Hero, 0
Heromove = 1
endif
if downkey()=1
rotate sprite Hero, 180
move sprite Hero, HeroSpeed
rotate sprite Hero, 0
Heromove = 2
endif
if leftkey()=1
rotate sprite Hero, 270
move sprite Hero, HeroSpeed
rotate sprite Hero, 0
Heromove = 3
endif
if rightkey()=1
rotate sprite Hero, 90
move sprite Hero, HeroSpeed
rotate sprite Hero, 0
Heromove = 4
endif
with something like
// We only need to do this once per loop
paste sprite Hero, screen width()/2 - sprite width(Hero)/2, screen height()/2 - sprite
height(Hero)/2
if upkey()=1
rotate sprite Hero, 0
// Increase the y position virtual camera
Inc CamY, 1
rotate sprite Hero, 0
Heromove = 1
endif
if downkey()=1
rotate sprite Hero, 180
// Decrease the y position virtual camera
Dec CamY, 1
rotate sprite Hero, 0
Heromove = 2
endif
if leftkey()=1
rotate sprite Hero, 270
// Increase the x position virtual camera
Inc CamX, 1
rotate sprite Hero, 0
Heromove = 3
endif
if rightkey()=1
rotate sprite Hero, 90
// Decrease the x position virtual camera
Dec CamX, 1
rotate sprite Hero, 0
Heromove = 4
endif
In this code the CamX and CamY are the x and y position of the camera.
There isn't a real camera, but we pretend there is, it will get clearer later on.
Now when we press the keys, nothing will happen yet.
So now we need to scroll the screen.
We do this by decreasing the x position of everything in the map, except the character,
with the x position of the camera, same for the y position.
This code should be in the place where you draw your map
// MapSprites
For i=0 to MapSpriteCount
paste sprite MapSprite(i).id, MapSprite(i).x - CamX, MapSprite(i).y - CamY
Next i
Using this code the larger the camera x position is, meaning the camera moved to the
right, the lower the x position of the map sprites is. This will result in the sprites
being drawed more to the left.
So when you press the rightkey, the x position of the camera will increase, resulting in
the sprites scrolling to the left.
The MapSprite() in the code above should be an array holding the id, x position and y
position of all the sprites in your map.
edit
To get a better performance you can change the drawing code to something like this:
For i=0 to MapSpriteCount
newX = MapSprite(i).x - Cam
newY = MapSprite(i).y - CamY
id = MapSprite(i).id
// this will check if the sprite is in the screen, so we only need to draw it if it is visible.
if newX>-sprite width(id) and newX<screen width() and newY>-sprite height(id) and newY<screen height()
paste sprite id, newX, newY
endif
Next i