Hello Shedz,
I made a physics based platformer that uses sprite groups like you want to.
I'm not the greatest coder and it isn't fully completed, but it should help to get you in the right direction.
Here is the full source code: (I have attached the code and media too)
// Project: FallingUp
// Creator: JHA
// Started: October 7, 2014
// Set Window Properties
SetWindowTitle("Falling Up")
SetWindowSize(1280, 1024, 0)
SetVirtualResolution(1280, 1024)
// Initialization Routines
Gosub _Setup
Gosub _InitializeLevel1
Gosub _DrawAttributes
// Main Loop
do
Gosub _Collisions
Gosub _MoveHero
Gosub _MoveEnemies
Gosub _MoveObjects
Gosub _DisplayMessages
// Press ESC to Exit Program
if GetRawKeyPressed(27) = 1 then End
Sync()
loop
End
// Routines
_Collisions:
// Detect The Ground and Climable objects
Climable = SpriteRayCastGroup(canClimb, GetSpriteX(sprHero), GetSpriteY(sprHero), GetSpriteX(sprHero)+32, GetSpriteY(sprHero)+32)
Grounded = SpriteRayCastGroup(onGround, GetSpriteX(sprHero)+16, GetSpriteY(sprHero)+16, GetSpriteX(sprHero)+16, GetSpriteY(sprHero)+50)
// Check for Enemy hit. Needs Updating: Jump on top, kill it. Walk into it, it kills you!
hitEnemy = GetSpriteHitGroup(Enemy, GetSpriteX(sprHero) + 16, GetSpriteY(sprHero) + 24)
if hitEnemy > 0
DeleteSprite(hitEnemy)
dec Score, 20
endif
// Check for Prize items and update score. (stars, etc.)
hitPrize = GetSpriteHitGroup(canGet, GetSpriteX(sprHero) + 16, GetSpriteY(sprHero) + 24)
if hitPrize <> 0
DeleteSprite(hitPrize)
inc Score, 10
endif
required = GetSpriteHitGroup(theKey, GetSpriteX(sprHero) + 16, GetSpriteY(sprHero) + 24)
if required <> 0
DeleteSprite(required)
inc Score, 100
hasKey = 1
endif
// If colliding with Door, make sure the Key has been obtained before declaring a win.
winDoor = GetSpriteHitGroup(winner, GetSpriteX(sprHero) + 16, GetSpriteY(sprHero) + 24)
if winDoor <> 0
if hasKey = 1
winMessage$ = "WINNER!!!"
SetSpriteImage(winDoor, imgDoor2)
else
winMessage$ = "You need the key first!"
hasKey = 0
endif
else
winMessage$ = ""
endif
Return
_MoveHero:
// Move Left or Right - Cursor Keys
moveRight = GetRawKeyState(39)
if moveRight > 0
Dir = 1
SetSpritePhysicsVelocity(sprHero, GetRawKeyState(39) * 200, GetSpritePhysicsVelocityY(sprHero))
endif
moveLeft = GetRawKeyState(37)
if moveLeft > 0
Dir = 2
SetSpritePhysicsVelocity(sprHero, GetRawKeyState(37) * -200, GetSpritePhysicsVelocityY(sprHero))
endif
// Standing Still
if moveRight = 0 and moveLeft = 0
Dir = 0
SetSpritePhysicsVelocity(sprHero, 0, GetSpritePhysicsVelocityY(sprHero))
endif
// Jumping - Space Bar
jump = GetRawKeyPressed(32)
if jump > 0 and Grounded = 1
SetSpritePhysicsVelocity(sprHero, GetSpritePhysicsVelocityX(sprHero), -jump * 240)
endif
// Climb Ladders or Ropes - Cursor Keys
if Climable = 1
climbUp = GetRawKeyState(38)
if climbUp = 1 and climbDown = 0
Dir = 3
SetSpritePhysicsVelocity(sprHero, GetSpritePhysicsVelocityX(sprHero), -climbUp * 175)
climbing = 1
endif
climbDown = GetRawKeyState(40)
if climbDown = 1 and climbUp = 0
Dir = 4
SetSpritePhysicsVelocity(sprHero, GetSpritePhysicsVelocityX(sprHero), climbDown * 175)
endif
// Account for gravity by applying opposite force when stopped on climable structures.
if climbDown = 0 and climbUp = 0
Dir = 5
SetSpritePhysicsVelocity(sprHero, GetSpritePhysicsVelocityX(sprHero), 0)
SetSpritePhysicsForce(sprHero, GetSpriteX(sprHero), GetSpriteY(sprHero), 0, drop#)
endif
else
if climbing = 1 and Climable = 0
SetSpritePhysicsVelocity(sprHero, GetSpritePhysicsVelocityX(sprHero), 0)
SetSpritePhysicsForce(sprHero, GetSpriteX(sprHero), GetSpriteY(sprHero), 0, drop#)
climbing = 0
endif
endif
// Animate Hero
inc iterations: if iterations > 17 then iterations = 1
Select Dir
case 0: // Stop
frame = 1
endcase
case 1: // Right
if Grounded = 1
if frame < 9 or frame > 12 then frame = 9
if Mod(iterations, 4) = 0 then inc frame
else
frame = 9
endif
endcase
case 2: // Left
if Grounded = 1
if frame < 5 or frame > 8 then frame = 5
if Mod(iterations, 4) = 0 then inc frame
else
frame = 5
endif
endcase
case 3: // Up
if frame < 13 or frame > 16 then frame = 13
if Mod(iterations, 4) = 0 then inc frame
endcase
case 4: // Down
if frame < 13 or frame > 16 then frame = 13
if Mod(iterations, 4) = 0 then inc frame
endcase
case 5: // Stopped on Ladder/Rope
frame = 13
endcase
endSelect
SetSpriteFrame(sprHero, frame)
Return
_MoveEnemies:
Return
_MoveObjects:
Return
_DisplayMessages:
// Display Variables for Debugging
SetTextString(1, "Score: " + str(Score) + " " + winMessage$)
SetTextString(2, "Frame: " + str(frame))
SetTextString(3, "Has Key?: " + str(hasKey))
Return
_DrawAttributes:
// Draw Attributes Section
sprStats = CreateSprite(imgStats)
SetSpritePosition(sprStats, 0, 768)
Return
_Setup:
// Groups
canClimb = 1
onGround = 2
Enemy = 3
canGet = 4
theKey = 5
winner = 6
// Variables
Score = 0
hasKey = 0
gravity = 300
drop# = -9530
winMessage$ = ""
frame = -1
iterations = 1
climbing = 0
// Load Images
imgBack = LoadImage("background.png")
imgHero = LoadImage("wizard.png")
imgBaddie = LoadImage("enemy.png")
imgPlatLeft = LoadImage("girderl.png")
imgPlatMiddle = LoadImage("girderm.png")
imgPlatRight = LoadImage("girderr.png")
imgPlatVert = LoadImage("girderv.png")
imgLadder = LoadImage("ladder.png")
imgPlatLadder = LoadImage("girderladder.png")
imgRope = LoadImage("chain.png")
imgBrick = LoadImage("wall.png")
imgOverlay = LoadImage("shadedpixelfill.png")
imgStar = LoadImage("star.png")
imgKey = LoadImage("key32.png")
imgDoor1 = LoadImage("closeddoor.png")
imgDoor2 = LoadImage("opendoor.png")
imgStats = LoadImage("stats.png")
// Frames For Character Animation
// 1 - 4 = Walking Down
// 5 - 8 = Walking Left
// 9 - 12 = Walking Right
// 13 - 16 = Walking Up
imgWizard = LoadImage("wizardatlas.png")
// Setup Hero - Need to Animate
hX# = 16.0: hY# = 700.0
sprHero = CreateSprite(imgWizard)
SetSpriteAnimation(sprHero, 32, 48, 16)
SetSpriteFrame(sprHero, 0)
SetSpritePosition(sprHero, hX#, hY#)
SetSpriteDepth(sprHero, 1)
SetSpriteShapeCircle(sprHero, 0, 0, 15.9)
SetSpritePhysicsOn(sprHero, 2)
SetSpritePhysicsCanRotate(sprHero, 0)
// World Physics Properties
SetPhysicsGravity(0, gravity)
// Messaging Text Setup
CreateText(1, ""): SetTextSize(1, 36): SetTextPosition(1, 10, 775)
CreateText(2, ""): SetTextSize(2, 24): SetTextPosition(2, 10, 860)
CreateText(3, ""): SetTextSize(3, 24): SetTextPosition(3, 10, 830)
Return
_InitializeLevel1:
Gosub _Level1Data
// Create Level
BLOCK = 999: loc = 0: WX = 0: WY = 0
For Y = 1 to 24
For X = 1 to 40
if mid(m$[loc], X, 1) = "0" // Platform Vertical
Inc BLOCK
CreateSprite(BLOCK, imgPlatVert)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsOn(BLOCK,1)
Endif
if mid(m$[loc], X, 1) = "2" // Platform Left
Inc BLOCK
CreateSprite(BLOCK, imgPlatLeft)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsOn(BLOCK,1)
SetSpriteGroup(BLOCK, onGround)
Endif
if mid(m$[loc], X, 1) = "3" // Platform Middle
Inc BLOCK
CreateSprite(BLOCK, imgPlatMiddle)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsOn(BLOCK,1)
SetSpriteGroup(BLOCK, onGround)
Endif
if mid(m$[loc], X, 1) = "4" // Platform Right
Inc BLOCK
CreateSprite(BLOCK, imgPlatRight)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsOn(BLOCK,1)
SetSpriteGroup(BLOCK, onGround)
Endif
if mid(m$[loc], X, 1) = "5" // Ladder
Inc BLOCK
CreateSprite(BLOCK, imgLadder)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpriteDepth(BLOCK, 9)
SetSpritePhysicsIsSensor(BLOCK, 0)
SetSpriteGroup(BLOCK, canClimb)
Endif
if mid(m$[loc], X, 1) = "6" // Rope
Inc BLOCK
CreateSprite(BLOCK, imgRope)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpriteDepth(BLOCK, 9)
SetSpritePhysicsIsSensor(BLOCK, 1)
SetSpriteGroup(BLOCK, canClimb)
Endif
if mid(m$[loc], X, 1) = "L" // Ladder Platform Combo
Inc BLOCK
CreateSprite(BLOCK, imgPlatLadder)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpriteDepth(BLOCK, 9)
SetSpritePhysicsIsSensor(BLOCK, 1)
SetSpriteGroup(BLOCK, canClimb)
Endif
if mid(m$[loc], X, 1) = "B" // Enemies
Inc BLOCK
CreateSprite(BLOCK, imgBaddie)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsIsSensor(BLOCK,2)
//SetSpritePhysicsCanRotate(BLOCK,0)
SetSpriteGroup(BLOCK, Enemy)
Endif
if mid(m$[loc], X, 1) = "S" // Star
Inc BLOCK
CreateSprite(BLOCK, imgStar)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsIsSensor(BLOCK, 1)
SetSpriteGroup(BLOCK, canGet)
Endif
if mid(m$[loc], X, 1) = "K" // Key
Inc BLOCK
CreateSprite(BLOCK, imgKey)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsIsSensor(BLOCK, 1)
SetSpriteGroup(BLOCK, theKey)
Endif
if mid(m$[loc], X, 1) = "D" // Door
Inc BLOCK
CreateSprite(BLOCK, imgDoor1)
SetSpritePosition(BLOCK, WX, WY-32)
SetSpriteShape(BLOCK, 2)
SetSpritePhysicsIsSensor(BLOCK, 1)
SetSpriteGroup(BLOCK, winner)
SetSpriteDepth(BLOCK, 2)
Endif
if mid(m$[loc], X, 1) = "d" // Door - Unuseable
Inc BLOCK
CreateSprite(BLOCK, imgDoor1)
SetSpritePosition(BLOCK, WX, WY-32)
Endif
// Wall Background
Inc BLOCK
CreateSprite(BLOCK, imgBrick)
SetSpritePosition(BLOCK, WX, WY)
SetSpriteDepth(BLOCK, 99)
inc WX, 32
Next X
inc loc
inc WY, 32
WX = 0
Next Y
Return
_Level1Data:
Dim m$[24]
m$[0] = "2333333333333333333333333333333333333334"
m$[1] = "0S111111111116111111161111111111111S1110"
m$[2] = "0S11111111111611111116111SS1111111111D10"
m$[3] = "0S11111111B1161111111611S111111111233410"
m$[4] = "0111111112341611111116111111111111111110"
m$[5] = "2333L33411111611111116111111111111111110"
m$[6] = "0111511111111611111116111111123333411110"
m$[7] = "0111511111111611111111111111111111111110"
m$[8] = "0111511111111111111111111111111111111110"
m$[9] = "0112341111111111111111112341111111111110"
m$[10] = "01111111111111111B1111111111111111111110"
m$[11] = "0B111111111111123334111111112L3411111110"
m$[12] = "2341111111111111111111111111151111111110"
m$[13] = "0111111111111111111111111111151111111110"
m$[14] = "01111K11111111111S111111111115111111S110"
m$[15] = "0111234111111111S1S1111111111511111SSS10"
m$[16] = "011111111111111111111111111115111111S110"
m$[17] = "0111111111111111111111111111151111111110"
m$[18] = "0111111111111111234111111111151111123334"
m$[19] = "0111112L33411111111111111111151111111110"
m$[20] = "0111111511111111111111111123451111111110"
m$[21] = "z111111511111111111111111111151111111110"
m$[22] = "d111111511111111111111111111151111111110"
m$[23] = "2333333333333333333333333333333333333334"
Return
I hope it helps and let me know if you have any questions.
Thank you
JHA
Proverbs Challenge: http://pc.potentialsunleashed.com/
Using AppGameKit V2 Tier 1 - FPSC Reloaded / Game Guru