Hi Steve
Your collision isnt working as its checking against one single sprite ID (spr4) for collisions when there are a number of sprites that are collidable.
One way to fix this is to store all the Sprite ID's of the collidable tiles then check for a collision against them. This is done in the code below and can simply replace your main file and works with collision. If a collision occurs then the player is moved back to where they were before the collision as to not get stuck on the object they collided against.
// Project: Collision Test
// Created: 2019-01-03
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Top Down Adventure" )
SetWindowSize( 960, 540, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 320, 180 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
//SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetVSync( 1 )
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// tiles -----------------------
global horseVX as float
global horseVY as float
global horseJump as float
global HorseGrav as float
global PlySpeed as float
horseVX = 160
horseVY = 90
horseJump = 4.0
horseGrav = 0.2
PlySpeed = 1
testHorse = LoadImage ( "GreenDude.png" )
SetImageMagFilter ( testHorse, 0 )
SetImageMinFilter ( testHorse, 0 )
//Globals to store the current map view offset
global viewX as float
global viewY as float
viewX = 0.0
viewY = 0.0
SetViewOffset(viewX,viewY)
// this controls the zoom amount
zoom as float
zoom = 1
CreateSprite( 1001, testHorse )
SetSpriteAnimation(1001, 16, 16, 5)
PlaySprite ( 1001, 6, 1, 1, 5 )
CreateSprite( 1002, testHorse )
SetSpriteAnimation(1002, 16, 16, 5)
PlaySprite ( 1002, 6, 1, 1, 5 )
//SetSpritePosition( 101, 90, 180)
SetSpriteDepth(1001, 1)
SetSpriteDepth(1002, 1)
IMFloor = LoadImage ( "Floor1.png" )
IMWall4 = LoadImage ( "Wall1.png" )
IMWall7 = LoadImage ( "Wall4.png" )
Collision = LoadImage ( "CollisionTile.png" )
SetImageMagFilter ( IMFloor, 0 )
SetImageMinFilter ( IMFloor, 0 )
SetImageMagFilter ( IMWall4, 0 )
SetImageMinFilter ( IMWall4, 0 )
SetImageMagFilter ( IMWall7, 0 )
SetImageMinFilter ( IMWall7, 0 )
SetImageMagFilter ( Collision, 0 )
SetImageMinFilter ( Collision, 0 )
tile as integer = 8
// Creating an array
map as integer[40,23]
//dim mapset[6,6]
local fileID as integer
local line as string
local tileID as string
if GetFileExists("MapData.txt")
fileID = OpenToRead("MapData.txt")
for y=0 to 22
line = Readline(fileID)
for x=0 to 39
tileID = GetStringToken2(line, ",", x + 1)
map[x, y] = val(tileID)
next x
next y
CloseFile(fileID)
endif
global cols as integer[]
for j = 0 to 22
for i = 0 to 39
if map[i,j] = 0
spr1 = CreateSprite ( IMFloor )
SetSpritePosition ( spr1, i*tile, j*tile )
elseif map[i,j] = 1
spr2 = CreateSprite ( IMWall4 )
SetSpritePosition ( spr2, i*tile, j*tile )
elseif map[i,j] = 2
spr3 = CreateSprite ( IMWall7 )
SetSpritePosition ( spr3, i*tile, j*tile )
elseif map[i,j] = 3
spr4 = CreateSprite ( Collision )
SetSpritePosition ( spr4, i*tile, j*tile )
cols.insert(spr4) // store all the collidable sprite id's
endif
next
next
do
// Check for all collisions
//if GetSpriteCollision(1001, spr4) = 1
PlySpeed = 1
// Store OLD horse positions
oldx = horseVX
oldy = horseVY
// use WASD to move horse
if GetRawKeyState(65) = 1
horseVX = horseVX - PlySpeed
endif
if GetRawKeyState(68) = 1
horseVX = horseVX + PlySpeed
endif
if GetRawKeyState(87) = 1
horseVY = horseVY - PlySpeed
endif
if GetRawKeyState(83) = 1
horseVY = horseVY + PlySpeed
endif
if GetRawKeyState(32) = 1
horseVY = horseVY - HorseJump
endif
// Set the new position
SetSpritePosition( 1001, horseVX, horseVY) // test horse sprite
// Check for a collision with any of the colidable tiles
for i=0 to cols.length
if GetSpriteCollision(1001, cols[i]) = 1
// If a collision has occured then set the position back to the previous position
SetSpritePosition( 1001, oldx, oldy)
horseVX = oldx
horseVY = oldy
continue
endif
next i
// page up and page down to zoom the map with SetViewZoom()
if GetRawKeyState(90) = 1
if zoom < 2
zoom = zoom + 0.25
SetViewZoom( zoom)
endif
//zoom = 1
endif
if GetRawKeyState(90) = 0
if zoom > 1
zoom = zoom - 0.25
SetViewZoom( zoom )
endif
//zoom = 1
endif
SetSpritePosition( 1002, 140, 70) // test horse sprite
sync()
loop
I would also add the following points too.
1) You can apply physics properties to the wall sprites and use physics to move the character then collision is then handled for you by AGK. Its still possible to do linear movement but the collision is a bit more realistic and easier to handle.
2) The above method is slow as it checks for collision against every single sprite rather than just checking those which are closest to the character - it was easy to implement though. If you store all the sprite ID's as an array then you could just then check the closest 9 sprites to the player and so the collision check is a lot faster rather then checking all the wall sprites against the player sprite
3) Someone (i think it was garbenjamin) did a platform game template which is worth checking out as this has the code for the collision testing built it. Even though it was a tile based platformer it is still valid for top down adventure. Also, there are numerous tile importers to import from the Tiled software straight into AppGameKit and so using a txt fie isnt needed and will allow you to design your maps in a specific free bit of software designed for it.
4) In your original code your creating a lot of sprites but not storing the ID's anywhere and so deleting them at the end of a level would be difficult without calling deleteallsprites(). Its probably worth having a map array variable to store all the Sprite id's in.