Main Menu

Moving Platforms

Started by kevin, September 19, 2009, 02:25:36 PM

Previous topic - Next topic

kevin

 
 Moving Platforms

  This example creates a fairly simple moving platform as found in many a classic 2d game.  The platform moves between a the origin point and the destination, to make it little more interesting, it pauses for a second upon reaching either the origin or target.  



PlayBASIC Code: [Select]
  Setfps 60

OriginX#=100
OriginY#=100

TargetX#=600
TargetY#=400

Mode=0

CurrentDist#=0

DistToTarget#=GetDistance2D(OriginX#,OriginY#,TargetX#,TargetY#)
AngleToTarget#=GetAngle2D(OriginX#,OriginY#,TargetX#,TargetY#)


Do
cls $4080a0

line OriginX#,OriginY#,TargetX#,TargetY#


Select Mode

// -----------------------------------------
case 0 // Platform moving from Original Point to Target
// -----------------------------------------
CurrentDist#= CurrentDist#+2
if CurrentDist#>=DistToTarget#
CurrentDist#=DistToTarget#
PauseTime=Timer()+1000
inc mode
endif

x#=OriginX#+CosRadius(AngleToTarget#,CurrentDist#)
y#=OriginY#+SinRadius(AngleToTarget#,CurrentDist#)

// -----------------------------------------
case 1 // Pausing while at this point
// -----------------------------------------
if PauseTime<Timer()
inc mode
endif

// -----------------------------------------
case 2 // Platform moving from target back to oring
// -----------------------------------------

CurrentDist#= CurrentDist#-2
if CurrentDist#<0
CurrentDist#=0
PauseTime=Timer()+1000
inc mode
endif

x#=OriginX#+CosRadius(AngleToTarget#,CurrentDist#)
y#=OriginY#+SinRadius(AngleToTarget#,CurrentDist#)

// -----------------------------------------
case 3
// -----------------------------------------
if PauseTime<Timer()
mode =0
endif

Endselect


DrawPLatform(X#,y#,50,20)

Sync
loop





Function DrawPLatform(X,y,width,Height)

x1=x-(Width/2)
x2=X+width

y1=y-(Height/2)
y2=y+Height

c1=$a04050
c2=$5060760

shadebox x1,y1,x2,y2,c1,c1,c2,c2

EndFunction







kevin

#1
 This one uses sinus to make the platform appear to accelerate while moving between the points.


PlayBASIC Code: [Select]
  Setfps 60

OriginX#=100
OriginY#=100

TargetX#=600
TargetY#=400


CenterX#=(OriginX#+TargetX#)/2
CenterY#=(OriginY#+TargetY#)/2


Mode=0

DistToTarget#=GetDistance2D(OriginX#,OriginY#,TargetX#,TargetY#)/2
AngleToTarget#=GetAngle2D(OriginX#,OriginY#,TargetX#,TargetY#)




Do
cls $4080a0

line OriginX#,OriginY#,TargetX#,TargetY#


Select Mode

// -----------------------------------------
case 0 // Platform moving from Original Point to Target
// -----------------------------------------

CurrentDistAngle#= CurrentDistAngle#+1

if CurrentDistAngle#>=180
CurrentDistAngle#=180
PauseTime=Timer()+1000
inc mode
endif


CurrentDist#=cos(180+CurrentDistAngle#)*(DistToTarget#)

x#=CenterX#+CosRadius(AngleToTarget#,CurrentDist#)
y#=CenterY#+SinRadius(AngleToTarget#,CurrentDist#)

// -----------------------------------------
case 1 // Pausing while at this point
// -----------------------------------------
if PauseTime<Timer()
inc mode
endif

// -----------------------------------------
case 2 // Platform moving from target back to oring
// -----------------------------------------

CurrentDistAngle#= CurrentDistAngle#+1

if CurrentDistAngle#>=360
CurrentDistAngle#=360
PauseTime=Timer()+1000
inc mode
endif
CurrentDist#=cos(180+CurrentDistAngle#)*(DistToTarget#)
x#=CenterX#+CosRadius(AngleToTarget#,CurrentDist#)
y#=CenterY#+SinRadius(AngleToTarget#,CurrentDist#)

// -----------------------------------------
case 3
// -----------------------------------------
if PauseTime<Timer()
mode =0
CurrentDistAngle#=0
endif

Endselect


DrawPLatform(X#,y#,50,20)


Sync
loop





Function DrawPLatform(X,y,width,Height)

x1=x-(Width/2)
x2=X+width

y1=y-(Height/2)
y2=y+Height

c1=$a04050
c2=$5060760

shadebox x1,y1,x2,y2,c1,c1,c2,c2

EndFunction







micky4fun

#2
Hi all

hope its ok to post just a little snippet using Kevins code ,
think its very worthwhile if you have spare time to go through all the old source codes,

this is 8 moving platforms as in mario sonic etc , could use more or less
thought it might be usefull , see if my latest game requires this !

mick

ATLUS