Hi guys, first post and I'm hoping to get some help here. I want to make a horizontal running + jumping game with platforms. I thought I could adapt the space shooter demo from AGK's examples but I can't figure out how to space them properly, or move the sprite when on them (with a platform) I want the platforms to be wavy like space shooters.
I know the way I tried to move the player with the platform can't be used because when the platform is going up and down because there is a gap , so no collision.
So my question is, can I adapt it or should I take a whole new approach ? If a whole new approach (which I think I need to do ) Any suggestions for generating and moving them the platforms and the player with them?
My butchered attempt is below. Thanks
// Project: platmove
// Created: 2015-08-12
// set window properties
SetWindowTitle( "platmove" )
SetWindowSize( 800, 768, 0 )
// set display properties
SetVirtualResolution( 800, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
Global Player = 0
Player = CreateSprite(LoadImage("sprite_enemy.png"))
SetSpritePhysicsOn(player,2)
SetSpriteSize(player,25,25)
Setphysicsgravity(0,1000)
SetSpritePosition(player, 150,400)
prevx = 0
preprex = 0
type splatform
iSprite as integer
iEngine as integer
fSpeed#
fValueA#
fValueB#
endtype
global g_iShip = 0
global dim g_platforms [ 5 ] as splatform
Function Build()
g_iShip = LoadImage ( "space/platform.png" )
for i = 1 to 5
g_platforms [ i ].iSprite = 0
SetSpritePhysicsOn(g_platforms [ i ].iSprite,1)
g_platforms [ i ].fSpeed# = 0.0
g_platforms [ i ].fValueA# = 0.0
g_platforms [ i ].fValueB# = 0.0
next i
resetplatforms()
EndFunction
Function resetplatforms()
// reset all platforms
// run through them
for i = 1 to 5
// set position
iX = 400 + Random ( 300, 1100 )
iY = Random ( 600, 700 )
// delete platform if it exists
if GetSpriteExists ( g_platforms [ i ].iSprite ) = 1
DeleteSprite ( g_platforms [ i ].iSprite )
endif
// create platform and engine
g_platforms [ i ].iSprite = CreateSprite ( g_iShip )
// give it a box shape
SetSpriteShape ( g_platforms [ i ].iSprite, 2 )
SetSpritePosition ( g_platforms [ i ].iSprite, iX, iY )
//
SetSpritePhysicsOn(g_platforms [ i ].iSprite,1)
// set up for movement
speed# = Random ( 10, 20 )
g_platforms [ i ].fSpeed# = speed# / 10.0
speed# = Random ( 1, 10 )
g_platforms [ i ].fValueA# = speed# / 100.0
g_platforms [ i ].fValueB# = 0.0
next i
Endfunction
Function Updat()
// update the platforms
for i = 1 to 5
if GetSpriteCollision ( g_platforms [ 1 ].iSprite, g_platforms [ 5 ].iSprite ) = 1
endif
// get the new location
fShipX# = GetSpriteX ( g_platforms [ i ].iSprite ) - g_platforms [ i ].fSpeed#
fShipY# = GetSpriteY ( g_platforms [ i ].iSprite )
// apply wavy movement
g_platforms [ i ].fValueB# = g_platforms [ i ].fValueB# + g_platforms [ i ].fValueA#
fShipY# = fShipY# + cos ( g_platforms [ i ].fValueB# * 180 / 3.14 )
// reset once they have moved off the screen
if ( WorldToScreenX ( fShipX# ) < 0 )
// new position off to the right
fShipX# = 400 + Random ( 600, 875 )
If abs (prevx-fshipX#)<300
fshipX# = fshipX# + 200
Endif
If abs (prevx-fshipX#)>600
if (prevx-fshipx#)<-300
fshipX# = fshipX# + 100
endif
if abs (prevx-fshipx#)> 600
if (prevx-fshipx#)<-300
fshipX# = fshipX# + 100
endif
endif
Endif
fShipY# = Random ( 600, 700 )
prevx = fshipx#
// convert to world position instead of screen (because of scrolling)
fShipX# = ScreenToWorldX ( fShipX# )
fShipY# = ScreenToWorldY ( fShipY# )
// generate new values for movement
speed# = 20
g_platforms [ i ].fSpeed# = speed# / 10.0
speed# = Random ( 9, 20 )
g_platforms [ i ].fValueA# = speed# / 100.0
g_platforms [ i ].fValueB# = 0.0
endif
// update position of platform and engine
SetSpritePosition ( g_platforms [ i ].iSprite, fShipX#, fShipY# )
next i
Endfunction
Function Collisions ()
if GetSpriteCollision ( player, g_platforms [ 1 ].iSprite ) = 1
setSpritePhysicsVelocity(player,GetSpritePhysicsVelocityX(g_platforms [ 1 ].iSprite) , GetSpritePhysicsVelocityY(g_platforms [ 1 ].iSprite))
endif
if GetSpriteCollision ( player, g_platforms [ 2 ].iSprite ) = 1
setSpritePhysicsVelocity(player,GetSpritePhysicsVelocityX(g_platforms [ 2 ].iSprite) , GetSpritePhysicsVelocityY(g_platforms [ 2 ].iSprite))
endif
if GetSpriteCollision ( player, g_platforms [ 3 ].iSprite ) = 1
setSpritePhysicsVelocity(player,GetSpritePhysicsVelocityX(g_platforms [ 3 ].iSprite) , GetSpritePhysicsVelocityY(g_platforms [ 3 ].iSprite))
endif
if GetSpriteCollision ( player, g_platforms [ 4 ].iSprite ) = 1
setSpritePhysicsVelocity(player,GetSpritePhysicsVelocityX(g_platforms [ 4 ].iSprite) , GetSpritePhysicsVelocityY(g_platforms [ 4 ].iSprite))
endif
if GetSpriteCollision ( player, g_platforms [ 5 ].iSprite ) = 1
setSpritePhysicsVelocity(player,GetSpritePhysicsVelocityX(g_platforms [ 5 ].iSprite) , GetSpritePhysicsVelocityY(g_platforms [ 5 ].iSprite))
endif
endfunction
build()
do
updat()
Collisions()
if GetPointerPressed ( ) = 1
setSpritePhysicsVelocity(player,100,-500)
endif
Print( ScreenFPS() )
Sync()
loop