Hi Guys,
Being new at this I have been attempting to create a simple platform game project. Utilising some the demo code and some of my own tinkering I have created a simple program where I can move a sprite around and jump using the physics commands.
I am having issues with collisions though.
I am trying to get the sprite to change to a jumping frame when the jump is initiated. I am doing this by setting the jump variable to 1 when the jump is initiated.
if GetRawKeyPressed(KEY_UP)=1
//Set sprite to jump frame
SetSpriteFrame(megaman, 10)
//If on ground perform jump
if AtGround(megaman)=1
iy#=-100.0
//Set jump to show jump in progress
jump=1
SetSpriteShapeBox( megaman, -25, -20, 30, 40, 0)
SetSpritePhysicsVelocity(megaman, ix#, iy#)
SetSpritePhysicsImpulse(megaman, GetSpritex(megaman), GetSpritey(megaman),GetSpritePhysicsVelocityX(megaman),GetSpritePhysicsVelocityY(megaman))
endif
endif
I cannot however get the sprite to detect contact with the ground to switch jump back to 0. I want to have it so when the sprite collides with the floor to change the spriteshapebox and set jump=0.
The trouble is I have tried this with this code
if AtGround(megaman)=1
jump=0
SetSpriteShapeBox( megaman, -20, -10, 25, 40, 0)
endif
This seems to make the program automatically change jump=0 as the sprites are technically touching when the jump command is sent.
Is there any way to get the program to check for the collision with the ground only once the jump is initiated?
Full code below in case something else is wrong
// Project: Test
// Created: 2015-07-22
// set window properties
SetWindowTitle( "Test" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetDefaultMagFilter(0)
SetDefaultMinFilter(0)
setphysicsscale(1) //belongs resolution! default 1 unit = 1 meter
SetPhysicsGravity(0 , 50)
setphysicsdebugon()
//Set key constants
#constant KEY_LEFT 37
#constant KEY_UP 38
#constant KEY_RIGHT 39
#constant KEY_DOWN 40
#constant groupground 1
#constant groupobject 2
//Load image and create sprite with frames
MainImage=LoadImage("megaman.png")
megaman=CreateSprite(MainImage)
SetSpriteAnimation(megaman,30,40,65)
SetSpriteFrame(megaman,2)
SetSpriteSize(megaman, 60,80)
SetSpritePosition(megaman,20,600)
//Set all physics on sprite
SetSpritePhysicsOn(megaman,2)
SetSpriteDepth(megaman,0)
SetSpritePhysicsMass (megaman,1000)
setspritephysicsisbullet(megaman,1)
SetSpritePhysicsCanRotate(megaman,0)
SetSpritePhysicsFriction(megaman,0.1)
SetSpritePhysicsRestitution(megaman,0)
SetSpritePhysicsDamping(megaman,0.25)
//Set bounding box to be correct
SetSpriteShapeBox( megaman, -20, -10, 25, 40, 0)
//Box
box = CreateSprite(0)
SetSpriteColor (box,233,75,80,255)
SetSpriteSize (box,50,-1)
SetSpritePosition(box,350,618)
SetSpritePhysicsON(box,1)
SetSpritePhysicsMass (box,2)
SetSpritePhysicsCanRotate(box,1)
SetSpritePhysicsFriction(box,0.75)
SetSpritePhysicsRestitution(box,0)
SetSpritePhysicsDamping(box,0.5)
SetSpriteGroup( box, groupobject )
//Ground
gr = CreateSprite(0)
SetSpriteColor (gr,128,128,128,255)
SetSpriteSize (gr,1024,250)
SetSpritePosition(gr,0,668)
SetSpritePhysicsON(gr,1)
SetSpritePhysicsMass(gr,2)
SetSpritePhysicsCanRotate(gr,0)
SetSpritePhysicsFriction(gr,0.75)
SetSpritePhysicsRestitution(gr,0)
SetSpritePhysicsDamping(gr,0.5)
SetSpriteGroup( gr, groupground )
type player
player as integer
dir as integer
endtype
global player as player
player.dir=0
do
gosub control:
print("ix# =")
print(ix#)
print("iy# =")
print(iy#)
print(jump)
Sync()
loop
control:
//Move when left key press
if GetRawKeyState(KEY_LEFT)
ix#=-25.0
//Check which direction sprite is facing and flip if required
if player.dir=1
setspriteflip(megaman,1,0)
player.dir=0
//If the sprite is on ground animate
elseif AtGround(megaman)=1
if GetSpritePlaying(megaman)=0
playsprite(megaman,10,0,3,6)
endif
//If not jumping set physics to move
if jump=0
SetSpritePhysicsVelocity(megaman, ix#, iy#)
SetSpritePhysicsImpulse(megaman, GetSpritex(megaman), GetSpritey(megaman),GetSpritePhysicsVelocityX(megaman),0)
endif
endif
endif
if GetRawKeyReleased(KEY_LEFT)
ix#=0.0
endif
if GetRawKeyState(KEY_RIGHT)
ix#=25.0
//Check which direction sprite is facing and flip if required
if player.dir=0
setspriteflip(megaman,0,0)
player.dir=1
//If the sprite is on ground animate
elseif AtGround(megaman)=1
if GetSpritePlaying(megaman) = 0
playsprite(megaman,10,0,3,6)
endif
//If not jumping set physics to move
if jump=0
SetSpritePhysicsVelocity(megaman, ix#, iy#)
SetSpritePhysicsImpulse(megaman, GetSpritex(megaman), GetSpritey(megaman),GetSpritePhysicsVelocityX(megaman),0)
endif
endif
endif
if GetRawKeyReleased(KEY_RIGHT)
ix#=0.0
endif
if GetRawKeyPressed(KEY_UP)=1
//Set sprite to jump frame
SetSpriteFrame(megaman, 10)
//If on ground perform jump
if AtGround(megaman)=1
iy#=-100.0
//Set jump to show jump in progress
jump=1
SetSpriteShapeBox( megaman, -25, -20, 30, 40, 0)
SetSpritePhysicsVelocity(megaman, ix#, iy#)
SetSpritePhysicsImpulse(megaman, GetSpritex(megaman), GetSpritey(megaman),GetSpritePhysicsVelocityX(megaman),GetSpritePhysicsVelocityY(megaman))
endif
endif
if GetRawKeyReleased(KEY_UP)
iy#=0.0
endif
if AtGround(megaman)=1
jump=0
SetSpriteShapeBox( megaman, -20, -10, 25, 40, 0)
endif
return
//Function to check if sprite is in contact with ground
function AtGround(spr)
ret=0
c=GetSpriteFirstContact(spr)
do
if c=0 then exit
sprcontact=GetSpriteContactSpriteID2()
if sprcontact<>0
group=getspritegroup(sprcontact)
if group=groupobject or group=groupground
ret=1
exit
endif
endif
c=GetSpriteNextContact()
loop
endfunction ret
Thanks in advance people
James