I thought I'd just write a quick tut on how I do scrolling lists, these are useful for level selector screens and that sort of thing.
I've just written it out as well documented code, check it out (fully working with no extra files)
SetDisplayAspect( 4.0/3.0 )
global aspect as float
aspect = 4.0/3.0 `Screen Aspect ratio
remstart
Scrolling lists, by MissJoJo, 14/09/13
total tiles in example, 100
remend
//***********************GLOBALS, DIMs and TYPES********************************
type option //a type for each tile
ox as float `Offset x for this particular tile
oy as float `Offset y
id `SpriteID
endtype
dim op[100] as option //Set this >= to the max number of tiles
//*******************************************************************************
//Create a black background sprite
b=CreateSprite(0)
SetSpriteColor(b,0,0,0,255)
SetspriteSize(b,100,100)
//l is a 'counter', it's fairly self explanatory in the code itself
l=0
//The speed that the tiles are scrolling at
spd as float
spd=0
//The X position of the first tile (the rest of the tiles are offseted from this position)
pos as float
pos=0
//NEW x (position of pointer), TEMP x (position of pointer last frame)
nx as float
tx as float
Rowl=100/5 //100 is total number of objects, 5 is the height (5 tiles vertically)
//rowl is 'Row Length', the number of tiles horizontally
rowl=ceil(rowl)
//ceil will round a float UP (not needed here)
//the reason it's done this way, is if you have 103 tiles, it'll have 21 columns etc
//CREATING THE TILES, 1 BY 1
for k=1 to rowl //Row Length (20 in this case)
for i=1 to 5 //Column length, 5 in this case
inc l,1 //Counter for tile x
if l<=100 //if you had 103 objects, you'd have 21 columns, which would come to 105 objects, but you'd only want 103
//So you'd set this to 103
op[l].id=CreateSprite(0) //Create a blank sprite
a#=k-1 //Need a float value for CURRENT COLUMN-1, integer won't do
b#=i-1 //Need a float value for CURRENT ROW-1
//The offset for this particular tile is defined as 'ROW position' * (tilewidth+2)
//This will create a space of 2px between each tile
op[l].ox=a#*12
//again, width(*aspect ratio)+2 for this tiles Y offset
op[l].oy=b#*((10*aspect)+2)
//Set the tiles width and height
SetSpriteSize(op[l].id,10,10*aspect)
//Set the tiles position!!!
//Y offset includes 5+[], the 5 is the offset of ALL the tiles from the top
//otherwise it;s simply POS+OFFSET
SetSpritePosition(op[l].id,pos+op[l].ox,5+op[l].oy)
endif
next
next
do
if GetPointerPressed()
//initialise TMPx,NEWx and STARTx
tx=GetPointerX()
nx=GetPointerX()
sx=GetPointerX()
endif
if GetPointerState()=1
tx=nx //TMPx = NEWx (therefore TMPx is the X position of the pointer from LAST frame)
nx=GetPointerX() //NEWx is the currect pointer X position
//If the pointer wasn't released this frame, then set the speed of the tiles to the difference between
//the pointerX positions from this frame and last
if GetPointerReleased()=0 then spd=nx-tx
endif
//SLOW the SPEED down
if spd<-0.1 then spd=spd+0.1
if spd>0.1 then spd=spd-0.1
//position = position + speed
pos=pos+spd
//if the speed's going slow enough, the STOP IT!
if spd<0.1 and spd>-0.1 then spd=0
//if the position of the first tile goes past the right-hand end of the screen, the stop it!
if pos>90
spd=-2
pos=90
endif
//if the position of the LAST tile goes past the right-hand end of the screen, the stop it!
//ROW Length-1 * (TILEwidth(10)+1)
if pos<-(rowl-1)*12
pos=-(rowl-1)*12
spd=2
endif
for k=1 to 100 //FOR k = 1 to MAX NUMBER OF TILES
if GetSpriteExists(op[k].id) //if the tile exists.....
//Set it's position via the POSvar and it's offset
SetSpritePosition(op[k].id,pos+op[k].ox,5+op[k].oy)
//
//SetSpriteColorAlpha(op[k].id,((50-diff(GetSpriteX(op[k].id)+5,50))+50)*2.55)
endif
next
sync()
loop
I've removed the method for clicking on a particular tile, try thinking of a way yourself!
The method is very versatile, here's a cool example of what I mean!
using a sinwave to adjust the sprite size!
SetDisplayAspect( 4.0/3.0 )
global aspect as float
aspect = 4.0/3.0 `Screen Aspect ratio
remstart
Scrolling lists, by MissJoJo, 14/09/13
info:
total tiles, 100
remend
//***********************GLOBALS, DIMs and TYPES********************************
type option //a type for each tile
ox as float `Offset x for this particular tile
oy as float `Offset y
id `SpriteID
endtype
dim op[100] as option //Set this to >= the max number of tiles
//*******************************************************************************
//Create a black background sprite, this is because huawei devices don't clear the screen properly, and as one of my test
//devices is a huawei, this is necassary
b=CreateSprite(0)
SetSpriteColor(b,0,0,0,255)
SetspriteSize(b,100,100)
//l is a 'counter', stands
l=0
//The speed that the tiles are scrolling at
spd as float
spd=0
//The X position of the first tile (the rest of the tiles are offseted from this position)
pos as float
pos=0
//NEW x (position of pointer), TEMP x (position of pointer last frame)
nx as float
tx as float
//100 isn't a float, so when
Rowl=100/5 //100 is total number of objects, 5 is the height (5 tiles vertically)
//rowl is 'Row Length', the number of tiles horizontally
rowl=ceil(rowl)
//the reason it's done this way, is if you have 103 tiles, it'll have 21 columns etc
for k=1 to rowl //Row Length (20 in this case)
for i=1 to 5 //Column length, 5 in this case
inc l,1 //Counter for array position
if l<=100 //if you had 103 objects, you'd have 21 columns, which would come to 105 objects, but you'd only want 103
//So you'd set this to 103
op[l].id=CreateSprite(0) //Create a blank sprite
a#=k-1 //Need a float value for CURRENT COLUMN-1
b#=i-1 //Need a float value for CURRENT ROW-1
//The offset for this particular tile is defined as 'ROW position' * (tilewidth+2)
//This will create a space of 2px between each tile
op[l].ox=a#*12
//again, width(*aspect ratio)+2 for this tiles Y offset
op[l].oy=b#*((10*aspect)+2)
//Set the tiles width and height
SetSpriteSize(op[l].id,10,10*aspect)
//Set the tiles position!!!
//Y offset includes 5+[], the 5 is the offset of ALL the tiles from the top
//otherwise it;s simply POS+OFFSET
SetSpritePosition(op[l].id,pos+op[l].ox,5+op[l].oy)
endif
next
next
do
if GetPointerPressed()
//initialise TMPx,NEWx and STARTx
tx=GetPointerX()
nx=GetPointerX()
sx=GetPointerX()
endif
if GetPointerState()=1
tx=nx //TMPx = NEWx (therefore TMPx is the X position of the pointer from LAST frame)
nx=GetPointerX() //NEWx is the currect pointer X position
//If the pointer wasn't released this frame, then set the speed of the tiles to the difference between
//the pointerX positions from this frame and last
if GetPointerReleased()=0 then spd=nx-tx
endif
//SLOW the SPEED down
if spd<-0.1 then spd=spd+0.1
if spd>0.1 then spd=spd-0.1
//position = position + speed
pos=pos+spd
//if the speed's going slow enough, the STOP IT!
if spd<0.1 and spd>-0.1 then spd=0
//if the position of the first tile goes past the right-hand end of the screen, the stop it!
if pos>90
spd=-2
pos=90
endif
//if the position of the LAST tile goes past the right-hand end of the screen, the stop it!
//ROW Length-1 * (TILEwidth(10)+1)
if pos<-(rowl-1)*12
pos=-(rowl-1)*12
spd=2
endif
for k=1 to 100 //FOR k = 1 to MAX NUMBER OF TILES
if GetSpriteExists(op[k].id) //if the tile exists.....
//Set it's position via the POSvar and it's offset CENTERED
SetSpritePosition(op[k].id,pos+op[k].ox-(GetSpriteWidth(op[k].id)/2),20+op[k].oy-(GetSpriteHeight(op[k].id)/2))
`SetSpriteColorAlpha(op[k].id,(50-diff(GetSpriteX(op[k].id)+(GetSpriteWidth(op[k].id)/2),50))*4)
//DISTANCE IS 90 to EDGE
//TEMP as float
t#=sin(sprdist(k))+2
`print(t#)
SetSpriteSize(op[k].id, (t#*2.5) , (t#*2.5)*aspect )
endif
next
`print(sprdist(1))
sync()
loop
function diff(a#,b#)
c#=a#-b#
if c#<0 then c#=-c#
endfunction c#
//distance from a tile to the center, 90 = center, 0 = edge
function sprdist(i)
c#=diff(GetSpriteX(op[i].id)+(GetSpriteWidth(op[i].id)/2),50)*1.8
c#=90-c#
endfunction c#
it could to with some perspective, anyone want to try making a method to adjust the perspective of a sprite using memblocks???